复杂条件查询功能分析
总记录数统计的代码实现
UserDao接口:
/** * 查询总记录数 * @return * @param condition */ int FindTotalCount(Map<String, String[]> condition); /** * 分页查询每页记录 * @param start * @param rows * @param condition * @return */ List<User> fingByPage(int start, int rows, Map<String, String[]> condition);
UserDaoImpl实现类:
@Override public int FindTotalCount(Map<String, String[]> condition) { //定义模板初始化sql String sql = "select count(*) from user where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //遍历map Set<String> keySet = condition.keySet(); //定义参数的集合 List<Object> params = new ArrayList<>(); 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()); }
UserService接口:
/** * 分页条件查询 * @param currentPage * @param rows * @param condition * @return */ PageBean<User> findUserByPage(String currentPage, String rows, Map<String, String[]> condition);
UserServiceImpl实现类:
@Override public PageBean<User> findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) { int currentPage = Integer.parseInt(_currentPage); int rows = Integer.parseInt(_rows); //判读最小页码 if (currentPage < 1){ currentPage = 1; } //1、创建空的PageBean对象 PageBean<User> pb = new PageBean<>(); //3、调用dao查询总记录数 int totalCount = dao.FindTotalCount(condition); pb.setTotalCount(totalCount); //5、计算总页码 int totalPage = (totalCount%rows)==0 ? totalCount/rows : (totalCount/rows)+1; //判断最大页码 if (currentPage >totalPage){ currentPage = totalPage; } //2、设置参数 pb.setCurrentPage(currentPage); pb.setRows(rows); //4、调用dao查询list集合 //计算开始的记录索引 int start = (currentPage-1)*rows; List<User> list = dao.fingByPage(start,rows,condition); pb.setList(list); pb.setTotalPage(totalPage); return pb; }
FindUserByPageServlet类:
@WebServlet("/findUserByPageServlet") public class FindUserByPageServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //1、获取参数 String currentPage = request.getParameter("currentPage");//当前页码 String rows = request.getParameter("rows");//每页显示条数 if (currentPage == null || "".equals(currentPage)){ currentPage = "1"; } if (rows == null || "".equals(rows)){ rows = "5"; } //获取条件查询参数 Map<String, String[]> condition = request.getParameterMap(); //2、调用servic查询 UserService service = new UserServiceImpl(); PageBean<User> pb = service.findUserByPage(currentPage,rows,condition); //3、将PageBean存入request request.setAttribute("pb",pb); //4、转发到list.jsp request.getRequestDispatcher("/list.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
list.jsp:
<form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post"> <div class="form-group"> <label for="exampleInputName2">姓名</label> <input type="text" class="form-control" id="exampleInputName2" name="name"> </div> <div class="form-group"> <label for="exampleInputName3">籍贯</label> <input type="text" class="form-control" id="exampleInputName3" name="address"> </div> <div class="form-group"> <label for="exampleInputEmail2">邮箱</label> <input type="email" class="form-control" id="exampleInputEmail2" name="email"> </div> <button type="submit" class="btn btn-default">查询</button> </form>
每页数据条件查询的代码实现
UserDaoImpl实现类:
@Override public List<User> fingByPage(int start, int rows, Map<String, String[]> condition) { String sql = "select * from user where 1 = 1"; StringBuilder sb = new StringBuilder(sql); //遍历map Set<String> keySet = condition.keySet(); //定义参数的集合 List<Object> params = new ArrayList<>(); 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); return template.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray()); }
list.jsp:
<body> <div class="container"> <h3 style="text-align: center">用户信息列表</h3> <div style="float: left;"> <form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post"> <div class="form-group"> <label for="exampleInputName2">姓名</label> <input type="text" class="form-control" id="exampleInputName2" name="name" value="${condition.name[0]}"> </div> <div class="form-group"> <label for="exampleInputName3">籍贯</label> <input type="text" class="form-control" id="exampleInputName3" name="address" value="${condition.address[0]}"> </div> <div class="form-group"> <label for="exampleInputEmail2">邮箱</label> <input type="text" class="form-control" id="exampleInputEmail2" name="email" value="${condition.email[0]}"> </div> <button type="submit" class="btn btn-default">查询</button> </form> </div> <div style="float:right;margin: 5px;"> <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a> <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中</a> </div> <form id="form" action="${pageContext.request.contextPath}/delSelectedServlet" method="post"> <table border="1" class="table table-bordered table-hover"> <tr class="success"> <th><input type="checkbox" id="firstCb"></th> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>籍贯</th> <th>QQ</th> <th>邮箱</th> <th>操作</th> </tr> <c:forEach items="${pb.list}" var="user" varStatus="s"> <tr> <th><input type="checkbox" name="uid" value="${user.id}"></th> <td>${s.count}</td> <td>${user.name}</td> <td>${user.gender}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.qq}</td> <td>${user.email}</td> <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a> <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除</a> </td> </tr> </c:forEach> </table> </form> <div> <nav aria-label="Page navigation"> <ul class="pagination"> <c:if test="${pb.currentPage == 1}"> <li class="disabled"> </c:if> <c:if test="${pb.currentPage != 1}"> <li> </c:if> <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage-1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <c:forEach begin="1" end="${pb.totalPage}" var="i"> <c:if test="${pb.currentPage == i}"> <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li> </c:if> <c:if test="${pb.currentPage != i}"> <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li> </c:if> </c:forEach> <li> <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage+1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> <span style="font-size: 25px;margin-left: 5px;"> 共${pb.totalCount}条记录,共${pb.totalPage}页 </span> </ul> </nav> </div> </div> </body>
标签:功能,rows,String,复杂,value,查询,pb,currentPage,condition From: https://www.cnblogs.com/xjw12345/p/16597646.html