

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: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.1.xsd">
<!-- 導入資源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<context:component-scan base-package="om.hy.spring.JdbcTemplate">
</context:component-scan>
<!-- 配置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.initPoolSize}"></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>
JDBCTest.java
package om.hy.spring.JdbcTemplate;
import static org.junit.Assert.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
public class JDBCTest {
private ApplicationContext ctx = null;
private JdbcTemplate jdbcTemplate ;
private EmployeeDao employeeDao;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
employeeDao = (EmployeeDao) ctx.getBean(EmployeeDao.class);
namedParameterJdbcTemplate = ctx.getBean(NamedParameterJdbcTemplate.class);
}
/**
* 使用具名參數時, 可以使用 update(String sql, SqlParameterSource paramSource) 方法進行更新操作
* 1. SQL 語句中的參數名和類的屬性一致!
* 2. 使用 SqlParameterSource 的 BeanPropertySqlParameterSource 實現類作為參數.
*/
@Test
public void testNamedParameterJdbcTemplate2(){
String sql = "INSERT INTO employee(lastName, email, dpetId) "
+ "VALUES(:lastName,:email,:dpetId)";
Employee employee = new Employee();
employee.setLastName("XYZ");
employee.setEmail("xyz@sina.com");
employee.setDpetId(3);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(employee);
namedParameterJdbcTemplate.update(sql, paramSource);
}
/**
* 可以為參數起名字.
* 1. 好處: 若有多個參數, 則不用再去對應位置, 直接對應參數名, 便於維護
* 2. 缺點: 較為麻煩.
*/
@Test
public void testNamedParameterJdbcTemplate(){
String sql = "INSERT INTO employee(lastName, email, dpetId) VALUES(:ln,:email,:deptid)";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("ln", "FF");
paramMap.put("email", "ff@atguigu.com");
paramMap.put("deptid", 2);
namedParameterJdbcTemplate.update(sql, paramMap);
}
@Test
public void testEmployeeDao(){
System.out.println(employeeDao.getEmployee(1));
}
@Test
public void testQueryForObject2(){
String sql = "select count(*) from employee ";
long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
}
@Test
public void testQueryForList(){
String sql = "select * from employee where id > ?";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
List<Employee> employees = jdbcTemplate.query(sql, rowMapper, 2);
System.out.println(employees);
}
/**
* 從數據庫中獲取一條記錄, 實際得到對應的一個對象
* 注意不是調用 queryForObject(String sql, Class<Employee> requiredType, Object... args) 方法!
* 而需要調用 queryForObject(String sql, RowMapper<Employee> rowMapper, Object... args)
* 1. 其中的 RowMapper 指定如何去映射結果集的行, 常用的實現類為 BeanPropertyRowMapper
* 2. 使用 SQL 中列的別名完成列名和類的屬性名的映射. 例如 last_name lastName
* 3. 不支持級聯屬性. JdbcTemplate 到底是一個 JDBC 的小工具, 而不是 ORM 框架
*/
@Test
public void testQueryForObject(){
String sql = "select * from employee where id = ?";
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);
System.out.println(employee);
}
/**
* 執行批量更新: 批量的 INSERT, UPDATE, DELETE
* 最后一個參數是 Object[] 的 List 類型: 因為修改一條記錄需要一個 Object 的數組, 那么多條不就需要多個 Object 的數組嗎
*/
@Test
public void testBatchInsert(){
String sql = "Insert into employee (lastName, email, dpetId) "
+ "values(?,?,?)";
List<Object[]> batchArgs = new ArrayList<Object[]>();
batchArgs.add(new Object[]{"AA", "aa@qq.com", 1});
batchArgs.add(new Object[]{"BB", "bb@qq.com", 1});
batchArgs.add(new Object[]{"CC", "cc@qq.com", 1});
batchArgs.add(new Object[]{"DD", "dd@qq.com", 1});
jdbcTemplate.batchUpdate(sql, batchArgs);
}
/**
* 執行 INSERT, UPDATE, DELETE
*/
@Test
public void testInsert(){
String sql = "Insert into employee (lastName, email, dpetId) "
+ "values(?,?,?)";
jdbcTemplate.update(sql, "TOM","tom@qq.com",1);
}
@Test
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
