NamedParameterJdbcTemple与RowMapper实现

NamedParameterJdbcTemplate和JdbcTemplate功能基本差不多。

 1、配置

db.properties

1 jdbc.user=root
2 jdbc.password=123456
3 jdbc.driverClass=com.mysql.jdbc.Driver
4 jdbc.jdbcUrl=jdbc\:mysql\:///test

 

applicationContext.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 9 
10     <context:property-placeholder location="classpath:db.properties"/>
11     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
12         <property name="user" value="${jdbc.user}"></property>
13         <property name="password" value="${jdbc.password}"></property>
14         <property name="driverClass" value="${jdbc.driverClass}"></property>
15         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
16     </bean>
17     
18     <!-- NamedParameterJdbcTemplate有一个带有DataSource的构造器 -->
19     <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
20         <constructor-arg ref="dataSource"></constructor-arg>
21     </bean>
22 </beans>
复制代码

 

2、RowMapper实现对象关系映射查询

public class UserDaoImpl implements UserDao{
    
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	private RowMapper<User> userRowMapper = new RowMapper<User>(){
		
		@Override
		public User mapRow(ResultSet rs,int rowNum) throws SQLException{
			User user = new User();
			user.setId = rs.getString("id");
			user.setUserName = rs.getString("userName");
			user.setPassword = rs.getString("password");
			return user;
		}
		
	}
	
	public List<User> getUserList(String userName){
		String sql = "SELECT * FROM t_user where userName = :userName";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userName",userName);
		return namedParameterJdbcTemplate.query(sql,map,userRowMapper);
	}
	

}

3、实现更新与对象设置属性

Java代码

复制代码
 1 //启动IoC容器
 2 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
 3 
 4 NamedParameterJdbcTemplate namedParameterJdbcTemplate=ctx.getBean(NamedParameterJdbcTemplate.class);
 5 //为变量名称前面加上冒号
 6 String sql="insert into user (name,deptid) values (:name,:deptid)";
 7 //定义map集合,其参数名称为sql语句中变量的名称
 8 Map<String,Object> paramMap=new HashMap<String,Object>();
 9 paramMap.put("name", "caoyc");
10 paramMap.put("deptid", 2);
11 namedParameterJdbcTemplate.update(sql, paramMap);
复制代码

方式二:

复制代码
 1 //启动IoC容器
 2 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
 3 
 4 NamedParameterJdbcTemplate namedParameterJdbcTemplate=ctx.getBean(NamedParameterJdbcTemplate.class);
 5 //为变量名称前面加上冒号
 6 String sql="insert into user (name,deptid) values (:name,:deptid)";
 7 //定义个实体类
 8 User user=new User();
 9 user.setName("zhh");
10 user.setDeptid(3);
11 
12 SqlParameterSource paramSource=new BeanPropertySqlParameterSource(user);
13 namedParameterJdbcTemplate.update(sql, paramSource);
复制代码

 

Reference:

[1] 追风-, Spring 中的 RowMapper, http://blog.csdn.net/yajunren/article/details/10103035
[2] HappyBKs, Spring框架笔记(二十五)——NamedParameterJdbcTemplate与具名参数, https://my.oschina.net/happyBKs/blog/497798
[3] JustDo, Spring NamedParameterJdbcTemplate详解, http://www.cnblogs.com/caoyc/p/5630957.html

posted @ 2017-08-09 14:32  ryelqy  阅读(572)  评论(0编辑  收藏  举报