快速学习-JdbcTemplate

第7章 JdbcTemplate

7.1 概述

为了使JDBC更加易于使用,Spring在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架。
作为Spring JDBC框架的核心,JDBC模板的设计目的是为不同类型的JDBC操作提供模板方法,通过这种方式,可以在尽可能保留灵活性的情况下,将数据库存取的工作量降到最低。
可以将Spring的JdbcTemplate看作是一个小型的轻量级持久化层框架,和我们之前使用过的DBUtils风格非常接近。

7.2 环境准备

7.2.1导入JAR包

  1. IOC容器所需要的JAR包
    commons-logging-1.1.1.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
  2. JdbcTemplate所需要的JAR包
    spring-jdbc-4.0.0.RELEASE.jar
    spring-orm-4.0.0.RELEASE.jar
    spring-tx-4.0.0.RELEASE.jar
  3. 数据库驱动和数据源
    c3p0-0.9.1.2.jar
    mysql-connector-java-5.1.7-bin.jar

7.2.2 创建连接数据库基本信息属性文件

user=root
password=root
jdbcUrl=jdbc:mysql:///query_data
driverClass=com.mysql.jdbc.Driver

initialPoolSize=30
minPoolSize=10
maxPoolSize=100
acquireIncrement=5
maxStatements=1000
maxStatementsPerConnection=10

7.2.3 在Spring配置文件中配置相关的bean

  1. 数据源对象
<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${user}"/>
	<property name="password" value="${password}"/>
	<property name="jdbcUrl" value="${jdbcUrl}"/>
	<property name="driverClass" value="${driverClass}"/>
	<property name="initialPoolSize" value="${initialPoolSize}"/>
	<property name="minPoolSize" value="${minPoolSize}"/>
	<property name="maxPoolSize" value="${maxPoolSize}"/>
	<property name="acquireIncrement" value="${acquireIncrement}"/>
	<property name="maxStatements" value="${maxStatements}"/>
<property name="maxStatementsPerConnection" 
value="${maxStatementsPerConnection}"/>
</bean>
  1. JdbcTemplate对象
<bean id="template" 
class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"/>
</bean>

7.3 持久化操作

  1. 增删改
    JdbcTemplate.update(String, Object…)
  2. 批量增删改
    JdbcTemplate.batchUpdate(String, List<Object[]>)
    Object[]封装了SQL语句每一次执行时所需要的参数
    List集合封装了SQL语句多次执行时的所有参数
  3. 查询单行
    JdbcTemplate.queryForObject(String, RowMapper, Object…)
    在这里插入图片描述
  4. 查询多行
    JdbcTemplate.query(String, RowMapper, Object…)
    RowMapper对象依然可以使用BeanPropertyRowMapper
  5. 查询单一值
    JdbcTemplate.queryForObject(String, Class, Object…)

7.4 使用具名参数的JdbcTemplate

  1. 关于具名参数
    在Hibernate的HQL查询中我们体验过具名参数的使用,相对于基于位置的参数,具名参数具有更好的可维护性,在SQL语句中参数较多时可以考虑使用具名参数。
    在Spring中可以通过NamedParameterJdbcTemplate类的对象使用带有具名参数的SQL语句。
  2. 通过IOC容器创建NamedParameterJdbcTemplate对象
<!-- 配置可以使用具名参数的JDBCTemplate类对象 -->
<bean 
	id="namedTemplate" 
	class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
	<!-- 没有无参构造器,必须传入数据源或JdbcTemplate对象 -->
	<constructor-arg ref="dataSource"/>
</bean>
  1. 具名参数在SQL语句中的格式
INSERT INTO depts (dept_name) VALUES (:deptName)
  1. 具名参数传入
    ① 通过Map对象传入
    NamedParameterJdbcTemplate.update(String sql, Map<String, ?> map)
    Map的键是参数名,值是参数值
    ② 通过SqlParameterSource对象传入

在这里插入图片描述

String sql = "INSERT INTO depts (dept_name) VALUES (:deptName)";
Department department = new Department(null, "YYY", null);
SqlParameterSource sqlParameterSource = 
		new BeanPropertySqlParameterSource(department);
namedTemplate.update(sql, sqlParameterSource);

7.5 使用JdbcTemplate实现Dao

  1. 通过IOC容器自动注入
    JdbcTemplate类是线程安全的,所以可以在IOC容器中声明它的单个实例,并将这个实例注入到所有的Dao实例中。
@Repository
public class EmployeeDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public Employee get(Integer id){
		//…
	}
}
发布了1356 篇原创文章 · 获赞 1124 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/104275517