1 JdbcTemplate开发步骤
jdbcTemplate是Spring框架中提供的一个对象,是对原始繁琐的jdbcAPI对象的简单封装,Spring框架为我们提供了很多的操作模板类,如操作关系数据的jdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate
jdbcTemplate的开发步骤:
1,导入spring-jdbc和spring-tx相关jar包
2,创建数据库表和对应实体
3,创建jdbcTemplate对象
4,执行数据库操作
1,导入spring-jdbc和spring-tx相关jar包:
<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,创建数据库表和对应实体:
数据库中的User表:
对应的实体类:
3,创建jdbcTemplate对象:
@Test
public void test1() throws PropertyVetoException {
// 准备数据源
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/chatroom");
dataSource.setUser("root");
dataSource.setPassword("111");
// 创建JdbcTemplate对象并设置数据源
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
}
4,执行数据库操作:
2 Spring产生JdbcTemplate对象
同样可以将JdbcTemplate对象作为Bean由Spring产生,使用时直接从容器中获取即可,但是jdbcTemplate对象必须注入数据源才能正常使用,因为其底层的操作也都是基于Connection对象进行的
在jdbc.properties文件中配置数据源需要的基本数据:
在applicationContext.xml对象中进行配置:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 导入jdbc.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 配置dataSource -->
<bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dateSource"></property>
</bean>
</beans>
测试:
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
int row = jdbcTemplate.update("INSERT INTO sys_client_userinfo VALUES (?, ?, ?)", 666, "coin", "666");
System.out.println(row);
}
3 JdbcTemplate的常用操作
使用的表:
3.1 更新
更新操作:jdbdTemplate.update(sql, params);
使用JdbcTemplate对象进行插入操作:
jdbcTemplate.update("INSERT INTO sys_client_userinfo VALUES (?, ?, ?)", 555, "rain", "555");
使用JdbcTemplate对象进行删除操作:
jdbcTemplate.update("DELETE FROM sys_client_userinfo WHERE id = ?", 444);
使用JdbcTemplate对象进行修改操作:
jdbcTemplate.update("UPDATE sys_client_userinfo set password = ? WHERE id = ?", "444", 444);
3.2 查询
查询操作:
jdbcTemplate.query(sql, Mapper, params)
jdbcTemplate.queryForObject(sql, Mapper, params)
使用JdbcTemplate对象进行查询操作:
1,全表查询并封装到列表:
List<User> userList = jdbcTemplate.query("SELECT * FROM sys_client_userinfo", new BeanPropertyRowMapper<>(User.class));
2,查询表中的一个对象并封装:
User user = jdbcTemplate.queryForObject("SELECT * FROM sys_client_userinfo WHERE id = ?", new BeanPropertyRowMapper<>(User.class), 111);
3,聚合查询:
Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM sys_client_userinfo", Integer.class);