JdbcTemplate 概述 JdbcTemplate 是 spring 框架中提供的一个对象,是对原始繁琐的 Jdbc API 对象的简单封装。
spring框架为我们提供了很多的操作模板类 。 例如:操作关系型数据的 JdbcTemplate 和 HibernateTemplate 操作 nosql 数据库的 RedisTemplate,操作消息队列的 JmsTemplate 等等。
JdbcTemplate 开发步骤 ① 导入spring-jdbc和spring-tx坐标 ② 创建数据库表和实体 ③ 创建JdbcTemplate对象 ④ 执行数据库操作
1. 导入坐标 1 2 3 4 5 6 7 8 9 10 11 12 <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 5.0.5.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-tx</artifactId > <version > 5.0.5.RELEASE</version > </dependency >
2. 创建数据库表和实体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Account { private String name; private double money; public String getName () { return name; } public void setName (String name) { this .name = name; } public double getMoney () { return money; } public void setMoney (double money) { this .money = money; } @Override public String toString () { return "Account{" + "name='" + name + '\'' + ", money=" + money + '}' ; } }
3. 创建JdbcTemplate对象 1 2 3 4 5 6 7 8 9 10 11 12 13 @Test public void test () throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver" ); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false" ); dataSource.setUser("root" ); dataSource.setPassword("root" ); JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); }
4. 执行数据库操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Test public void test () throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver" ); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false" ); dataSource.setUser("root" ); dataSource.setPassword("root" ); JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); int row = jdbcTemplate.update("insert into account values(?,?)" , "tom" , 5000 ); System.out.println("影响行数:" + row); }
Spring 产生 JdbcTemplate 对象 我们可以将 JdbcTemplate 的创建权交给 Spring,将数据源 DataSource 的创建权也交给 Spring,在 Spring 容器内部将数据源 DataSource 注入到 JdbcTemplate 模版对象中,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 <bean id ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name ="driverClass" value ="com.mysql.jdbc.Driver" /> <property name ="jdbcUrl" value ="jdbc:mysql://localhost:3306/test" /> <property name ="user" value ="root" /> <property name ="password" value ="root" /> </bean > <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" > <property name ="dataSource" ref ="dataSource" /> </bean >
从容器中获得 JdbcTemplate 进行添加操作:
1 2 3 4 5 6 7 @Test public void test2 () throws PropertyVetoException { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml" ); JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class); int row = jdbcTemplate.update("insert into account values(?,?)" , "gaojie" , 4000 ); System.out.println("影响行数:" + row); }
在解耦的基础上,进一步解耦,抽取 jdbc 参数。
1 2 3 4 jdbc.driver =com.mysql.jdbc.Driver jdbc.url =jdbc:mysql://localhost:3306/test?useSSL=false jdbc.username =root jdbc.password =root
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <context:property-placeholder location ="classpath:jdbc.properties" /> <bean id ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name ="driverClass" value ="${jdbc.driver}" /> <property name ="jdbcUrl" value ="${jdbc.url}" /> <property name ="user" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> </bean > <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" > <property name ="dataSource" ref ="dataSource" /> </bean >
JdbcTemplate 常用操作 更新操作 1 2 3 4 5 6 7 8 9 10 11 12 13 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateCURDTest { @Autowired private JdbcTemplate jdbcTemplate; @Test public void testUpdate () { int row = jdbcTemplate.update("update account set money = ? where name = ?" , 10000 , "gaojie" ); System.out.println("影响行数:" + row); } }
删除操作 1 2 3 4 5 @Test public void testDelete () { int row = jdbcTemplate.update("delete from account where name = ?" , "lisa" ); System.out.println("影响行数:" + row); }
查询操作 查询全部
1 2 3 4 5 @Test public void testQueryAll () { List<Account> accountList = jdbcTemplate.query("select * from account" , new BeanPropertyRowMapper<Account>(Account.class)); System.out.println(accountList); }
查询单个对象 简单查询
1 2 3 4 5 @Test public void testQueryOne () { Account account = jdbcTemplate.queryForObject("select * from account where name = ?" , new BeanPropertyRowMapper<Account>(Account.class), "tom" ); System.out.println(account);; }
聚合查询
1 2 3 4 5 @Test public void testQueryCount () { Long account = jdbcTemplate.queryForObject("select count(*) from account" , Long.class); System.out.println(account); }