package com.hopetesting.dao.impl;import com.hopetesting.dao.UserDao;import com.hopetesting.domain.User;import com.hopetesting.utils.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;/** * @author newcityman * @date 2019/9/7 - 21:07 */public class UserDaoImpl implements UserDao { private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User> findAllUser() { String sql = "select * from user"; List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class)); return users; } @Override public User login(User user) { String sql = "select * from user where username=? and password=? "; User loginUser = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), user.getUsername(), user.getPassword()); return loginUser; } @Override public void add(User user) { String sql = "insert into user values(null,?,?,?,?,?,?,null,null)"; jdbcTemplate.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail()); } @Override public void delUser(int id) { String sql = "delete from user where id=?"; jdbcTemplate.update(sql, id); } @Override public User findUserById(int id) { String sql ="select * from user where id= ?"; return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),id); } @Override public void updateUserById(User user) { String sql="update user set name=?, gender=?, age=?, address=?, qq=?, email=? where id=?"; jdbcTemplate.update(sql,user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(),user.getId()); } @Override public int findTotalCount(Map<String, String[]> condition) { //1、定义一个sql模板 String sql="select count(*) from user where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //2、遍历map,拼装sql语句 Set<String> keySet = condition.keySet(); //定义一个参数的集合 List<Object> parameters = new ArrayList<Object>(); for (String key : keySet) { //获取value if("currentPage".equals(key) ||"rows".equals(key)){ continue; } String value = condition.get(key)[0]; if(value!=null && !"".equals(value)){ sb.append(" and "+key+" like ? "); parameters.add("%"+value+"%"); //?条件的值 } } /* System.out.println(sb.toString()); System.out.println(parameters);*/ return jdbcTemplate.queryForObject(sb.toString(),Integer.class,parameters.toArray()); } @Override public List<User> findByPage(int startIndex, int rows, Map<String, String[]> condition) { //1、定义一个末班初始化sql String sql="select * from user where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //2、遍历map Set<String> keySet = condition.keySet(); //3、定义参数的集合 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(startIndex); params.add(rows); /* System.out.println(sb); System.out.println(params);*/ return jdbcTemplate.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray()); }}
来源:博客园
作者:小猪_佩奇
链接:https://www.cnblogs.com/newcityboy/p/11509635.html