对于搜索功能结果展示的分页处理,首先应该明确的是目前页,每页显示数据的条数,以及总的数据数目。
接下来,通过这些数据来计算出总的显示页数,以及数据库语句limit查询的范围。
Object result:是分页显示的数据,这里设置为Object的目的是为了方便通过JSON传输。
1 public class PageBean { 2 private int pageNum; // 当前页,从请求那边传过来 3 private int pageSize; // 每页显示的数据条数 4 private int totalRecord; //总的记录条数 5 6 private int totalPage; // 总页数:totalRecord/pageSize 7 private int startIndex; // 从第几行开始查数据 8 9 private Object result; // JSON封装好的数据 10 11 // 分页显示的页数,比如在页数上显示1,2,3,4,5.start就是1,end就是5 12 private int start; 13 private int end; 14 15 public PageBean(int pageNum, int pageSize, int totalRecord) { 16 this.pageNum = pageNum; 17 this.pageSize = pageSize; 18 this.totalRecord = totalRecord; 19 20 // 计算总页数 21 if (totalRecord % pageSize == 0) {// 总的数据正好整除每页所显示数据 22 this.totalPage = totalRecord / pageSize; 23 } else { 24 // 不整除,页数+1 25 this.totalPage = totalRecord / pageSize + 1; 26 } 27 28 // 数据库查询索引 29 this.startIndex = (pageNum - 1) * pageSize; 30 // 显示5页 31 this.start = 1; 32 this.end = 5; 33 34 // 显示页数的算法 35 if (totalPage <= 5) {// 总页数小于5页 36 this.end = this.totalPage; 37 } else { 38 // 总页数是大于5的,那么就要根据当前是第几页来判断显示的start和end 39 this.start = pageNum - 2; 40 this.end = pageNum + 2; 41 42 if (start <= 0) { 43 this.start = 1; 44 this.end = 5; 45 } 46 if (end > this.totalPage) { 47 this.end = totalPage; 48 this.start = end - 4; 49 } 50 } 51 52 } 53 54 public int getPageNum() { 55 return pageNum; 56 } 57 58 public void setPageNum(int pageNum) { 59 this.pageNum = pageNum; 60 } 61 62 public int getPageSize() { 63 return pageSize; 64 } 65 66 public void setPageSize(int pageSize) { 67 this.pageSize = pageSize; 68 } 69 70 public int getTotalRecord() { 71 return totalRecord; 72 } 73 74 public void setTotalRecord(int totalRecord) { 75 this.totalRecord = totalRecord; 76 } 77 78 public int getTotalPage() { 79 return totalPage; 80 } 81 82 public void setTotalPage(int totalPage) { 83 this.totalPage = totalPage; 84 } 85 86 public int getStartIndex() { 87 return startIndex; 88 } 89 90 public void setStartIndex(int startIndex) { 91 this.startIndex = startIndex; 92 } 93 94 public Object getResult() { 95 return result; 96 } 97 98 public void setResult(Object result) { 99 this.result = result; 100 } 101 102 public int getStart() { 103 return start; 104 } 105 106 public void setStart(int start) { 107 this.start = start; 108 } 109 110 public int getEnd() { 111 return end; 112 } 113 114 public void setEnd(int end) { 115 this.end = end; 116 } 117 118 119 }
持久层主要是得到PageBean对象里的startIndex和pageSize数据来进行limit查询
代码如下:
1 public class SearchUtils { 2 /** 3 * 通过模糊匹配帖子标题查询所有帖子信息(themeId,themeTitle,createTime,userID,nickName,boardId, 4 * boardNamereadCount,postCount,) 5 * 6 * @param keyWord 7 * @return 8 */ 9 public static List<Object> searchByThemeTitle(String keyWord) { 10 List<Object> searchResult = null; 11 String sql = "select bt.themeId, bt.themeTitle,bt.createTime,bt.userID,u.nickName,bt.boardId,sb.smallBoardName,bt.readCount,bt.postCount,bt.content, bb.bigBoardName from bbs_theme bt, `user` u, smallboard sb, bigboard bb where bt.boardId = sb.smallBoardId and bt.userID = u.userID and sb.parentId=bb.bigBoardId and bt.themeTitle like '%"+keyWord+"%' order by bt.readCount DESC"; 12 Session session = HibernateSessionFactory.openSession(); 13 try { 14 SQLQuery sqlQuery = session.createSQLQuery(sql); 15 searchResult = sqlQuery.list(); 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } finally { 19 session.close(); 20 } 21 return searchResult; 22 } 23 24 /** 25 * 分页查询 26 * @param start 27 * @param end 28 * @param keyWord 29 * @return 30 */ 31 public static List<Object> searchByThemeTitle(int start, int end, String keyWord) { 32 List<Object> searchResult = null; 33 String sql = "select bt.themeId, bt.themeTitle,bt.createTime,bt.userID,u.nickName,bt.boardId,sb.smallBoardName,bt.readCount,bt.postCount,bt.content, bb.bigBoardName from bbs_theme bt, `user` u, smallboard sb, bigboard bb where bt.boardId = sb.smallBoardId and bt.userID = u.userID and sb.parentId=bb.bigBoardId and bt.themeTitle like '%"+keyWord+"%' order by bt.readCount DESC limit "+start+","+end; 34 Session session = HibernateSessionFactory.openSession(); 35 try { 36 SQLQuery sqlQuery = session.createSQLQuery(sql); 37 searchResult = sqlQuery.list(); 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } finally { 41 session.close(); 42 } 43 return searchResult; 44 } 45 }