17.Spring中的JdbcTemplate

◆作用:用于和数据库交互,实现对表的CRUD操作

 
1.JdbcTemplate最基本用法
pom依赖:
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>
定义与表格对应的实体,设置setter和getter以及toString方法

main函数:
public class JdbcTemplateDemo1 {

    public static void main(String[] args) {
        //准备数据源,spring的内置数据源
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/test");
        ds.setUsername("root");
        ds.setPassword("sql9630");
        //1.创建JdbcTemplate对象
        JdbcTemplate jt = new JdbcTemplate();
        //给jt设置数据源
        jt.setDataSource(ds);
        //2.执行操作
        jt.execute("insert into account(uname,money)values('ccc',1000)");
    }
}

2.JdbcTemplate在xml文件中配置的用法

pom依赖和实体类Account同上,xml文件中配置:

main函数:

public class JdbcTemplateDemo2 {

    public static void main(String[] args) {
       //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.获取对象
        JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
        //3.执行操作
        jt.execute("insert into account(uname,money)values('ddd',222)");
    }
}

3.JdbcTemplate的增删查改
方法①:同以上例子,直接用jt.execute()执行所有sql语句

方法②:

保存  

 jt.update("insert into account(uname,money)values(?,?)","eee",333f);

更新   

jt.update("update account set uname=?,money=? where id= ?","www",9890f,7);

删除  

 jt.update("delete from account where id=?",8);

查询所有(自定义Account封装策略) 

List<Account> accounts = jt.query("select * from account where money > ?",new AccountRowMapper(),1000f);
 //定义Account的封装策略
class AccountRowMapper implements RowMapper<Account>{
     //把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account = new Account();
        account.setId(resultSet.getInt("id"));
        account.setUname(resultSet.getString("uname"));
        account.setMoney(resultSet.getFloat("money"));
        return account;
    }
}

查询所有(使用Spring提供的RowMapper)  

List<Account> accounts = jt.query("select * from account where money > ?",new BeanPropertyRowMapper<Account>(Account.class),1000f);

查询一个 

List<Account> accounts = jt.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),3);

查询返回一行一列   

int x = jt.queryForObject("select count(*) from account where money > ?",Integer.class,1000f);

4.JdbcTemplate在Dao层使用

pom文件配置同上,实体类Account和持久层接口IAccountDao代码省略

接口实现类代码

xml文件配置

main函数测试

★使用继承 JdbcDaoSupport的方式抽取代码

Spring自带JdbcDaoSupport类,此处我们抽取了JdbcTemplate的相关代码,而用父类的getJdbcTemplate方法获取。而且JdbcDaoSupport可以直接注入dataSource自动生成JdbcTemplate

此时的xml中只需注入dataSource即可

发布了51 篇原创文章 · 获赞 1 · 访问量 1069

猜你喜欢

转载自blog.csdn.net/si_si_si/article/details/104750828