复杂条件查询功能分析
总记录数统计的代码实现
UserDao接口:
/** * 查询总记录数 * @return */ int findTotalCount(Map<String, String[]> condition); /** * 分页查询每页记录 * * @param start * @param rows * @param condition * @return */ List<User1> findByPage(int start, int rows, Map<String, String[]> condition);
UserDaoImpl实现类:
@Override public int findTotalCount(Map<String, String[]> condition) { //1.定义sql String sql = "select count(*) from user1 where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //2.遍历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 ? ");//拼接sql params.add("%"+value+"%");// ?条件的值 } } System.out.println(sb.toString()); System.out.println(params); return template.queryForObject(sb.toString(), Integer.class,params.toArray()); }
UserService接口:
/** * 批量删除用户 * @param ids */ void delSelectedUser(String[] ids); /** * 分页条件查询 * @param currentPage * @param rows * @return */ PageBean<User1> findUserByPage(String currentPage, String rows, Map<String,String[]> condition);
UserServiceImpl实体类:
@Override public PageBean<User1> findUserByPage(String _currentPage, String _rows, Map<String,String[]> condition) { int currentPage = Integer.parseInt(_currentPage); int rows = Integer.parseInt(_rows); if (currentPage <= 0){ currentPage = 1; }else if (currentPage >= 8){ currentPage = 7; } //1.创建空的PageBean对象 PageBean<User1> pd = new PageBean<User1>(); //2.设置参数 pd.setCurrentPage(currentPage); pd.setRows(rows); //3.调用dao查询totalCount总记录数 int totalCount = dao.findTotalCount(condition); pd.setTotalCount(totalCount); //4.调用dao查询List集合 //计算开始的记录索引 int start = (currentPage -1) * rows; List<User1> list = dao.findByPage(start,rows,condition); pd.setList(list); //5.计算总页码 int totalPage = (totalCount % rows) == 0 ? totalCount/rows : (totalCount/rows) + 1;// 三元运算符 pd.setTotalPage(totalPage); return pd; }
FindUserByPageServlet:
package com.example.web.servlet; import com.example.domain.PageBean; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.Map; @WebServlet(name = "FindUserByPageServlet", value = "/findUserByPageServlet") public class FindUserByPageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override 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.调用service查询 UserService service = new UserServiceImpl(); PageBean<User1> pd = service.findUserByPage(currentPage,rows,condition); System.out.println(pd); //3.将PageBean存入request存储 request.setAttribute("pd", pd); //4.转发到list.jsp页面 request.getRequestDispatcher("/list.jsp").forward(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" name="name" class="form-control" id="exampleInputName2" placeholder="name"> </div> <div class="form-group"> <label for="exampleInputEmail2">籍贯</label> <input type="text" name="address" class="form-control" id="exampleInputEmail2" placeholder="address"> </div> <div class="form-group"> <label for="exampleInputEmail3">邮箱</label> <input type="email" name="email" class="form-control" id="exampleInputEmail3" placeholder="email"> </div> <button type="submit" class="btn btn-default">查询</button> </form>
每页数据条件查询的代码实现
UserDaoImpl实现类:
@Override public List<User1> findByPage(int start, int rows, Map<String, String[]> condition) { //1.定义sql语句 String sql = "select * from user1 where 1 = 1 "; StringBuilder sb = new StringBuilder(sql); //2.遍历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 ? ");//拼接sql 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<User1>(User1.class),params.toArray()); }
FindUserByPageServlet:
package com.example.web.servlet; import com.example.domain.PageBean; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.Map; @WebServlet(name = "FindUserByPageServlet", value = "/findUserByPageServlet") public class FindUserByPageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override 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.调用service查询 UserService service = new UserServiceImpl(); PageBean<User1> pd = service.findUserByPage(currentPage,rows,condition); System.out.println(pd); //3.将PageBean存入request存储 request.setAttribute("pd", pd); request.setAttribute("condition", condition);//将查询条件存入request //4.转发到list.jsp页面 request.getRequestDispatcher("/list.jsp").forward(request, response); } }
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" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" placeholder="name"> </div> <div class="form-group"> <label for="exampleInputEmail2">籍贯</label> <input type="text" name="address" value="${condition.address[0]}" class="form-control" id="exampleInputEmail2" placeholder="address"> </div> <div class="form-group"> <label for="exampleInputEmail3">邮箱</label> <input type="text" name="email" value="${condition.email[0]}" class="form-control" id="exampleInputEmail3" placeholder="email"> </div> <button type="submit" class="btn btn-default">查询</button> </form> </div> <div style="float: right; margin: 5px"> <td colspan="8" align="center"> <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a> </td> <td colspan="8" align="center"> <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中</a> </td> </div> <form id="from" 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="${pd.list}" var="user" varStatus="s"> <tr> <td><input type="checkbox" name="uid" value="${user.id}"></td> <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="${pageContext.request.contextPath}/delUserServlet?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="${pd.currentPage == 1}"> <li class="disabled"> </c:if> <c:if test="${pd.currentPage != 1}"> <li> </c:if> <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pd.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="${pd.totalPage}" var="i"> <!-- 分页选中 --> <c:if test="${pd.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="${pd.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=${pd.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"> 共${pd.totalCount}条记录,共${pd.totalPage}页 </span> </ul> </nav> </div> </div> </body>
标签:功能,rows,复杂,request,查询,pd,import,currentPage,condition From: https://www.cnblogs.com/qihaokuan/p/17030981.html