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




}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值