Examples of hibernate and spring for DAO processing

Source: Internet
Author: User
Quote from the "Spring" manual: Hibernate+spring is obviously a natural union.

Here is a Hibernatedao instance I used with spring processing, and I 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
7 Import InfoWeb. Pojo. Info;
8
9
Ten import net. SF. Hibernate. Hibernateexception;
Import Net. SF. Hibernate. Query;
Import Net. SF. Hibernate. session;
13
import org. springframework. Orm. Hibernate. Hibernatecallback;
import org. springframework. Orm. Hibernate. Support. Hibernatedaosupport;
16
17
18/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2004</p>
* <p>company: </p>
* @author Kenhongjie
* @version 1.0
25 */
26
The public class Infodaoimpl extends Hibernatedaosupport implements Iinfodao {...} 28/**
29 * Constructor
30 */
to public Infodaoimpl () {...} Super ();
33} 34
35
36/**
37 * Record Increase
* @param info Info
39 */
The public void SetInfo (info info) throws Exception {...} Gethibernatetemplate (). Save (info);
42} 43
44
45/**
46 * Get the record by ID
* @param ID String
* @return Info
49 */
Public Info Getinfobyid (String id) throws Exception {...} Wuyi info = (info) gethibernatetemplate (). Load (Info. class, ID);
return info;
53} 54
55
56/**
57 * Change Record
* @param info Info
59 */
The public void Modifyinfo (info info) throws Exception {...} Gethibernatetemplate (). Update (info);
62} 63
64
65/**
66 * Delete Record
* @param info Info
68 */
Throws public void Removeinfo (info info) Exception {...} Gethibernatetemplate (). Delete (info);
71} 72
73
74////////////////////////////////////////////////////////
75////////
76/////with the lower part without audit function///
77////////
78////////////////////////////////////////////////////////
79
80/**
81 * Total number of records taken
* @return int
83 */
Throws public int Getinfoscount () Exception {...} int count = 0;
A String querystring = "SELECT COUNT (*) from Info";
Count = ((Integer) gethibernatetemplate (). Iterate (QueryString). Next ()).
Intvalue ();
-Return count;
90} 91
92
93/**
94 * Take all Records collection
* @return Iterator
96 */
Getallinfos Public iterator () throws Exception {...} Iterator iterator = null;
The String querystring = "Select info from info as info", by info. ID DESC ";
List List = Gethibernatetemplate (). Find (querystring);
iterator = list. Iterator ();
102 return iterator;
103} 104
105
106/**
107 * Fetch Records Collection
108 * @return Iterator
109 * @param int position, int length
110 */
The public iterator Getinfos (int position, int length) throws Exception {...} 112 iterator iterator = null;
113 String QueryString = "Select info from info as 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
List = query. List ();
121//Put the results of the query into the iterator
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 {...} Iterator iterator = null;
134 info info = null;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.