使用NamedParameterJdbcTemplate 通过BeanPropertyRowMapper 返回一个对象或List 集合

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 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;iparame = 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.class));
}


@Override
public List search(Map 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.class) );
}

@Override
public List> search2(Map 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 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);
}




}

你可能感兴趣的:(spring3.0)