Today, we finally implemented the hibernate implementation page .. Added the SSH example of oksonic to implement query paging.
At the same time, I have reviewed the implementation methods of srtuts + spring + hibernate, which are summarized as follows:
1 first, you must map the corresponding database tables. Put the ing XML file of the ing Class and table in the packet BO:
Contains the abstractuser. Java User. Java User. HBM. xml file.
The ing configuration file of the table is as follows:
<Hibernate-mapping>
<Class name = "com. OA. Data. Bo. User" table = "userlist">
<ID name = "ID" type = "integer">
<Column name = "ID"/>
<Generator class = "native"> </generator>
</ID>
<Property name = "username" type = "string">
<Column name = "username" length = "100" not-null = "true"/>
</Property>
<Property name = "userpwd" type = "string">
<Column name = "userpwd" length = "100" not-null = "true"/>
</Property>
</Class>
</Hibernate-mapping>
2. Write the DaO data access interface iuserdao. Java and place it in the DaO package:
Package com. OA. Data. Dao;
Import java. util. List;
Import com. OA. Data. Bo. user;
Public interface iuserdao {
Public abstract void save (User transientinstance );
Public abstract void Delete (User persistentinstance );
Public abstract user findbyid (Java. Lang. Integer ID );
Public abstract list findbyexample (user instance );
Public abstract user merge (User detachedinstance );
Public abstract void attachdirty (user instance );
Public abstract void attachclean (user instance );
Public abstract user findbyusername (string username );
Public list findwithpage (INT pagesize, int startrow );
Public int counter ();
}
3 Write Dao Data Access Implementation class userdao. Java, put in Dao. IMP package:
Package com. OA. Data. Dao. impl;
Import java. util. arraylist;
Import java. util. List;
Import net. SF. hibernate. transaction;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
Import org. hibernate. lockmode;
Import org. hibernate. criterion. example;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
Import com. OA. Data. Bo. user;
Import com. OA. Data. Dao. iuserdao;
/**
* Data Access Object (DAO) for domain model class user.
* @ See. User
* @ Author myeclipse-hibernate tools
*/
Public class userdao extends hibernatedaosupport implements iuserdao {
Private Static final log = logfactory. getlog (userdao. Class );
Protected void initdao (){
// Do nothing
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # Save (COM. OA. Data. Bo. User)
*/
Public void save (User transientinstance ){
Log. debug ("Saving user instance ");
Try {
This. gethibernatetemplate (). saveorupdate (transientinstance );
Log. debug ("Save successful ");
} Catch (runtimeexception re ){
Log. Error ("save failed", RE );
Throw re;
}
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # Delete (COM. OA. Data. Bo. User)
*/
Public void Delete (User persistentinstance ){
Log. debug ("deleting user instance ");
Try {
Gethibernatetemplate (). Delete (persistentinstance );
Log. debug ("delete successful ");
} Catch (runtimeexception re ){
Log. Error ("delete failed", RE );
Throw re;
}
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # findbyid (Java. Lang. integer)
*/
Public user findbyid (Java. Lang. Integer ID ){
Log. debug ("getting user instance with ID:" + id );
Try {
User instance = (User) gethibernatetemplate ()
. Get ("user", ID );
Return instance;
} Catch (runtimeexception re ){
Log. Error ("Get failed", RE );
Throw re;
}
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # findbyexample (COM. OA. Data. Bo. User)
*/
Public list findbyexample (user instance ){
Log. debug ("finding user instance by example ");
Try {
List Results = getsession ()
. Createcriteria ("user ")
. Add (example. Create (Instance ))
. List ();
Log. debug ("find by example successful, result size:" + results. Size ());
Return results;
} Catch (runtimeexception re ){
Log. Error ("find by example failed", RE );
Throw re;
}
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # merge (COM. OA. Data. Bo. User)
*/
Public user merge (User detachedinstance ){
Log. debug ("merging user instance ");
Try {
User result = (User) gethibernatetemplate ()
. Merge (detachedinstance );
Log. debug ("merge successful ");
Return result;
} Catch (runtimeexception re ){
Log. Error ("merge failed", RE );
Throw re;
}
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # attachdirty (COM. OA. Data. Bo. User)
*/
Public void attachdirty (user instance ){
Log. debug ("attaching dirty user instance ");
Try {
Gethibernatetemplate (). saveorupdate (instance );
Log. debug ("Attach successful ");
} Catch (runtimeexception re ){
Log. Error ("Attach failed", RE );
Throw re;
}
}
/* (Non-javadoc)
* @ See COM. OA. Data. Dao. impl. iuserdao # attachclean (COM. OA. Data. Bo. User)
*/
Public void attachclean (user instance ){
Log. debug ("attaching clean user instance ");
Try {
Gethibernatetemplate (). Lock (instance, lockmode. None );
Log. debug ("Attach successful ");
} Catch (runtimeexception re ){
Log. Error ("Attach failed", RE );
Throw re;
}
}
Public static iuserdao getfromapplicationcontext (applicationcontext CTX ){
Return (iuserdao) CTX. getbean ("userdao ");
}
Public user findbyusername (string username ){
Log. debug ("getting user instance with Username:" + username );
Try {
List list = gethibernatetemplate (). Find (
"From user as u where username =? ", Username );
If (list. Size ()> 0 ){
User instance = (User) list. Get (0 );
Return instance;
} Else {
Return NULL;
}
} Catch (runtimeexception re ){
Log. Error ("Get failed", RE );
Throw re;
}
}
Public list findwithpage (INT pagesize, int startrow ){
List list = new arraylist ();
Transaction Tx = NULL;
Log. debug ("showalluser ");
Try {
Org. hibernate. query Q;
Q = This. getsessionfactory (). opensession (). createquery ("from user ");
Q. setfirstresult (startrow );
Q. setmaxresults (pagesize); // hibernate simple paging settings, set the number of records on the Start row and each page
List = Q. List ();
} Catch (runtimeexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return list;
}
Public int counter (){
Int counter = 0;
Transaction Tx = NULL;
Log. debug ("counter ");
Counter = (integer) This. gethibernatetemplate (). iterate ("select count (*) from user"). Next (). intvalue ();
Return counter;
}
}
4. Because spring is used and Dao Service proxy is required, it is necessary to implement the service class. Service package:
Package com. OA. model. Service;
Import java. util. List;
Import com. OA. Data. Bo. user;
Public interface iuserservice {
// Verify that the user is valid and return a user object
Public user isvaliduser (string username, string password );
Public list findwithpage (INT pagesize, int startrow );
Public int counter ();
}
5. A service interface has an implementation class: In the service. IMP package:
Package com. OA. model. Service. impl;
Import java. util. arraylist;
Import java. util. List;
Import com. OA. Data. Bo. user;
Import com. OA. Data. Dao. iuserdao;
Import com. OA. model. Service. iuserservice;
Public class userservice implements iuserservice {
Private iuserdao userdao;
Public iuserdao getuserdao (){
Return userdao;
}
Public void setuserdao (iuserdao userdao ){
This. userdao = userdao;
}
Public user isvaliduser (string username, string password ){
// Todo auto-generated method stub
User user = userdao. findbyusername (username );
If (user = NULL)
Return NULL;
If (user. getuserpwd (). Equals (password ))
Return user;
Return NULL;
}
Public list findwithpage (INT pagesize, int startrow ){
// Todo auto-generated method stub
Arraylist list = new arraylist ();
List = (arraylist) userdao. findwithpage (pagesize, startrow );
Return list;
}
Public int counter (){
// Todo auto-generated method stub
Int counter = 0;
Counter = userdao. Counter ();
Return counter;
}
}