Today finally realized the hibernate implementation paging. The example of Oksonic ssh is added to implement the paging of the query.
At the same time now also srtuts + Spring + hibernate combination of the implementation of a review, summed up as follows:
1 First of all must be to map the corresponding database table, the mapping of the mappings of Class and table XML file into the packet bo:
Include file Abstractuser.java User.java User.hbm.xml
The mapping configuration file for the table is as follows:
<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= "not-null=" "true"/>
</property>
<property name= "Userpwd" type= "string" >
<column name= "userpwd" length= "not-null=" "true"/>
</property>
</class>
2 Write DAO data access interface Iuserdao. java, put in 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, placed in the 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 () {
Doing 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, setting the starting row and the number of records per 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 needed to be a DAO Service Broker, it is necessary to implement a service class. In 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 legitimate and returns an image
Public User isvaliduser (String username,string password);
Public List findwithpage (int pageSize, int startrow);
public int counter ();
}
5 has the service interface has the implementation class: 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;
}
}