Spring_使用 NamedParameterJdbcTemplate

 

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());
}

 

}

 

 

posted @ 2016-08-31 18:50  疯狂的tiger  阅读(5245)  评论(0编辑  收藏  举报