package cn.itcast.dao.impl; import cn.itcast.dao.StudentDao; import cn.itcast.domain.Student; import cn.itcast.util.JDBCUtils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; public class StudentDaoImpl implements StudentDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<Student> findAll() { //使用JDBC操作数据库... //1.定义sql String sql = "select * from student"; List<Student> students = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class)); return students; } @Override public void add(Student student) { //1.定义sql String sql = "insert into student values(null,?,?,?,?,?,?)"; //2.执行sql template.update(sql, student.getName(), student.getGender(), student.getAge(), student.getAddress(), student.getQq(), student.getEmail()); } @Override public void delete(int id) { //1.定义sql String sql = "delete from student where id = ?"; //2.执行sql template.update(sql, id); } @Override public Student findById(int id) { String sql = "select * from student where id = ?"; return template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), id); } @Override public void update(Student student) { String sql = "update student set name = ?,gender = ? ,age = ? , address = ? , qq = ?, email = ? where id = ?"; template.update(sql, student.getName(), student.getGender(), student.getAge(), student.getAddress(), student.getQq(), student.getEmail(), student.getId()); } @Override public int findTotalCount(Map<String, String[]> condition) { //1.定义模板初始化sql String sql = "select count(*) from student where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //2.遍历map Set<String> keySet = condition.keySet(); //定义参数的集合 List<Object> params = new ArrayList<Object>(); for (String key : keySet) { //排除分页条件参数 if("currentPage".equals(key) || "rows".equals(key)){ continue; } //获取value String value = condition.get(key)[0]; //判断value是否有值 if(value != null && !"".equals(value)){ //有值 sb.append(" and "+key+" like ? "); params.add("%"+value+"%");//?条件的值 } } System.out.println(sb.toString()); System.out.println(params); return template.queryForObject(sb.toString(),Integer.class,params.toArray()); } @Override public List<Student> findByPage(int start, int rows, Map<String, String[]> condition) { String sql = "select * from student where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //2.遍历map Set<String> keySet = condition.keySet(); //定义参数的集合 List<Object> params = new ArrayList<Object>(); for (String key : keySet) { //排除分页条件参数 if("currentPage".equals(key) || "rows".equals(key)){ continue; } //获取value String value = condition.get(key)[0]; //判断value是否有值 if(value != null && !"".equals(value)){ //有值 sb.append(" and "+key+" like ? "); params.add("%"+value+"%");//?条件的值 } } //添加分页查询 sb.append(" limit ?,? "); //添加分页查询参数值 params.add(start); params.add(rows); sql = sb.toString(); System.out.println(sql); System.out.println(params); return template.query(sql,new BeanPropertyRowMapper<Student>(Student.class),params.toArray()); } }

1273251505 LV1
1月5日
ma406805131 LV19
2024年12月19日
TTThai LV1
2024年12月17日
xiaoaitx LV8
2024年11月19日
微信网友_6520355827929088 LV2
2024年11月18日
krispeng LV14
2024年9月10日
Darchry LV2
2024年6月29日
taoshen95 LV16
2024年6月25日
wanglinddad LV55
2024年3月13日
hyt123456 LV3
2024年2月9日

yangchengshuai LV15
3月7日
微信网友_7289626841501696 LV3
3月6日
docnnxxy688
3月1日
暂无贡献等级
1273251505 LV1
1月5日
经典拿铁
2024年12月29日
暂无贡献等级
3375276400
2024年12月24日
暂无贡献等级
ma406805131 LV19
2024年12月18日
TTThai LV1
2024年12月17日
xiaoaitx LV8
2024年11月19日
微信网友_6520355827929088 LV2
2024年11月18日