今天终于实现了hibernate实现分页了。。把oksonic的SSH的例子做了增加,实现了查询的分页。
同时现在也把srtuts + spring + hibernate 结合的实现方法重新复习了一下,总结如下:
1 首先肯定是要把对应的数据库的表进行映射的,把影射类和表的映射XML文件放到包bo中:
包含文件AbstractUser.java User.java User.hbm.xml
表的映射配置文件如下:
<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 写DAO数据访问接口IUserDAO .java,放在dao包中:
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 写DAO数据访问实现类 UserDAO .java,放在dao.imp包中:
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 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简单分页的设置,设置起始行和每页的记录数
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 因为用到了spring,需要做DAO服务代理,所以有必要实现服务类。service包中:
package com.oa.model.service;
import java.util.List;
import com.oa.data.bo.User;
public interface IUserService {
// 验证用户是否合法,并返回一个 User 对像
public User isValidUser(String username,String password);
public List findWithPage(int pageSize, int startRow);
public int counter();
}
5 有服务接口就有实现类:service.imp包中:
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;
}
}