您的位置:首页 > 移动开发

使用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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐