JdbcTemplate 概述

它是 Spring 框架中提供的一个对象,是对原始繁琐的 jdbc api 对象的简单封装。 Spring 框架为我们提供了很多的操作模板类,例如: 操作关系型数据的 JdbcTemplate 和 HibernateTemplate,操作 nosql 数据库的 RedisTemplate,操作消息队列的 JmsTemplate 等等。

JdbcTemplate 开发步骤

导入坐标

导入 spring-jdbcspring-tx 的坐标。

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

2. 创建数据库实体类

3. 创建数据源

4. 使用 JdbcTemplate

@Test
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("root");

JdbcTemplate jdbcTemplate = new JdbcTemplate();
// 设置数据源对象
jdbcTemplate.setDataSource(dataSource);
// 执行操作
int rows = jdbcTemplate.update("insert into student values(?, ?)", "tom", 18);
System.out.println(rows);
}

Spring 配置 JdbcTemplate

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<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>

</beans>
@Test
public void test2(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");
int rows = jdbcTemplate.update("insert into student values(?, ?)", "wek", 19);
System.out.println(rows);
}

Spring JdbcTemplate 增删改查

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

@Autowired
private JdbcTemplate jdbcTemplate;

@Test
public void testInsert() {
int rows = jdbcTemplate.update("insert into student values(?, ?)", "tom", 18);
System.out.println(rows);
}
@Test
public void testUpdate() {
int rows = jdbcTemplate.update("update student set age = 2211 where name = ?", "tom");
System.out.println(rows);
}
@Test
public void testDelete() {
int rows = jdbcTemplate.update("delete from student where name = ?", "tom");
System.out.println(rows);
}
@Test
public void testQueryAll() {
List<Student> students = jdbcTemplate.query("select * from student", new BeanPropertyRowMapper<Student>(Student.class));
System.out.println(students);
}
@Test
public void testQueryOne() {
Student tom = jdbcTemplate.queryForObject("select * from student where name = ?", new BeanPropertyRowMapper<Student>(Student.class), "tom");
System.out.println(tom);
}
@Test
public void testQueryCount() {
int count = jdbcTemplate.queryForObject("select count(1) from student", int.class);
System.out.println(count);
}
}