使用NamedParameterJdbcTemplate 通过BeanPropertyRowMapper 返回一个对象或List 集合
2014-04-01 21:45
881 查看
package com.tlz.app.core.staff.dao.impl;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.tlz.app.core.staff.dao.StaffDao;
import com.tlz.app.core.staff.entity.Staff;
@Repository
public class StaffDaoImpl implements StaffDao {
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
@Override
public int update(Staff staff) {
String sql = "update core_staff set staff_name = :staffName where staff_num = :staffNum";
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(staff);
return jdbcTemplate.update(sql, paramSource);
}
@Override
public int updateBatch(List<Serializable> ids) {
String sql = "update core_staff set staff_isLocked=1 where staff_id = :staffId";
SqlParameterSource[] sources = new MapSqlParameterSource[ids.size()];
MapSqlParameterSource parame;
for(int i=0;i<ids.size();i++){
parame = new MapSqlParameterSource();
parame.addValue("staffId", ids.get(i));
sources[i] = parame;
}
int[] result = jdbcTemplate.batchUpdate(sql, sources);
return result.length;
}
@Override
public Staff getStaffById(Serializable id) {
String sql = "select * from core_staff where staff_id=:staffId";
MapSqlParameterSource parame = new MapSqlParameterSource();
parame.addValue("staffId", id);
return jdbcTemplate.queryForObject(sql, parame, new BeanPropertyRowMapper<Staff>(Staff.class));
}
@Override
public List<Staff> search(Map<String, Object> condition) {
StringBuffer sql = new StringBuffer();
MapSqlParameterSource parame = new MapSqlParameterSource();
sql.append("select * from core_staff where 1 = 1 ");
if(condition.containsKey("staffNum")){
sql.append(" and staff_num like '%'||:staffNum||'%'");
parame.addValue("staffNum", condition.get("staffNum"));
}
if(condition.containsKey("staffIsLocked")){
sql.append(" and staff_isLocked = :staffIsLocked");
parame.addValue("staffIsLocked", condition.get("staffIsLocked"));
}
return jdbcTemplate.query(sql.toString(), parame,new BeanPropertyRowMapper<Staff>(Staff.class) );
}
@Override
public List<Map<String,Object>> search2(Map<String, Object> condition) {
StringBuffer sql = new StringBuffer();
MapSqlParameterSource parame = new MapSqlParameterSource();
sql.append("select * from core_staff where 1 = 1 ");
if(condition.containsKey("staffNum")){
sql.append(" and staff_num like '%'||:staffNum||'%'");
parame.addValue("staffNum", condition.get("staffNum"));
}
if(condition.containsKey("staffIsLocked")){
sql.append(" and staff_isLocked = :staffIsLocked");
parame.addValue("staffIsLocked", condition.get("staffIsLocked"));
}
return jdbcTemplate.queryForList(sql.toString(), parame);
}
@Override
public int getCount(Map<String, Object> condition) {
StringBuffer sql = new StringBuffer();
MapSqlParameterSource parame = new MapSqlParameterSource();
sql.append("select count(1) from core_staff where 1 = 1 ");
if(condition.containsKey("staffNum")){
sql.append(" and staff_num like :staffNum");
parame.addValue("staffNum", "%"+condition.get("staffNum")+"%");
}
if(condition.containsKey("staffIsLocked")){
sql.append(" and staff_isLocked = :staffIsLocked");
parame.addValue("staffIsLocked", condition.get("staffIsLocked"));
}
return jdbcTemplate.queryForObject(sql.toString(), parame,Integer.class);
}
}
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.tlz.app.core.staff.dao.StaffDao;
import com.tlz.app.core.staff.entity.Staff;
@Repository
public class StaffDaoImpl implements StaffDao {
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
@Override
public int update(Staff staff) {
String sql = "update core_staff set staff_name = :staffName where staff_num = :staffNum";
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(staff);
return jdbcTemplate.update(sql, paramSource);
}
@Override
public int updateBatch(List<Serializable> ids) {
String sql = "update core_staff set staff_isLocked=1 where staff_id = :staffId";
SqlParameterSource[] sources = new MapSqlParameterSource[ids.size()];
MapSqlParameterSource parame;
for(int i=0;i<ids.size();i++){
parame = new MapSqlParameterSource();
parame.addValue("staffId", ids.get(i));
sources[i] = parame;
}
int[] result = jdbcTemplate.batchUpdate(sql, sources);
return result.length;
}
@Override
public Staff getStaffById(Serializable id) {
String sql = "select * from core_staff where staff_id=:staffId";
MapSqlParameterSource parame = new MapSqlParameterSource();
parame.addValue("staffId", id);
return jdbcTemplate.queryForObject(sql, parame, new BeanPropertyRowMapper<Staff>(Staff.class));
}
@Override
public List<Staff> search(Map<String, Object> condition) {
StringBuffer sql = new StringBuffer();
MapSqlParameterSource parame = new MapSqlParameterSource();
sql.append("select * from core_staff where 1 = 1 ");
if(condition.containsKey("staffNum")){
sql.append(" and staff_num like '%'||:staffNum||'%'");
parame.addValue("staffNum", condition.get("staffNum"));
}
if(condition.containsKey("staffIsLocked")){
sql.append(" and staff_isLocked = :staffIsLocked");
parame.addValue("staffIsLocked", condition.get("staffIsLocked"));
}
return jdbcTemplate.query(sql.toString(), parame,new BeanPropertyRowMapper<Staff>(Staff.class) );
}
@Override
public List<Map<String,Object>> search2(Map<String, Object> condition) {
StringBuffer sql = new StringBuffer();
MapSqlParameterSource parame = new MapSqlParameterSource();
sql.append("select * from core_staff where 1 = 1 ");
if(condition.containsKey("staffNum")){
sql.append(" and staff_num like '%'||:staffNum||'%'");
parame.addValue("staffNum", condition.get("staffNum"));
}
if(condition.containsKey("staffIsLocked")){
sql.append(" and staff_isLocked = :staffIsLocked");
parame.addValue("staffIsLocked", condition.get("staffIsLocked"));
}
return jdbcTemplate.queryForList(sql.toString(), parame);
}
@Override
public int getCount(Map<String, Object> condition) {
StringBuffer sql = new StringBuffer();
MapSqlParameterSource parame = new MapSqlParameterSource();
sql.append("select count(1) from core_staff where 1 = 1 ");
if(condition.containsKey("staffNum")){
sql.append(" and staff_num like :staffNum");
parame.addValue("staffNum", "%"+condition.get("staffNum")+"%");
}
if(condition.containsKey("staffIsLocked")){
sql.append(" and staff_isLocked = :staffIsLocked");
parame.addValue("staffIsLocked", condition.get("staffIsLocked"));
}
return jdbcTemplate.queryForObject(sql.toString(), parame,Integer.class);
}
}
相关文章推荐
- spring JdbcTemplate 查询,使用BeanPropertyRowMapper
- 前台使用ajax 调用action方法,返回一个对象List集合的json
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- 使用Spring的JdbcTemplate和BeanPropertyRowMapper完成的JDBC
- 关于ASP.NET下,JQuery+AJAX使用JSON返回对象集合List数据的总结
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合
- BeanPropertyRowMapper使用注意事项
- spring JdbcTemplate 查询,使用BeanPropertyRowMapper
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合
- 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
- 通过jdbcTemplate如何返回自定义的对象集合,以及在java中如何遍历这个集合
- 通过反射从数据库返回集合ResultSet中得到实体对象的list集合
- Hibernate查询之SQL查询,查询结果用new新对象的方式接受,hql查询,通过SQL查询的结果返回到一个实体中,查询不同表中内容,并将查到的不同表中的内容放到List中
- 阿里巴巴 Java 开发手册:asList 的返回对象是一个 Arrays 内部类,并没有实现集合的修改方法
- List集合中删除属性(一个或者多个属性)相同的对象返回List且根据对象属性进行排序输出
- Hibernate查询之SQL查询,查询结果用new新对象的方式接受,hql查询,通过SQL查询的结果返回到一个实体中,查询不同表中内容,并将查到的不同表中的内容放到List中
- Java工具类 通过ResultSet对象返回对应的实体List集合
- DBUtils 的使用 之 ResultSetHandler的实现 类;ArrayListHandler: 将多条记录封装到一个装有Object[]的List集合中。BeanHandler:(常用)将
- spring JdbcTemplate 查询,使用BeanPropertyRowMapper
- BeanPropertyRowMapper使用注意事项