除了标准的JdbcTemplate外,Spring还提供了两个易用的JDBC模板类
在低版本的Spring 中, 用户只能使用“?”占位符声明参数,并使用索引号绑定参数,必须要保证参数的索引号和SQL语句中的占位符“?”的位置正确匹配。
NamedParameterJdbcTemplate模板了支持命名参数变量的SQL,位于org.springframework.jdbc.namedparam包中,该包中还定义了一个用于承载命名参数的SqlParameterSource接口
BeanPropertySqlParameterSource:该实现类是将一个JavaBean对像封装成一个参数源,以便通过JavaBean属性名和SQL语句中的命名参数匹配的方式绑定参数
MapSqlparameterSource:该实现类内部通过一个Map存储参数,可以通过addValue(String paramName , Object value) 或 addValue(Map value)添加参数,并通过参数键名和SQL语句的命名参数的方式绑定参数。
package com.xgj.dao.namedParameterJdbcTemplate.dao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;import org.springframework.jdbc.core.namedparam.SqlParameterSource;import org.springframework.stereotype.Repository;import com.xgj.dao.namedParameterJdbcTemplate.domain.Artisan;/** * * * @ClassName: ArtisanNJDaoImpl * * @Description: @Repository标注的DAO层,受Spring管理 * * @author: Mr.Yang * * @date: 2017年9月30日 上午12:42:26 */@RepositorypublicclassArtisanNJDaoImplimplementsArtisanNJDao {private NamedParameterJdbcTemplate namedParameterJdbcTemplate;privatefinalstatic String insertArtisanSql ="insert into artisan(artisan_name) values(:artisanName)";/** * * * @Title: setNamedParameterJdbcTemplate * * @Description: 自动注入namedParameterJdbcTemplate * * @param namedParameterJdbcTemplate * * @return: void */@AutowiredpublicvoidsetNamedParameterJdbcTemplate( NamedParameterJdbcTemplate namedParameterJdbcTemplate) {this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; }@OverridepublicvoidaddArtisan(Artisan artisan) {// 定义命名参数 SqlParameterSource sps =new BeanPropertySqlParameterSource(artisan);// 使用模板类方法 namedParameterJdbcTemplate.update(insertArtisanSql, sps); } }
在SQL语句中声明命名参数的格式为
:paranName
比如values(:artisanName) ,多个参数使用逗号分隔。
在这个示例中,使用BeanPropertySqlParameterSource提供数据源,它接收一个JavaBean作为构造函数的入参,调用namedParameterJdbcTemplate.update(insertArtisanSql, sps)执行插入数据的而操作。
配置文件
<?xml version="1.0" encoding="UTF-8" ?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"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"><!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --><context:component-scanbase-package="com.xgj.dao.namedParameterJdbcTemplate" /><!-- 使用context命名空间 配置数据库的properties文件 --><context:property-placeholderlocation="classpath:spring/jdbc.properties" /><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"p:driverClassName="${jdbc.driverClassName}"p:url="${jdbc.url}"p:username="${jdbc.username}"p:password="${jdbc.password}" /><!-- 定义 namedParameterJdbcTemplate--><beanid="namedParameterJdbcTemplate"class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"><constructor-argref="dataSource"/></bean></beans>
注意NamedParameterJdbcTemplate的配置,使用构造函数。
Domain
package com.xgj.dao.namedParameterJdbcTemplate.domain;import java.io.Serializable;publicclassArtisanimplementsSerializable {privatestaticfinallong serialVersionUID =1L;private String artisanId;private String artisanName;public StringgetArtisanId() {return artisanId; }publicvoidsetArtisanId(String artisanId) {this.artisanId = artisanId; }public StringgetArtisanName() {return artisanName; }publicvoidsetArtisanName(String artisanName) {this.artisanName = artisanName; } }
Artisan拥有两个属性,我们这里没有插入ID,暂且忽略。其中 artisanName 这个属性和 SQL语句中的命名参数匹配,参数即按照这个匹配关系进行绑定。
单元测试
package com.xgj.dao.namedParameterJdbcTemplate.dao;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.xgj.dao.namedParameterJdbcTemplate.domain.Artisan;publicclassArtisanNJDaoImplTest { ClassPathXmlApplicationContext ctx =null; ArtisanNJDaoImpl artisanNJDaoImpl =null;@BeforepublicvoidinitContext() {// 启动Spring 容器 ctx =new ClassPathXmlApplicationContext("classpath:com/xgj/dao/namedParameterJdbcTemplate/conf_namedParameterJdbcTemplate.xml"); artisanNJDaoImpl = ctx.getBean("artisanNJDaoImpl", ArtisanNJDaoImpl.class); System.out.println("initContext successfully"); }@TestpublicvoidqueryTeacherById() { Artisan artisan =new Artisan(); artisan.setArtisanName("ArtisanNJ"); artisanNJDaoImpl.addArtisan(artisan); }@AfterpublicvoidcloseContext() {if (ctx !=null) { ctx.close(); } System.out.println("close context successfully"); } }
结果:
查看下数据库是否插入成功 (只是演示,忽略ID…)
如果有数据表记录没有对应的领域对象,则用户可以直接使用MapSqlparameterSource达到绑定参数的目的。
publicvoidaddArtisanWithMapSqlParameterSource(Artisan artisan) {// 使用MapSqlParameterSource绑定参数 MapSqlParameterSource mapSqlParameterSource =new MapSqlParameterSource() .addValue("artisanName", artisan.getArtisanName());// 使用模板类方法 namedParameterJdbcTemplate.update(insertArtisanSql, mapSqlParameterSource); }
由于MapSqlParameterSource中的大多数方法都能返回对象本身,所以可以将几个参数的调用串成一个链,假设Artisan还有个artisanSex属性,如下
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource().addValue("artisanName", artisan.getArtisanName()).addValue("artisanSex",artisan.getArtisanSex);
由于这个原因,使用方法调用链模式设计的API很容易使用。
单元测试
@TestpublicvoidqueryTeacherById() {// Artisan artisan = new Artisan();// artisan.setArtisanName("ArtisanNJ");// artisanNJDaoImpl.addArtisan(artisan); Artisan artisan =new Artisan(); artisan.setArtisanName("ArtisanMS"); artisanNJDaoImpl.addArtisanWithMapSqlParameterSource(artisan); }
数据库结果
如果我们想查找artisan_id在 1 ,3 , 5 中的数据, PrepareStatement对in的操作只能动态拼接
Stringin_data ="1,3,5"; pst = conn.prepareStatement("select artisan_name from artisan where artisan_id in (?)"); pst.setString(1,in_data);
使用传统的prepareStatement是动态设定参数的,也就是生成 select artisan_name from artisan where artisan_id in (?) ,一个 ? 代表一个参数,pst.setString(1,”1,3,5”) 就相当于 select artisan_name from artisan where artisan_id in (“1,3,5”) ,这个SQL是查找artisan_id为 “1,3,5”的记录,而不是在 1,3,5中记录。
NamedParameterJdbcTemplate可以很好地解决上述问题呢。
....privatefinalstatic String selectArtisanByIds ="select artisan_name from artisan where artisan_id in (:artisanId)"; ....public List<Artisan>getArtisanByIds(List<String> artisanIds) {final List<Artisan> artisanList =new ArrayList<Artisan>();// 使用MapSqlParameterSource绑定参数 MapSqlParameterSource mapSqlParameterSource =new MapSqlParameterSource(); mapSqlParameterSource.addValue("artisanId", artisanIds); namedParameterJdbcTemplate.query(selectArtisanByIds, mapSqlParameterSource,new RowCallbackHandler() {@OverridepublicvoidprocessRow(ResultSet rs)throws SQLException { Artisan artisan =new Artisan(); artisan.setArtisanName(rs.getString("artisan_name"));// 加入集合 artisanList.add(artisan); } });return artisanList; }
单元测试
@TestpublicvoidqueryTeacherById() { List<String> artisanIds =new ArrayList<String>(); artisanIds.add("1"); artisanIds.add("3"); artisanIds.add("5"); List<Artisan> artisans = artisanNJDaoImpl.getArtisanByIds(artisanIds);for (Artisan artisan : artisans) { System.out.println("artisanName:" + artisan.getArtisanName()); } }
输出:
2017-09-30 02:01:27,648INFO[main] (AbstractApplicationContext.java:583)-Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@5f0ab5d: startup date [Sat Sep3002:01:27 BOT2017];rootofcontexthierarchy 2017-09-30 02:01:27,777INFO[main] (XmlBeanDefinitionReader.java:317)-LoadingXMLbeandefinitionsfromclasspathresource[com/xgj/dao/namedParameterJdbcTemplate/conf_namedParameterJdbcTemplate.xml]initContextsuccessfullyartisanName:Xiao2artisanName:Xiao0artisanName:Xiao4 2017-09-30 02:01:30,387INFO[main] (AbstractApplicationContext.java:984)-Closingorg.springframework.context.support.ClassPathXmlApplicationContext@5f0ab5d: startup date [Sat Sep3002:01:27 BOT2017];rootofcontexthierarchyclosecontextsuccessfully
代码已托管到Github—>https://github.com/yangshangwei/SpringMaster