一个通用的分页方法,不仅在数据库操作上分页了还在上层也做了分页.理解了这个就很容易理解其他框架的分页了.
java 代码
1. 2. import java.math. * ; 3. 4. 5. 6. public class Pager { 7. 8. private int totalRows; // 总行数
9. 10. private int pageSize=30 ; // 每页显示的行数
11. 12. private int currentPage; // 当前页号
13. 14. private int totalPages; // 总页数
15. 16. private int startRow; // 当前页在数据库中的起始行
17. 18. 19. 20. public Pager() { 21. 22. 23. 24. } 25. 26. 27. 28. public Pager( int _totalRows) { 29. 30. totalRows=_totalRows; 31. 32. totalPages=totalRows / pageSize; 33. 34. int mod=totalRows % pageSize; 35. 36. if (mod > 0 ) { 37. 38. totalPages ++ ; 39. 40. } 41. 42. currentPage=1 ; 43. 44. startRow=0 ; 45. 46. } 47. 48. 49. 50. public int getStartRow() { 51. 52. return startRow; 53. 54. } 55. 56. public int getTotalPages() { 57. 58. return totalPages; 59. 60. } 61. 62. public int getCurrentPage() { 63. 64. return currentPage; 65. 66. } 67. 68. public int getPageSize() { 69. 70. return pageSize; 71. 72. } 73. 74. public void setTotalRows( int totalRows) { 75. 76. this.totalRows = totalRows; 77. 78. } 79. 80. public void setStartRow( int startRow) { 81. 82. this.startRow = startRow; 83. 84. } 85. 86. public void setTotalPages( int totalPages) { 87. 88. this.totalPages = totalPages; 89. 90. } 91. 92. public void setCurrentPage( int currentPage) { 93. 94. this.currentPage = currentPage; 95. 96. } 97. 98. public void setPageSize( int pageSize) { 99. 100. this.pageSize = pageSize; 101. 102. } 103. 104. public int getTotalRows() { 105. 106. return totalRows; 107. 108. } 109. 110. public void first() { 111. 112. currentPage = 1 ; 113. 114. startRow = 0 ; 115. 116. } 117. 118. public void previous() { 119. 120. if (currentPage == 1 ) { 121. 122. return ; 123. 124. } 125. 126. currentPage -- ; 127. 128. startRow =(currentPage-1)*pageSize; 129. 130. } 131. 132. public void next() { 133. 134. if (currentPage<totalPages) { 135. 136. currentPage ++ ; 137. 138. } 139. 140. startRow=(currentPage-1)*pageSize; 141. 142. } 143. 144. public void last() { 145. 146. currentPage=totalPages; 147. 148. startRow=(currentPage-1 )*pageSize; 149. 150. } 151. 152. public void refresh(int _currentPage) { 153. 154. currentPage=_currentPage; 155. 156. if (currentPage>totalPages) { 157. 158. last(); 159. 160. } 161. 162. } 163. 164. } 165. 166. 167.
|
操作pager的PagerService
java 代码
1. 2. package com.game.common; 3. 4. 5. 6. public class PagerService { 7. 8. 9. 10. 11. 12. 13. 14. public Pager getPager(String currentPage,String pagerMethod, int totalRows) { 15. 16. // 定义pager对象,用于传到页面 17. 18. Pager pager= new Pager(totalRows); 19. 20. // 如果当前页号为空,表示为首次查询该页 21. 22. // 如果不为空,则刷新pager对象,输入当前页号等信息 23. 24. if (currentPage!=null ) { 25. 26. pager.refresh(Integer.parseInt(currentPage)); 27. 28. } 29. 30. // 获取当前执行的方法,首页,前一页,后一页,尾页。 31. 32. if (pagerMethod!=null ) { 33. 34. if (pagerMethod.equals("first")) { 35. 36. pager.first(); 37. 38. } else if (pagerMethod.equals("previous")) { 39. 40. pager.previous(); 41. 42. } else if (pagerMethod.equals("next")) { 43. 44. pager.next(); 45. 46. } else if (pagerMethod.equals("last")) { 47. 48. pager.last(); 49. 50. } 51. 52. } 53. 54. return pager; 55. 56. } 57. 58. } 59. 60. 61.
|
java 代码
1. 2. public class ProductsDaoImp extends HibernateDaoSupport implements ProductsDao { 3. 4. 5. 6. public int getRows() { 7. 8. 9. 10. String sql = " FROM Products ORDER BY gameNameCn " ; 11. 12. List list = this .getHibernateTemplate().find(sql); 13. 14. return list.size(); 15. 16. 17. 18. } 19. 20. 21. 22. public void addProduct(Products pd) { 23. 24. this.getHibernateTemplate().save(pd); 25. 26. 27. 28. } 29. 30. 31. 32. public void deleteProduct(Products pd) { 33. 34. this.getHibernateTemplate().delete(pd); 35. 36. 37. 38. } 39. 40. 41. 42. public String getMaxId() { 43. 44. String sql="select max(gameId)+1 from Products" ; 45. 46. String noStr=null; 47. 48. List list=(List)this.getHibernateTemplate().find(sql); 49. 50. Iterator it=list.iterator(); 51. 52. if(it.hasNext()){ 53. 54. Object noInt=it.next(); 55. 56. if(noInt==null){ 57. 58. noStr="1"; 59. 60. }else{ 61. 62. noStr=noInt.toString(); 63. 64. } 65. 66. 67. 68. } 69. 70. if(noStr.length()==1){ 71. 72. noStr="000"+noStr; 73. 74. }else if(noStr.length()==2){ 75. 76. noStr="00"+noStr; 77. 78. }else if(noStr.length()==2){ 79. 80. noStr="0"+noStr; 81. 82. } else { 83. 84. noStr=noStr; 85. 86. } 87. 88. 89. 90. return noStr; 91. 92. } 93. 94. 95. 96. public List getProducts() { 97. 98. String hql="from products order by gameNameCn" ; 99. 100. this.getHibernateTemplate().find(hql); 101. 102. return null; 103. 104. } 105. 106. 107. 108. public List getProducts(final int pageSize, final int startRow) { 109. 110. 111. 112. return this.getHibernateTemplate().executeFind(new HibernateCallback(){ 113. 114. 115. 116. public Object doInHibernate(Session session ) throws HibernateException, SQLException { 117. 118. Query query=session.createQuery("from products order by gameNameCn" ); 119. 120. query.setFirstResult(startRow); 121. 122. 123. 124. query.setMaxResults(pageSize); 125. 126. return query.list(); 127. 128. } 129. 130. 131. 132. }); 133. 134. 135. 136. } 137. 138. 139. 140. public Products getProducts(int gameId) { 141. 142. return (Products)this.getHibernateTemplate().get(Products.class, gameId); 143. 144. 145. 146. } 147. 148. 149. 150. public int getRows(String fieldName, String value) { 151. 152. 153. 154. String sql = " FROM Products where " + fieldName + " like '% " + value + " %' " + " ORDER BY gameNameCn " ; 155. 156. List list = this .getHibernateTemplate().find(sql); 157. 158. return list.size(); 159. 160. } 161. 162. 163. 164. public List queryProducts(String fieldName, String value) { 165. 166. String sql = "FROM Products where" + fieldName + "like'%" + value + "%' " + "ORDER BY gameNameCn" ; 167. 168. 169. 170. return this.getHibernateTemplate().find(sql); 171. 172. } 173. 174. 175. 176. public List queryProducts(String fieldName, String value, final int pageSize, 177. 178. final int startRow) { 179. 180. final String sql = " FROM Products where " + fieldName+ " like '% " + value + " %' " + " ORDER BY gameNameCn " ; 181. 182. return this.getHibernateTemplate().executeFind(new HibernateCallback(){ 183. 184. 185. 186. 187. 188. public Object doInHibernate(Session session ) throws HibernateException, SQLException { 189. 190. Query query=session.createQuery(sql); 191. 192. query.setFirstResult(startRow); 193. 194. 195. 196. query.setMaxResults(pageSize); 197. 198. return query.list(); 199. 200. } 201. 202. 203. 204. }); 205. 206. 207. 208. 209. 210. } 211. 212. 213. 214. public void updateProductd(Products pd) { 215. 216. this.getHibernateTemplate().update(pd); 217. 218. } 219. 220. 221. 222. }
|
阅读(2188) | 评论(0) | 转发(0) |