1、概念
Spring框架提供的,用于操作数据库的,对JDBC进行了封装的一个工具类
2、开发步骤
⑴ 导入jar包
除了:
commons-logging-1.1.3.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
还需:
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
此外还要:
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.7-bin.jar
⑵ 编写jdbc.properties配置文件,并放到src路径下。里面包括驱动类,数据库url,数据库连接池等配置信息
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///
jdbc.user=root
jdbc.password=???
⑶ 编写核心配置文件
① 首先添加context名称空间,通过
<context:property-placeholder location="classpath:/jdbc.properties" />
来引入外部的配置文件
② 通过bean 标签,来声明DataSource对象
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
③ 通过bean标签, 来声明JdbcTemplate对象
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource" />
</bean>
注意:JdbcTemplate没有无参的构造方法,所以必须通过constructor-arg 标签,来给其注入DataSource对象
public JdbcTemplate(DataSource dataSource) { }
④ 还可以配置【使用具名参数的JdbcTemplate类对象】
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource" />
</bean>
注意:NamedParameterJdbcTemplate没有无参的构造方法,所以必须通过constructor-arg 标签,来给其注入DataSource对象
public NamedParameterJdbcTemplate(DataSource dataSource) { }
⑤ 通过IOC容器获取JdbcTemplate或NameParameterJdbcTemplate对象,并操作数据库
3、JdbcTemplate类的相关方法
update
public int update(String sql, Object… args) throws DataAccessException {}
执行增删改操作。第一个参数为SQL语句;第二个参数为填充占位符的参数
batchUpdate
public int[] batchUpdate(String sql, List<Object[]> batchArgs) {}
批量增删改操作。第一个参数为SQL语句;第二个参数为List集合,泛型为Object类型的数组,即封装了多个填充占位符元素的集合
queryForObject【查询单行记录】
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object… args) throws DataAccessException {}
查询单行记录。第一个参数为SQL语句;第二个参数需要传递RowMapper的实现类BeanPropertyRowMapper;第三个参数为填充占位符的参数
BeanPropertyRowMapper
该类用于将JavaBean的属性和数据表的字段一一映射起来
public BeanPropertyRowMapper(Class<T> mappedClass) {}
构造方法,需要将要封装的JavaBean对象的Class类型传入
query
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object… args) throws DataAccessException {}
查询多行记录。第一个参数为SQL语句;第二个参数需要传递RowMapper【接口】的实现类BeanPropertyRowMapper;第三个参数为填充占位符的参数
queryForObject【查询单个记录】
public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException {}
查询单个记录。第一个参数为SQL语句;第二个参数为返回结果的Class类型
4、NamedParameterJdbcTemplate类的相关方法
update
public int update(String sql, Map<String, ?> paramMap) throws DataAccessException {}
执行增删改操作。第一个参数为SQL语句;第二个参数为Map集合,要求键为String类型,值为任意类型
注意:
⑴ 对SQL语句的要求:占位符不能用问号(?),而是用冒号加字符串来代替(:String)。最好字符串用属性名来表示
⑵ 对Map集合的要求:key就是SQL语句中的占位符去掉冒号(具名),value就是填充占位符的值
update
public int update(String sql, SqlParameterSource paramSource) throws DataAccessException {}
执行增删改操作。第一个参数为SQL语句;第二个参数需要传递SqlParameterSource【接口】的实现类对象BeanPropertySqlParameterSource
注意:
⑴ BeanPropertySqlParameterSource没有无参构造,需要传递一个JavaBean对象,并且各个属性都有值
public BeanPropertySqlParameterSource(Object object) {}
传递带参数的JavaBean对象
⑵ 对SQL语句的要求:占位符不能用问号(?),而是用冒号加字符串来代替(:String)。字符串必须用属性名来表示
否则报错:
org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter '???': Invalid property '???' of bean class [???]: Bean property '???' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?