Quoted on the "Spring" manual: Hibernate Spring is obviously a born binding. Below is a hibernatedao instance I have handled by Spring, you can see that the amount of code is greatly reduced.
Java code:
1
2
Package infoweb.
DAO;
3
4
Import Java.
Util.
List;
5
Import Java.
Util.
Iterator;
6
Seduce
Import Infoweb.
Pojo.
INFO;
8
9
10
Import Net.
sf.
Hibernate.
HibernateException;
11
Import Net.
sf.
Hibernate.
Query;
12
Import Net.
sf.
Hibernate.
SESSION;
13
14
Import org.
Springframework.
ORM.
Hibernate.
HibernateCallback;
15
Import org.
Springframework.
ORM.
Hibernate.
Support.
Hibernatedaosupport;
16
In one
18
/ ** 19 *
Title: p> 20 *
Description: p> 21 *
Copyright: Copyright (c) 2004 p> 22 *
Company: P> 23 * @author Duan Hongjie 24 * @version 1.0 25 * /
26
27
public
Class InfodaoImpl
Extends Hibernatedaosupport
Implements Iinfodao
{
...}
Twist
/ ** 29 * Constructor 30 * /
31
Public InfodaoIMPL
(
)
{
...}
32 Super
(
);
33
}
34
35
36
/ ** 37 * Added record 38 * @Param info 39 * /
40
public
Void setInfo
(Info Info
)
Throws
EXCEPTION
{
...}
41 GethibernateTemplate
(
).
Save
(Info
);
42
}
43
44
45
/ ** 46 * Take records through ID 47 * @Param ID String 48 * @return info 49 * /
50
Public info getInfobyid
(
String ID
)
Throws
EXCEPTION
{
...}
51 info info =
(Info
GethibernateTemplate
(
).
Load
(Info.
Class, ID
);
52
Return INFO;
53
}
54
55
56
/ ** 57 * Modified Record 58 * @Param Info Info 59 * /
60
public
Void ModifyInfo
(Info Info
)
Throws
EXCEPTION
{
...}
61 GethibernateTemplate
(
).
Update
(Info
);
62
}
63
64
65
/ ** 66 * Delete Record 67 * @Param INFO INFO 68 * / 69
public
Void RemoveInfo
(Info Info
)
Throws
EXCEPTION
{
...}
70 gethibernateTemplate
(
).
Delete
(Info
);
71
}
72
73
74
75
/ ///
76
/ The following part without audit function ///
77
/ ///
78
79
80
/ ** 81 * Total number of records 82 * @return int 83 * /
84
public
Int getInfoscount
(
)
Throws
EXCEPTION
{
...}
85
INT count =
0;
86
String querystring = "SELECT COUNT
(*
) from info;
87 count =
(
(
Integer
GethibernateTemplate
(
).
iTerate
(QueryString
).
NEXT
(
)
).
88
INTVALUE
(
);
89
Return count;
90
}
91
92
93
/ ** 94 * Take all records collection 95 * @Return Iterator 96 * /
97
public
Iterator getAllinfos
(
)
Throws
EXCEPTION
{
...}
98
Iterator itrator =
NULL;
99
String queryString = "select info from info as info order by info.
ID DESC ";
100
List list = gethateTemplate
(
).
Find
(QueryString
);
101 item = list.
Iterator
(
);
102
Return Iterator;
103
}
104
105
106
/ ** 107 * Take a record collection 108 * @Return Iterator 109 * @Param INT POSITION, INT LENGTH 110 * /
111
public
Iterator getInfos
(
INT POSITION,
INT Length
)
Throws
EXCEPTION
{
...}
112
Iterator itrator =
NULL;
113
String queryString = "select info from info as info order by info.
ID DESC ";
114 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
115
// Set the starting point of the cursor
116 query.
SetFirstResult
(position)
);
117
// Set the length of the cursor
118 query.
SetMaxResults (Length
);
119
// Record generation
120
List list = query.
List
(
);
121
// put the result of the query into iterators
122 Iterator = List.
Iterator
(
);
123
Return Iterator;
124
}
125
126
127
/ ** 128 * Take the first record 129 * @Throws Exception 130 * @return station 131 * /
132
Public info getFirstinfo
(
)
Throws
EXCEPTION
{
...}
133
Iterator itrator =
NULL;
134 Info Info =
NULL;
135
String queryString = "select info from info as info order by info.
ID DESC ";
136 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
137
// Record generation
138
List list = query.
List
(
);
139
// put the result of the query into iterators
140 Iterator = List.
Iterator
(
);
141
IF
Iterator.
Hasnext
(
)
)
{
...}
142 info =
(Info
Iterator.
NEXT
(
);
143
}
144
Return INFO;
145
}
146
147
148
/ ** 149 * Take the last record 150 * @Throws Exception 151 * @return station 152 * /
153
Public info getlastinfo
(
)
Throws
EXCEPTION
{
...}
154
Iterator itrator =
NULL;
155 info info =
NULL;
156
String queryString = "select info from info as info order by info.
ID ASC ";
157 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
158
// Record generation
159
List list = query.
List
(
);
160
// put the result of the query into iterators
161 Iterator = List.
Iterator
(
);
162
IF
Iterator.
Hasnext
(
)
)
{
...}
163 info =
(Info
Iterator.
NEXT
(
);
164
}
165
Return INFO;
166
167
}
168
169
170
171
/ ///
172
/ The following part table must have a specific field to ///
173
/ /// 174
175
176
/ ** 177 * Take the total number of requirements, [ISPERSON field in [Table] 178 * @return int 179 * @Param int isperson 180 * /
181
182
public
Int getInfoScountbyisperson
(
Int isperson
)
Throws
EXCEPTION
{
...}
183
INT count =
0;
184
String querystring =
185 "SELECT COUNT
(*
From info as info where info.
ISPERSON = " isperson;
186 count =
(
(
Integer
GethibernateTemplate
(
).
iTerate
(QueryString
).
NEXT
(
)
).
187
INTVALUE
(
);
188
Return count;
189
}
190
191
192
/ ** 193 * Take all the qualified records collection, fuzzy query conditions. [There are ISPERSON fields in the table] 194 * @return itrator 195 * @Param Int ISPERSON 196 * /
197
198
public
Iterator getAllinfosbyisperson
(
Int isperson
)
Throws
EXCEPTION
{
...}
1999
Iterator itrator =
NULL;
200
String querystring = "select info from info as info where info.
IsPerson = "
201 IsPerson "Order by Info.
ID DESC ";
202
List list = gethateTemplate
(
).
Find
(QueryString
);
203
// put the result of the query into iterators
204 Iterator = List.
Iterator
(
);
205
Return Iterator;
206
}
207
208
209
/ ** 210 * Take the conditional record collection, fuzzy query conditions. [There are ISPERSON fields in the table] 211 * @Return Iterator 212 * @Param Int IsPerson, int position, int length 213 * /
214
215
public
Iterator getInfosbyisperson
(
Int isperson,
INT POSITION,
INT Length
)
Throws
216
EXCEPTION
{
...}
217
Iterator itrator =
NULL;
218
String querystring = "select info from info as info where info.
IsPerson = "
219 isperson "Order by Info.
ID DESC "; 220
// Create a query
221 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
222
// Set the starting point of the cursor
223 query.
SetFirstResult
(position)
);
224
// Set the length of the cursor
225 query.
SetMaxResults
(Length
);
226
// Record generation
227
List list = query.
List
(
);
228
// put the result of the query into iterators
229 item = list.
Iterator
(
);
230
Return Iterator;
231
}
232
233
234
235
/ ///
236
/ The following part table must have a specific field to query some ///
237
/ ///
238
///
239
/ ** 240 * Take the total number of conditions and fuzzy query conditions. [There are title fields in the table] 241 * @return int 243 * @Param string text 243 * /
244
public
Int getInfoscount
(
String text
)
Throws
EXCEPTION
{
...}
245
INT count =
0;
246 count =
(
(
Integer
GethibernateTemplate
(
).
iTerate
(
247 "SELECT Count
(*
From info as info where info.
Title Like
'% " Text 248"%' "
).
NEXT
(
)
).
INTVALUE
(
);
249
Return count;
250
}
251
252
253
/ ** 254 * Take all the qualified record sets, fuzzy query conditions. [Table must have title field] 255 * @return itrator 256 * @Param string text 257 * /
258
259
public
Iterator getAllinfos
(
String text
)
Throws
EXCEPTION
{
...}
260
Iterator itrator =
NULL;
261
String querystring =
262 "Select Info from Info As Info WHERE INFO.
Title Like
'% " Text 263"%' Order by Info.
ID DESC ";
264
// Create a query
265 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
266
// Record generation
267
List list = query.
List
(
);
268
// put the result of the query into the iterator 269 item = list.
Iterator
(
);
270
Return Iterator;
271
}
272
273
274
/ ** 275 * Take a collection of conditions, fuzzy query conditions. [There are title fields in the table] 276 * @return itrator 277 * @Param String text, int position, int length 278 * /
279
public
Iterator getInfos
(
String text,
INT POSITION,
INT Length
)
Throws
280
EXCEPTION
{
...}
281
Iterator itrator =
NULL;
282
String querystring =
"Select info from info as info where info.
Title Like
'% " Text 284"%' Order by Info.
ID DESC ";
285
286
// Create a query
287 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
288
// Set the starting point of the cursor
289 query.
SetFirstResult
(position)
);
290
// Set the length of the cursor
291 query.
SetMaxResults
(Length
);
292
// Record generation
293
List list = query.
List
(
);
294
// put the result of the query into iterators
295 item = list.
Iterator
(
);
296
Return Iterator;
297
}
298
299
300
301
/ ///
302
/ The following part table must have a specific field to ///
303
/ ///
304
305
306
/ ** 307 * Take the total number of requirements. [There are registername fields in the table] 308 * @return int 309 * @Param String text 310 * /
311
public
Int getInfoScountByregistername
(
String registername
)
Throws
EXCEPTION
{
...}
312
INT count =
0;
313 count =
(
(
Integer
GethibernateTemplate
(
).
iTerate
(
314 "SELECT COUNT
(*
From info as info where info.
Registername =
'" 315 registername "' "
).
NEXT
(
)
).
INTVALUE
(
);
316
Return count;
317
}
318
319
320
/ ** 321 * Get a record through the registration name, if there are multiple pieces, only the first one. [There is a registername field in the table] 322 * @param registername string 323 * @return info 324 * /
325
Public info getInfobyregistername
(
String registername
)
Throws
EXCEPTION
{
...}
326
Iterator itrator =
NULL;
327 Info Info =
NULL;
328
String querystring =
"SELECT INFO from info as info where info."
Registername =
'" 330 registername "' Order by Info.
ID DESC ";
331
// Create a query
332 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
333
// Record generation
334
List list = query.
List
(
);
335
// put the result of the query into iterators
336 Iterator = List.
Iterator
(
);
337
IF
Iterator.
Hasnext
(
)
)
{
...}
338 info =
(Info
Iterator.
NEXT
(
);
339
}
340
Return INFO;
341
}
342
343
344
/ ** 345 * Get all records collection through the registration name. [Table must have registername field] 346 * @Param registername string 347 * @return item 348 * /
349
public
Iterator getAllinfosbyregistername
(
String registername
)
Throws
350
EXCEPTION
{
...}
351
Iterator itrator =
NULL;
352
String querystring =
"SELECT INFO AS INFO WHERE INFO.
Registername =
'" 354 registername "' ORDER BY INFO.
ID DESC ";
355
// Create a query
356 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
357
// Record generation
358
List list = query.
List
(
);
359
// put the result of the query into iterators
360 Iterator = List.
Iterator
(
);
361
Return Iterator;
362
}
363
364
365
/ ** 366 * acquire a list of records through the registration name. [There are registername fields in the table] 367 * @Param registername string 368 * @return Iterator 369 * / 370
public
Iterator getInfosbyregistername
(
String registername,
INT POSITION,
371
INT Length
)
Throws
EXCEPTION
{
...}
372
Iterator itrator =
NULL;
373
String querystring =
"SELECT INFO from info as info where info.
Registername =
'" 375 registername "' Order by Info.
ID DESC ";
376
// Create a query
377 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
378
// Set the starting point of the cursor
379 query.
SetFirstResult
(position)
);
380
// Set the length of the cursor
381 query.
SetMaxResults
(Length
);
382
// Record generation
383
List list = query.
List
(
);
384
// put the result of the query into iterators
385 item = list.
Iterator
(
);
386
Return Iterator;
387
}
388
389
390
391
/ ///
392
/ The following part table must have a specific field to ///
393
/ ///
394
395
396
/ ** 397 * Take the total record. [There are Board_ID fields in the table] 398 * @return int 399 * @Param String BoardID 400 * /
401
public
Int getInfoScountByboard
(
String boardid
)
Throws
EXCEPTION
{
...}
402
INT count =
0;
403
404 count =
(
(
Integer
GethibernateTemplate
(
).
iTerate
(
405 "SELECT COUNT
(*
From info as info where info.
BoardID =
'" BoardID 406"' "
).
NEXT
(
)
).
INTVALUE
(
);
407
408
Return count;
409
}
410
411
412
/ ** 413 * Get all records collection through the section name. [Table must have a Board_ID field] 414 * @Param Boardid string 415 * @return Iterator 416 * / 417
public
Iterator getAllinfosbyboard
(
String boardid
)
Throws
EXCEPTION
{
...}
418
Iterator itrator =
NULL;
419
String querystring = "select info from info as info where info.
BoardID =
'" 420 boardid "' ORDER by info.
ID DESC ";
421
// Create a query
422 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
423
// Record generation
424
List list = query.
List
(
);
425
// put the result of the query into iterators
426 Iterator = List.
Iterator
(
);
427
Return Iterator;
428
}
429
430
431
/ ** 432 * acquires a list of records through the section name. [There are Board_ID fields in the table] 433 * @param boardid string 434 * @return item 435 * /
436
public
Iterator getInfosbyboard
(
String BoardID,
INT POSITION,
INT Length
)
Throws
437
EXCEPTION
{
...}
438
Iterator itrator =
NULL;
439
String querystring = "select info from info as info where info.
BoardID =
'" 440 boardid "' Order by Info.
ID DESC ";
441
442
// Create a query
443 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
444
// Set the starting point of the cursor
445 query.
SetFirstResult
(position)
);
446
// Set the length of the cursor
447 query.
SetMaxResults
(Length
);
448
// Record generation
449
List list = query.
List
(
);
450
// put the result of the query into iterators
451 Iterator = List.
Iterator
(
);
452
453
Return Iterator;
454
455
}
456
457
458
/ ** 459 * Take the total number of requirements. [Table must have a Board_id field, title] Fuzzy query title 460 * @return int 461 * @param string boardid, String text 462 * / 463
public
Int getInfoScountByboard
(
String BoardID,
String text
)
Throws
EXCEPTION
{
...}
464
INT count =
0;
465
466 count =
(
(
Integer
GethibernateTemplate
(
).
iTerate
(
467 "SELECT Count
(*
From info as info where info.
BoardID =
'" BoardId 468"' and info.
Title Like
'% " Text "%' "
).
NEXT
(
)
).
INTVALUE
(
);
469
470
Return count;
471
472
}
473
474
475
/ ** 476 * acquires a list of records through the section name. [There are Board_id fields in the table] Fuzzy query title 477 * @Param string boardid, int position, int length 478 * @return item 479 * /
480
public
Iterator getInfosbyboard
(
String BoardID,
INT POSITION,
INT Length,
481
String text
)
Throws
EXCEPTION
{
...}
482
Iterator itrator =
NULL;
483
String querystring = "select info from info as info where info.
BoardID =
'" 484 BoardID "' and INFO.
Title Like
'% " Text 485"%' Order by Info.
ID DESC ";
486
487
// Create a query
488 query query = gethibernateTemplate
(
).
CREATEQUERY
(GetSession)
(
QueryString
);
489
// Set the starting point of the cursor
490 query.
SetFirstResult
(position)
);
491
// Set the length of the cursor
492 query.
SetMaxResults
(Length
);
493
// Record generation
494
List list = query.
List
(
);
495
// put the result of the query into iterators
496 item = list.
Iterator
(
);
497
Return Iterator;
498
499
}
500
501
Learning in ing