1.它是spring框架中提供的一个对象,是对原始繁琐的JdbcAPI对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。
2.JdbcTemplate开发步骤
(1)导入spring-jdbc和spring-tx坐标
(2)创建数据库表和实体
(3)创建JdbcTemplate对象
(4)执行数据库操作
代码示例:
//pom.xml导入spring-jdbc和spring-tx坐标
org.springframework
spring-jdbc
5.0.5.RELEASE
org.springframework
spring-tx
5.0.5.RELEASE
代码示例:
//创建数据库表和实体,Account.java
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 +
'}';
}
}
代码示例:
//创建JdbcTemplate对象,JdbcTemplateTest.java
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import java.beans.PropertyVetoException;
public class JdbcTemplateTest {
@Test
//测试JdbcTemplate开发步骤
public void test1() throws PropertyVetoException {
//创建一个数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("123456");
//创建JdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//设置数据源对象,知道数据库在哪
jdbcTemplate.setDataSource(dataSource);
//执行操作
int row = jdbcTemplate.update("insert into account values(?,?)","小C",50000);
System.out.println(row);
}
}
测试截图:
3.Spring产生JdbcTemplate对象
我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到JdbcTemplate模板对象中。
代码示例:
//applicationContext.xml
//创建JdbcTemplate对象,JdbcTemplateTest.java
@Test
//测试Spring产生JdbcTemplate对象
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(?,?)","小B",65000);
System.out.println(row);
}
测试截图:
4.JdbcTemplate常用操作
(1)更新操作
代码示例:
//JdbcTemplateCRUDTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testUpdate(){
jdbcTemplate.update("update account set money=? where name=?",100000,"小C");
}
}
测试截图:
(2)删除操作
代码示例:
//JdbcTemplateCRUDTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testDelete(){
jdbcTemplate.update("delete from account where name=?","小C");
}
}
测试截图:
(3)查询操作
①查询多个对象
代码示例:
@Test
public void testQueryAll(){
List accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper(Account.class));
System.out.println(accountList);
}
测试截图:
②查询单个对象
代码示例:
@Test
public void testQueryOne(){
Account account = jdbcTemplate.queryForObject("select * from account where name=?",new BeanPropertyRowMapper(Account.class),"小C");
System.out.println(account);
}
测试截图:
(4)查询数据个数
代码示例:
@Test
public void testQueryCount(){
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
测试截图:



