Spring中NamedParameterJdbcTemplate详解

来源:互联网 发布:实时数据库软件有哪些 编辑:程序博客网 时间:2024/06/06 12:57
Spring中NamedParameterJdbcTemplate详解

一、说在前面

1、在经典的 JDBC 用法中,SQL 参数是用占位符 ? 表示,并且受到位置的限制。定位参数的问题在于,一旦参数的顺序发生变化,就必须改变参数绑定。
2、在 Spring JDBC 框架中,绑定 SQL 参数的另一种选择是使用具名参数(named parameter)。
3、具名参数: SQL 按名称(以冒号开头)而不是按位置进行指定,具名参数更易于维护, 也提升了可读性。

4、具名参数只在 NamedParameterJdbcTemplate 中得到支持。


二、实例代码如下:

1、数据库表和上一篇中一模一样,在此不再赘述。
2、applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.at.jdbc"></context:component-scan><!-- 导入资源文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 配置C3P0数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="driverClass" value="${jdbc.driverClass}"></property><property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property></bean><!-- 配置Spring的JdbcTemplate模板类 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置 NamedParameterJdbcTemplate ,该对象可以使用具名参数,其没有无参构造器,所以必须为其构造器指定参数 --><bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"><constructor-arg ref="dataSource"></constructor-arg></bean></beans>
3、实现方法一,使用update(String sql, Map<String, ?> paramMap) 
/** * 具名参数,可以为参数起名字 * 1、好处:若有多个参数,则不用再去对应位置,直接去对应参数名,便于维护 * 2、缺点:较为麻烦 */@Testpublic void testNamedParameterJdbcTemplate(){String sql = "INSERT INTO employee(LAST_NAME,EMAIL,DEPT_ID) VALUES (:ln,:email,:deptid)";Map<String, Object> paramMap = new HashMap<String, Object>();paramMap.put("ln", "HH");paramMap.put("email", "hh@163.com");paramMap.put("deptid", 2);namedParameterJdbcTemplate.update(sql, paramMap);}
4、实现方法二,使用update(String sql, SqlParameterSource paramSource)
/** * 使用具名参数时候,可以使用update(String sql, SqlParameterSource paramSource) 方法来进行更新 * 1、SQL语句中的参数名和类的属性一致 * 2、使用SqlParameterSource的BeanPropertySqlParameterSource实现来作为参数。 */@Testpublic void testNamedParameterJdbcTemplate2(){String sql = "INSERT INTO employee(LAST_NAME,EMAIL) VALUES (:lastName,:email)";Employee employee = new Employee();employee.setLastName("KK");employee.setEmail("kk@163.com");SqlParameterSource paramSource = new BeanPropertySqlParameterSource(employee);namedParameterJdbcTemplate.update(sql, paramSource);}

By luyepiaoxue2014
微博地址: http://weibo.com/luoyepiaoxue2014 点击打开链接



原创粉丝点击