昨天的河北省科技政策查询系统需求项目没有整出来总记录,老师没让通过。
今天又修改了一下
package cn.itcase.dao.impl; import cn.itcase.dao.UserDao; import cn.itcase.domain.User; import cn.itcase.util.JDBCUtils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import javax.xml.soap.Text; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; public class UserDaoImpl implements UserDao { private JdbcTemplate template= new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<User> findAll() { /* * 使用jdbc * */ String sql="select * from policy"; List<User> users=template.query(sql,new BeanPropertyRowMapper<User>(User.class)); return users; } @Override public User findById(int id) { String sql="select * from policy where id=?"; return template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class), id); } @Override public int findTotalCount(Map<String, String[]> condition) { //1.定义模板初始化sql; String sql="select count(*) from policy 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 ").append(key).append(" like ?"); params.add("%"+value+"%");//条件的值 } } System.out.println(sb.toString()); System.out.println(params); return template.queryForObject(sb.toString(),int.class,params.toArray()); } @Override public List<User> findByPage(int start, int rows, Map<String, String[]> condition) { String sql="select * from policy 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 ").append(key).append(" like ?"); params.add("%"+value+"%");//条件的值 } } //添加分页查询 sb.append(" limit ?,? "); //添加分页查询参数 params.add(start); params.add(rows); return template.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray()); } // @Override // public User findUserByUsernameAndPassword(String username, String password) { // try { // String sql="select * from user where username=? and password=?"; // User user=template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class)),username,password; // return user; // }catch (Exception e){ // e.printStackTrace(); // return null; // } // // // } }
package cn.itcase.domain; import java.util.List; /* * 分页工具对象 * */ public class PageBean<T>{ private int totalCount;//总记录数 private int totalPage; //总页码 private List <T> list; //每页的数据 private int currentPage;//当前页码 private int rows;//每页显示的记录数 public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } @Override public String toString() { return "PageBean{" + "totalCount=" + totalCount + ", totalPage=" + totalPage + ", list=" + list + ", currentPage=" + currentPage + ", rows=" + rows + '}'; } public PageBean(int totalCount, int totalPage, List<T> list, int currentPage, int rows) { this.totalCount = totalCount; this.totalPage = totalPage; this.list = list; this.currentPage = currentPage; this.rows = rows; } public PageBean() { } }
标签:4.11,总结,rows,return,String,int,每日,currentPage,public From: https://www.cnblogs.com/louwangshayu/p/17307914.html