标签:
Page.java
1 package com.imooc.entity; 2 3 /** 4 * 分页对应的实体类 5 */ 6 public class Page { 7 /** 8 * 总条数 9 */ 10 private int totalNumber; 11 /** 12 * 当前第几页 13 */ 14 private int currentPage; 15 /** 16 * 总页数 17 */ 18 private int totalPage; 19 /** 20 * 每页显示条数 21 */ 22 private int pageNumber = 5; 23 /** 24 * 数据库中limit的参数,从第几条开始取 25 */ 26 private int dbIndex; 27 /** 28 * 数据库中limit的参数,一共取多少条 29 */ 30 private int dbNumber; 31 32 /** 33 * 根据当前对象中属性值计算并设置相关属性值 34 */ 35 public void count() { 36 // 计算总页数 37 int totalPageTemp = this.totalNumber / this.pageNumber; 38 int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1; 39 totalPageTemp = totalPageTemp + plus; 40 if(totalPageTemp <= 0) { 41 totalPageTemp = 1; 42 } 43 this.totalPage = totalPageTemp; 44 45 // 设置当前页数 46 // 总页数小于当前页数,应将当前页数设置为总页数 47 if(this.totalPage < this.currentPage) { 48 this.currentPage = this.totalPage; 49 } 50 // 当前页数小于1设置为1 51 if(this.currentPage < 1) { 52 this.currentPage = 1; 53 } 54 55 // 设置limit的参数 56 this.dbIndex = (this.currentPage - 1) * this.pageNumber; 57 this.dbNumber = this.pageNumber; 58 } 59 60 public int getTotalNumber() { 61 return totalNumber; 62 } 63 64 public void setTotalNumber(int totalNumber) { 65 this.totalNumber = totalNumber; 66 this.count(); 67 } 68 69 public int getCurrentPage() { 70 return currentPage; 71 } 72 73 public void setCurrentPage(int currentPage) { 74 this.currentPage = currentPage; 75 } 76 77 public int getTotalPage() { 78 return totalPage; 79 } 80 81 public void setTotalPage(int totalPage) { 82 this.totalPage = totalPage; 83 } 84 85 public int getPageNumber() { 86 return pageNumber; 87 } 88 89 public void setPageNumber(int pageNumber) { 90 this.pageNumber = pageNumber; 91 this.count(); 92 } 93 94 public int getDbIndex() { 95 return dbIndex; 96 } 97 98 public void setDbIndex(int dbIndex) { 99 this.dbIndex = dbIndex; 100 } 101 102 public int getDbNumber() { 103 return dbNumber; 104 } 105 106 public void setDbNumber(int dbNumber) { 107 this.dbNumber = dbNumber; 108 } 109 }
xml
1 <select id="queryMessageListByPage" parameterType="java.util.Map" resultMap="MessageResult"> 2 select <include refid="columns"/> from MESSAGE 3 <where> 4 <if test="message.command != null and !"".equals(message.command.trim())"> 5 and COMMAND=#{message.command} 6 </if> 7 <if test="message.description != null and !"".equals(message.description.trim())"> 8 and DESCRIPTION like ‘%‘ #{message.description} ‘%‘ 9 </if> 10 </where> 11 order by ID LIMIT #{page.dbIndex},#{page.dbNumber} 12 </select>
dao
1 public List<Message> queryMessageListByPage(Map<String,Object> parameter) { 2 DBAccess dbAccess = new DBAccess(); 3 List<Message> messageList = new ArrayList<Message>(); 4 SqlSession sqlSession = null; 5 try { 6 sqlSession = dbAccess.getSqlSession(); 7 // 通过sqlSession执行SQL语句 8 IMessage imessage = sqlSession.getMapper(IMessage.class); 9 messageList = imessage.queryMessageListByPage(parameter); 10 } catch (Exception e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } finally { 14 if(sqlSession != null) { 15 sqlSession.close(); 16 } 17 } 18 return messageList; 19 }
servlet
1 package com.imooc.servlet; 2 3 import java.io.IOException; 4 import java.util.regex.Pattern; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.imooc.entity.Page; 12 import com.imooc.service.QueryService; 13 14 /** 15 * 列表页面初始化和查询控制 16 */ 17 @SuppressWarnings("serial") 18 public class ListServlet extends HttpServlet { 19 @Override 20 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 21 throws ServletException, IOException { 22 // 设置编码 23 req.setCharacterEncoding("UTF-8"); 24 // 接受页面的值 25 String command = req.getParameter("command"); 26 String description = req.getParameter("description"); 27 String currentPage = req.getParameter("currentPage"); 28 // 创建分页对象 29 Page page = new Page(); 30 Pattern pattern = Pattern.compile("[0-9]{1,9}"); 31 if(currentPage == null || !pattern.matcher(currentPage).matches()) { 32 page.setCurrentPage(1); 33 } else { 34 page.setCurrentPage(Integer.valueOf(currentPage)); 35 } 36 QueryService listService = new QueryService(); 37 // 查询消息列表并传给页面 38 req.setAttribute("messageList", listService.queryMessageListByPage(command, description,page)); 39 // 向页面传值 40 req.setAttribute("command", command); 41 req.setAttribute("description", description); 42 req.setAttribute("page", page); 43 // 向页面跳转 44 req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp); 45 } 46 47 @Override 48 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 49 throws ServletException, IOException { 50 this.doGet(req, resp); 51 } 52 }
标签:
原文地址:http://www.cnblogs.com/sherrykid/p/4631189.html