一个通用的分页方法,不仅在数据库操作上分页了还在上层也做了分页.理解了这个就很容易理解其他框架的分页了.
java 代码
|
操作pager的PagerService
java 代码
|
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. }