在 JDBC 模板中使用具名参数
•在 SQL 语句中使用具名参数时, 可以在一个 Map 中提供参数值, 参数名为键
•也可以使用 SqlParameterSource 参数
•批量更新时可以提供 Map 或 SqlParameterSource 的数组
代码示例:
1、数据库表的建立如下:
employee表
department表
2、目录结构
Spring
|——src
|——|——com.hzyc.spring.jdbc
|——|——|——employee.java
|——|——|——JdbcTest.java
|——|——application-context.xml
|——|——db.properties
2、db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring
jdbc.user=root
jdbc.password=root
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
3、application-context.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: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-4.0.xsd">
<context:component-scan base-package="com.hzyc.spring"/>
<!--导入资源文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置 c3p0 数据源-->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
</bean>
<!--配置 Spring 的 Jdbc Template-->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--
配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数(具有名字)
其没有无参的构造器,必须为其构造器指定参数
-->
<bean id="namedParameterJdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
</beans>
employee.java
package com.hzyc.spring.jdbc;
/**
* @author xuehj2016
* @Title: Employee
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/21 14:41
*/
public class Employee {
private int id;
private String name;
private String email;
private int deptId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", deptId=" + deptId +
'}';
}
}
JdbcTest.java
package com.hzyc.spring.jdbc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import java.util.HashMap;
import java.util.Map;
/**
* @author xuehj2016
* @Title: JdbcTest
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/21 14:02
*/
public class JdbcTest {
private ApplicationContext applicationContext;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
{
applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
namedParameterJdbcTemplate = (NamedParameterJdbcTemplate)
applicationContext.getBean("namedParameterJdbcTemplate");
}
/**
* 使用具名参数时, 可以使用 update(String sql, SqlParameterSource paramSource) 方法进行更新操作
* 1. SQL 语句中的参数名和类的属性一致!
* 2. 使用 SqlParameterSource 的 BeanPropertySqlParameterSource 实现类作为参数.
*/
@Test
public void testNamedParameterJdbcTemplate2() {
String sql = "insert into employee(name, email, dept_id) " +
"values(:name, :email, :deptId)";
Employee employee = new Employee();
employee.setName("薛恒杰");
employee.setEmail("[email protected]");
employee.setDeptId(2);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(employee);
int row = namedParameterJdbcTemplate.update(sql, paramSource);
System.out.println(row);
}
/**
* 可以为参数起名字.
* 1. 好处: 若有多个参数, 则不用再去对应位置, 直接对应参数名, 便于维护
* 2. 缺点: 较为麻烦.
*/
@Test
public void testNamedParameterJdbcTemplate() {
String sql = "insert into employee(name, email, dept_id) " +
"values(:name, :email, :id)";
Map<String, Object> paramMap = new HashMap<>(10);
paramMap.put("name", "薛恒杰");
paramMap.put("email", "[email protected]");
paramMap.put("id", 3);
int row = namedParameterJdbcTemplate.update(sql, paramMap);
System.out.println(row);
}
}