Spring 學習(二十二)——使用 NamedParameterJdbcTemplate
阿新 • • 發佈:2018-12-25
在 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);
}
}