Hibernate和Spring对DAO处理的实例 (!!!)

系统 2153 0
引用"Spring"手册上的话说: Hibernate+Spring显然是天生的结合.

下面是我用spring处理的一个HibernateDAO实例,可以看到,代码量大大减少了.

java代码:

1
2 package infoweb. dao ;
3
4 import java. util . List ;
5 import java. util . Iterator ;
6
7 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
17
18 /**
19 * <p>Title: </p>
20 * <p>Description: </p>
21 * <p>Copyright: Copyright (c) 2004</p>
22 * <p>Company: </p>
23 * @author 段洪杰
24 * @version 1.0
25 */

26
27 public class InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO {
...}
28 /**
29 * 构造函数
30 */

31 public InfoDAOImpl ( ) {
...}
32 super ( ) ;
33 }
34
35
36 /**
37 * 增加记录
38 * @param info Info
39 */

40 public void setInfo ( Info info ) throws Exception {
...}
41 getHibernateTemplate ( ) . save ( info ) ;
42 }
43
44
45 /**
46 * 通过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 * 修改记录
58 * @param Info info
59 */

60 public void modifyInfo ( Info info ) throws Exception {
...}
61 getHibernateTemplate ( ) . update ( info ) ;
62 }
63
64
65 /**
66 * 删除记录
67 * @param Info info
68 */

69 public void removeInfo ( Info info ) throws Exception {
...}
70 getHibernateTemplate ( ) . delete ( info ) ;
71 }
72
73
74 ////////////////////////////////////////////////////////
75 ///// ///
76 /////以下部份不带审核功Ä軤 ///
77 ///// ///
78 ////////////////////////////////////////////////////////
79
80 /**
81 * 取记录总数
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 * 取所有记录集合
95 * @return Iterator
96 */

97 public Iterator getAllInfos ( ) throws Exception {
...}
98 Iterator iterator = null ;
99 String queryString = " select info from Info as info order by info. id desc";
100 List list = getHibernateTemplate ( ) . find ( queryString ) ;
101 iterator = list. iterator ( ) ;
102 return iterator;
103 }
104
105
106 /**
107 * 取记录集合
108 * @return Iterator
109 * @param int position, int length
110 */

111 public Iterator getInfos ( int position, int length ) throws Exception {
...}
112 Iterator iterator = null ;
113 String queryString = " select info from Info as info order by info. id desc";
114 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
115 //设置游标的起始点
116 query. setFirstResult ( position ) ;
117 //设置游标的长度
118 query. setMaxResults ( length ) ;
119 //记录生成
120 List list = query. list ( ) ;
121 //把查询到的结果放入迭代器
122 iterator = list. iterator ( ) ;
123 return iterator;
124 }
125
126
127 /**
128 * 取第一条记录
129 * @throws Exception
130 * @return Station
131 */

132 public Info getFirstInfo ( ) throws Exception {
...}
133 Iterator iterator = 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 //记录生成
138 List list = query. list ( ) ;
139 //把查询到的结果放入迭代器
140 iterator = list. iterator ( ) ;
141 if ( iterator. hasNext ( ) ) {
...}
142 info = ( Info ) iterator. next ( ) ;
143 }
144 return info;
145 }
146
147
148 /**
149 * 取最后一条记录
150 * @throws Exception
151 * @return Station
152 */

153 public Info getLastInfo ( ) throws Exception {
...}
154 Iterator iterator = 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 //记录生成
159 List list = query. list ( ) ;
160 //把查询到的结果放入迭代器
161 iterator = list. iterator ( ) ;
162 if ( iterator. hasNext ( ) ) {
...}
163 info = ( Info ) iterator. next ( ) ;
164 }
165 return info;
166
167 }
168
169
170 ////////////////////////////////////////////////////////
171 ///// ///
172 ///// 以下部份表中要有特定字段才能Õ吩诵袪 牳鋈撕推笠禒 ///
173 ///// ///
174 ////////////////////////////////////////////////////////
175
176 /**
177 * 取符合条件记录总数, [表中要有 isperson 字段]
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 * 取所有符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
194 * @return Iterator
195 * @param int isPerson
196 */

197
198 public Iterator getAllInfosByIsperson ( int isPerson ) throws Exception {
...}
199 Iterator iterator = null ;
200 String queryString = " select info from Info as info where info. isperson =" +
201 isPerson + " order by info. id desc";
202 List list = getHibernateTemplate ( ) . find ( queryString ) ;
203 //把查询到的结果放入迭代器
204 iterator = list. iterator ( ) ;
205 return iterator;
206 }
207
208
209 /**
210 * 取符合条件记录集合, 模糊查询条件.[表中要有 isperson 字段]
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 iterator = null ;
218 String queryString = " select info from Info as info where info. isperson =" +
219 isPerson + " order by info. id desc";
220 //创建查询
221 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
222 //设置游标的起始点
223 query. setFirstResult ( position ) ;
224 //设置游标的长度
225 query. setMaxResults ( length ) ;
226 //记录生成
227 List list = query. list ( ) ;
228 //把查询到的结果放入迭代器
229 iterator = list. iterator ( ) ;
230 return iterator;
231 }
232
233
234 ////////////////////////////////////////////////////////
235 ///// ///
236 ///// 以下部份表中要有特定字段才能Õ吩诵袪 查询部份 ///
237 ///// ///
238 ///////////////////////////////////////////////////////
239 /**
240 * 取符合条件记录总数, 模糊查询条件.[表中要有 title 字段]
241 * @return int
242 * @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 * 取所有符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
255 * @return Iterator
256 * @param String text
257 */

258
259 public Iterator getAllInfos ( String text ) throws Exception {
...}
260 Iterator iterator = null ;
261 String queryString =
262 " select info from Info as info where info. title like '%" + text +
263 "%'
order by info. id desc";
264 //创建查询
265 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
266 //记录生成
267 List list = query. list ( ) ;
268 //把查询到的结果放入迭代器
269 iterator = list. iterator ( ) ;
270 return iterator;
271 }
272
273
274 /**
275 * 取符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
276 * @return Iterator
277 * @param String text,int position, int length
278 */

279 public Iterator getInfos ( String text, int position, int length ) throws
280 Exception {
...}
281 Iterator iterator = null ;
282 String queryString =
283 " select info from Info as info where info. title like '%" + text +
284 "%'
order by info. id desc";
285
286 //创建查询
287 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
288 //设置游标的起始点
289 query. setFirstResult ( position ) ;
290 //设置游标的长度
291 query. setMaxResults ( length ) ;
292 //记录生成
293 List list = query. list ( ) ;
294 //把查询到的结果放入迭代器
295 iterator = list. iterator ( ) ;
296 return iterator;
297 }
298
299
300 ////////////////////////////////////////////////////////
301 ///// ///
302 ///// 以下部份表中要有特定字段才能Õ吩诵袪 犠⒉嵯喙貭 ///
303 ///// ///
304 ////////////////////////////////////////////////////////
305
306 /**
307 * 取符合条件记录总数.[ 表中要有 registername 字段]
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 * 通过注册名取得一条记录,如有多条,只取第一条.[表中要有 registername字段]
322 * @param registername String
323 * @return Info
324 */

325 public Info getInfoByRegisterName ( String registerName ) throws Exception {
...}
326 Iterator iterator = null ;
327 Info info = null ;
328 String queryString =
329 " select info from Info as info where info. registername = '" +
330 registerName + "'
order by info. id desc";
331 //创建查询
332 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
333 //记录生成
334 List list = query. list ( ) ;
335 //把查询到的结果放入迭代器
336 iterator = list. iterator ( ) ;
337 if ( iterator. hasNext ( ) ) {
...}
338 info = ( Info ) iterator. next ( ) ;
339 }
340 return info;
341 }
342
343
344 /**
345 * 通过注册名取得所有记录集合.[表中要有 registername字段]
346 * @param registername String
347 * @return Iterator
348 */

349 public Iterator getAllInfosByRegisterName ( String registerName ) throws
350 Exception {
...}
351 Iterator iterator = null ;
352 String queryString =
353 " select info from Info as info where info. registername = '" +
354 registerName + "'
order by info. id desc";
355 //创建查询
356 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
357 //记录生成
358 List list = query. list ( ) ;
359 //把查询到的结果放入迭代器
360 iterator = list. iterator ( ) ;
361 return iterator;
362 }
363
364
365 /**
366 * 通过注册名取得记录列表.[表中要有 registername字段]
367 * @param registername String
368 * @return Iterator
369 */

370 public Iterator getInfosByRegisterName ( String registerName, int position,
371 int length ) throws Exception {
...}
372 Iterator iterator = null ;
373 String queryString =
374 " select info from Info as info where info. registername = '" +
375 registerName + "'
order by info. id desc";
376 //创建查询
377 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
378 //设置游标的起始点
379 query. setFirstResult ( position ) ;
380 //设置游标的长度
381 query. setMaxResults ( length ) ;
382 //记录生成
383 List list = query. list ( ) ;
384 //把查询到的结果放入迭代器
385 iterator = list. iterator ( ) ;
386 return iterator;
387 }
388
389
390 ////////////////////////////////////////////////////////
391 ///// ///
392 ///// 以下部份表中要有特定字段才能Õ吩诵袪 犑餍桶婵闋 ///
393 ///// ///
394 ////////////////////////////////////////////////////////
395
396 /**
397 * 取记录总数.[ 表中要有 board_id 字段]
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 * 通过版块名取得所有记录集合.[表中要有 board_id字段]
414 * @param BoardId String
415 * @return Iterator
416 */

417 public Iterator getAllInfosByBoard ( String boardId ) throws Exception {
...}
418 Iterator iterator = null ;
419 String queryString = " select info from Info as info where info. boardId = '" +
420 boardId + "'
order by info. id desc";
421 //创建查询
422 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
423 //记录生成
424 List list = query. list ( ) ;
425 //把查询到的结果放入迭代器
426 iterator = list. iterator ( ) ;
427 return iterator;
428 }
429
430
431 /**
432 * 通过版块名取得记录列表.[表中要有 board_id字段]
433 * @param BoardId String
434 * @return Iterator
435 */

436 public Iterator getInfosByBoard ( String boardId, int position, int length ) throws
437 Exception {
...}
438 Iterator iterator = null ;
439 String queryString = " select info from Info as info where info. boardId = '" +
440 boardId + "'
order by info. id desc";
441
442 //创建查询
443 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
444 //设置游标的起始点
445 query. setFirstResult ( position ) ;
446 //设置游标的长度
447 query. setMaxResults ( length ) ;
448 //记录生成
449 List list = query. list ( ) ;
450 //把查询到的结果放入迭代器
451 iterator = list. iterator ( ) ;
452
453 return iterator;
454
455 }
456
457
458 /**
459 * 取符合条件记录总数.[ 表中要有 board_id 字段,title] 模糊查询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 * 通过版块名取得记录列表.[表中要有 board_id字段] 模糊查询title
477 * @param String boardID,int position, int length
478 * @return Iterator
479 */

480 public Iterator getInfosByBoard ( String boardId, int position, int length,
481 String text ) throws Exception {
...}
482 Iterator iterator = 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 //创建查询
488 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
489 //设置游标的起始点
490 query. setFirstResult ( position ) ;
491 //设置游标的长度
492 query. setMaxResults ( length ) ;
493 //记录生成
494 List list = query. list ( ) ;
495 //把查询到的结果放入迭代器
496 iterator = list. iterator ( ) ;
497 return iterator;
498
499 }
500
501
502 ////////////////////////////////////////////////////////
503 ///// ///
504 /////以下部份带有审核功Ä軤 ///
505 ///// ///
506 ////////////////////////////////////////////////////////
507
508 /**
509 * 取记录总数
510 * @return int
511 * @param int isAuditing
512 */

513 public int getInfosCount ( int isAuditing ) throws Exception {
...}
514 int count = 0 ;
515
516 count = ( ( Integer ) getHibernateTemplate ( ) . iterate (
517 "select count ( * ) from Info as info where info. isauditing =" +
518 isAuditing ) . next ( ) ) . intValue ( ) ;
519 return count;
520 }
521
522
523 /**
524 * 取所有记录集合
525 * @return Iterator
526 * @param int position, int length,int isAuditing
527 */

528 public Iterator getAllInfos ( int isAuditing ) throws Exception {
...}
529 Iterator iterator = null ;
530 String queryString =
531 " select info from Info as info where info. isauditing =" + isAuditing +
532 " order by info. id desc";
533 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
534 //记录生成
535 List list = query. list ( ) ;
536 //把查询到的结果放入迭代器
537 iterator = list. iterator ( ) ;
538 return iterator;
539 }
540
541
542 /**
543 * 取记录集合
544 * @return Iterator
545 * @param int position, int length,int isAuditing
546 */

547 public Iterator getInfos ( int position, int length, int isAuditing ) throws
548 Exception {
...}
549 Iterator iterator = null ;
550 String queryString =
551 " select info from Info as info where info. isauditing =" + isAuditing +
552 " order by info. id desc";
553
554 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
555 //设置游标的起始点
556 query. setFirstResult ( position ) ;
557 //设置游标的长度
558 query. setMaxResults ( length ) ;
559 //记录生成
560 List list = query. list ( ) ;
561 //把查询到的结果放入迭代器
562 iterator = list. iterator ( ) ;
563
564 return iterator;
565 }
566
567
568 ////////////////////////////////////////////////////////////////
569 ///// ///
570 ///// 以下部份表中要有特定字段才能Õ吩诵Ð 有审核功Ä軤 个人和Æ笠禒 ///
571 ///// ///
572 ///////////////////////////////////////////////////////////////
573
574 /**
575 * 取符合条件记录总数, [表中要有 isperson isAuditing 字段]
576 * @return int
577 * @param int isPerson,int isAuditing
578 */

579
580 public int getInfosCountByIsperson ( int isPerson, int isAuditing ) throws
581 Exception {
...}
582 int count = 0 ;
583
584 count = ( ( Integer ) getHibernateTemplate ( ) . iterate (
585 "select count ( * ) from Info as info where info. isperson =" + isPerson +
586 " and info. isauditing =" + isAuditing ) . next ( ) ) . intValue ( ) ;
587
588 return count;
589 }
590
591
592 /**
593 * 取所有符合条件记录集合, 模糊查询条件.[表中要有 isperson isAuditing 字段]
594 * @return Iterator
595 * @param int isPerson,int isAuditing
596 */

597
598 public Iterator getAllInfosByIsperson ( int isPerson, int isAuditing ) throws
599 Exception {
...}
600 Iterator iterator = null ;
601 String queryString = " select info from Info as info where info. isperson =" +
602 isPerson + " and info. isauditing =" + isAuditing +
603 " order by info. id desc";
604
605 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
606 //记录生成
607 List list = query. list ( ) ;
608 //把查询到的结果放入迭代器
609 iterator = list. iterator ( ) ;
610
611 return iterator;
612 }
613
614
615 /**
616 * 取符合条件记录集合, 模糊查询条件.[表中要有 isperson isAuditing 字段]
617 * @return Iterator
618 * @param int isPerson,int position, int length,int isAuditing
619 */

620
621 public Iterator getInfosByIsperson ( int isPerson, int position, int length,
622 int isAuditing ) throws Exception {
...}
623 Iterator iterator = null ;
624 String queryString = " select info from Info as info where info. isperson =" +
625 isPerson + " and info. isauditing =" + isAuditing +
626 " order by info. id desc";
627
628 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
629 //设置游标的起始点
630 query. setFirstResult ( position ) ;
631 //设置游标的长度
632 query. setMaxResults ( length ) ;
633 //记录生成
634 List list = query. list ( ) ;
635 //把查询到的结果放入迭代器
636 iterator = list. iterator ( ) ;
637
638 return iterator;
639
640 }
641
642
643 ////////////////////////////////////////////////////////
644 ///// ///
645 ///// 要有特定字段才能Õ吩诵袪 有审核功Ä軤 查询部份 ///
646 ///// ///
647 ///////////////////////////////////////////////////////
648 /**
649 * 取符合条件记录总数, 模糊查询条件.[表中要有 title 字段]
650 * @return int
651 * @param String text,int isAuditing
652 */

653 public int getInfosCount ( String text, int isAuditing ) throws Exception {
...}
654 int count = 0 ;
655
656 count = ( ( Integer ) getHibernateTemplate ( ) . iterate (
657 "select count ( * ) from Info as info where info. isauditing =" +
658 isAuditing + " and info. title like '%" + text + "%' " ) . next ( ) ) .
659 intValue ( ) ;
660
661 return count;
662 }
663
664
665 /**
666 * 取所有符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
667 * @return Iterator
668 * @param String text,int isAuditing
669 */

670
671 public Iterator getAllInfos ( String text, int isAuditing ) throws Exception {
...}
672 Iterator iterator = null ;
673 String queryString =
674 " select info from Info as info where info. isauditing =" + isAuditing +
675 " and info. title like '%" + text + "%' order by info. id desc";
676
677 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
678 //记录生成
679 List list = query. list ( ) ;
680 //把查询到的结果放入迭代器
681 iterator = list. iterator ( ) ;
682
683 return iterator;
684 }
685
686
687 /**
688 * 取符合条件记录集合, 模糊查询条件.[表中要有 title 字段]
689 * @return Iterator
690 * @param String text,int position, int length,int isAuditing
691 */

692 public Iterator getInfos ( String text, int position, int length,
693 int isAuditing ) throws Exception {
...}
694 Iterator iterator = null ;
695 String queryString =
696 " select info from Info as info where info. isauditing =" + isAuditing +
697 " and info. title like '%" + text + "%' order by info. id desc";
698
699 //创建查询
700 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
701 //设置游标的起始点
702 query. setFirstResult ( position ) ;
703 //设置游标的长度
704 query. setMaxResults ( length ) ;
705 //记录生成
706 List list = query. list ( ) ;
707 //把查询到的结果放入迭代器
708 iterator = list. iterator ( ) ;
709
710 return iterator;
711 }
712
713
714 ////////////////////////////////////////////////////////
715 ///// ///
716 ///// 要有特定字段才能Õ吩诵袪 有审核功Ä軤 注册相关 ///
717 ///// ///
718 ////////////////////////////////////////////////////////
719
720 /**
721 * 取符合条件记录总数.[ 表中要有 registername isauditing字段]
722 * @return int
723 * @param String text,int isAuditing
724 */

725 public int getInfosCountByRegisterName ( String registerName, int isAuditing ) throws
726 Exception {
...}
727 int count = 0 ;
728
729 count = ( ( Integer ) getHibernateTemplate ( ) . iterate (
730 "select count ( * ) from Info as info where info. isauditing =" +
731 isAuditing + " and info. registername = '" + registerName + "' " ) . next ( ) ) .
732 intValue ( ) ;
733
734 return count;
735 }
736
737
738 /**
739 * 通过注册名取得一条记录,如有多条,只取第一条.[表中要有 registername isauditing字段]
740 * @param registername String,int isAuditing
741 * @return Info
742 */

743 public Info getInfoByRegisterName ( String registerName, int isAuditing ) throws
744 Exception {
...}
745 Iterator iterator = null ;
746 Info info = null ;
747
748 String queryString =
749 " select info from Info as info where info. isauditing =" + isAuditing +
750 " and info. registername = '" + registerName + "' order by info. id desc";
751
752 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
753 //记录生成
754 List list = query. list ( ) ;
755 //把查询到的结果放入迭代器
756 iterator = list. iterator ( ) ;
757 if ( iterator. hasNext ( ) ) {
...}
758 info = ( Info ) iterator. next ( ) ;
759 }
760
761 return info;
762 }
763
764
765 /**
766 * 通过注册名取得所有记录集合.[表中要有 registername isauditing字段]
767 * @param registername String,int isAuditing
768 * @return Iterator
769 */

770 public Iterator getAllInfosByRegisterName ( String registerName, int isAuditing ) throws
771 Exception {
...}
772 Iterator iterator = null ;
773
774 String queryString =
775 " select info from Info as info where info. isauditing =" + isAuditing +
776 " and info. registername = '" + registerName + "' order by info. id desc";
777
778 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
779 //记录生成
780 List list = query. list ( ) ;
781 //把查询到的结果放入迭代器
782 iterator = list. iterator ( ) ;
783
784 return iterator;
785 }
786
787
788 /**
789 * 通过注册名取得记录列表.[表中要有 registername isauditing字段]
790 * @param registername String,int isAuditing
791 * @return Iterator
792 */

793 public Iterator getInfosByRegisterName ( String registerName, int position,
794 int length, int isAuditing ) throws
795 Exception {
...}
796 Iterator iterator = null ;
797 String queryString =
798 " select info from Info as info where info. isauditing =" + isAuditing +
799 " and info. registername = '" + registerName + "' order by info. id desc";
800
801 //创建查询
802 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
803 //设置游标的起始点
804 query. setFirstResult ( position ) ;
805 //设置游标的长度
806 query. setMaxResults ( length ) ;
807 //记录生成
808 List list = query. list ( ) ;
809 //把查询到的结果放入迭代器
810 iterator = list. iterator ( ) ;
811
812 return iterator;
813 }
814
815
816 ////////////////////////////////////////////////////////
817 ///// ///
818 ///// 要有特定字段才能Õ吩诵袪 有审核功能 树型版块 ///
819 ///// ///
820 ////////////////////////////////////////////////////////
821
822 /**
823 * 取记录总数.[ 表中要有 board_id isauditing字段]
824 * @return int
825 * @param String boardId,int isAuditing
826 */

827 public int getInfosCountByBoard ( String boardId, int isAuditing ) throws
828 Exception {
...}
829 int count = 0 ;
830
831 count = ( ( Integer ) getHibernateTemplate ( ) . iterate (
832 "select count ( * ) from Info as info where info. isauditing =" +
833 isAuditing + " and info. boardId = '" + boardId + "' " ) . next ( ) ) . intValue ( ) ;
834
835 return count;
836 }
837
838
839 /**
840 * 通过版块名取得所有记录集合.[表中要有 board_id isauditing字段]
841 * @param BoardId String,int isAuditing
842 * @return Iterator
843 */

844 public Iterator getAllInfosByBoard ( String boardId, int isAuditing ) throws
845 Exception {
...}
846 Iterator iterator = null ;
847
848 String queryString =
849 " select info from Info as info where info. isauditing =" + isAuditing +
850 " and info. boardId = '" + boardId + "' order by info. id desc";
851
852 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
853 //记录生成
854 List list = query. list ( ) ;
855 //把查询到的结果放入迭代器
856 iterator = list. iterator ( ) ;
857 return iterator;
858 }
859
860
861 /**
862 * 通过版块名取得记录列表.[表中要有 board_id isauditing字段]
863 * @param BoardId String,int isAuditing
864 * @return Iterator
865 */

866 public Iterator getInfosByBoard ( String boardId, int position, int length,
867 int isAuditing ) throws Exception {
...}
868 Iterator iterator = null ;
869 String queryString =
870 " select info from Info as info where info. isauditing =" + isAuditing +
871 " and info. boardId = '" + boardId + "' order by info. id desc";
872
873 //创建查询
874 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
875 //设置游标的起始点
876 query. setFirstResult ( position ) ;
877 //设置游标的长度
878 query. setMaxResults ( length ) ;
879 //记录生成
880 List list = query. list ( ) ;
881 //把查询到的结果放入迭代器
882 iterator = list. iterator ( ) ;
883
884 return iterator;
885
886 }
887
888
889 /**
890 * 取符合条件记录总数.[ 表中要有 board_id isauditing字段,title] 模糊查询title
891 * @return int
892 * @param String boardId ,String text,int isAuditing
893 */

894 public int getInfosCountByBoard ( String boardId, String text, int isAuditing ) throws
895 Exception {
...}
896 int count = 0 ;
897
898 count = ( ( Integer ) getHibernateTemplate ( ) . iterate (
899 "select count ( * ) from Info as info where info. isauditing =" +
900 isAuditing + " and info. boardId = '" + boardId +
901 "'
and info. title like '%" +
902 text + "%'
" ) . next ( ) ) . intValue ( ) ;
903
904 return count;
905
906 }
907
908
909 /**
910 * 通过版块名取得记录列表.[表中要有 board_id字段 isauditing] 模糊查询title
911 * @param String boardId,int position, int length,int isAuditing
912 * @return Iterator
913 */

914 public Iterator getInfosByBoard ( String boardId, int position, int length,
915 String text, int isAuditing ) throws Exception {
...}
916 Iterator iterator = null ;
917 String queryString =
918 " select info from Info as info where info. isauditing =" + isAuditing +
919 " and info. boardId = '" + boardId + "' and info. title like '%" + text +
920 "%'
order by info. id desc";
921
922 //创建查询
923 Query query = getHibernateTemplate ( ) . createQuery ( getSession ( ) , queryString ) ;
924 //设置游标的起始点
925 query. setFirstResult ( position ) ;
926 //设置游标的长度
927 query. setMaxResults ( length ) ;
928 //记录生成
929 List list = query. list ( ) ;
930 //把查询到的结果放入迭代器
931 iterator = list. iterator ( ) ;
932
933 return iterator;
934
935 }
936
937
938 }
939
940

Hibernate和Spring对DAO处理的实例 (!!!)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论