这个类,基本实现了适合所有查询指令的方法,包括分页和联合查询,然后将自动生成的DAO类,改成基础该类,这样,就完成了每个DAO类的功能改造.如下是修改后的TuserDAO.java,注意粗体部分.
package cn.com.book.demo.hibernate.dao;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Data access object (DAO) for domain model class Tuser.
*
* @see cn.com.book.demo.hibernate.dao.Tuser
* @author MyEclipse Persistence Tools
*/
public class TuserDAO extends BaseDAO {
private static final Log log = LogFactory.getLog(TuserDAO.class);
// property constants
public static final String USER_NAME = "userName";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String PHONE = "phone";
public static final String PASSWORD = "password";
protected void initDao() {
// do nothing
}
public void save(Tuser transientInstance) {
log.debug("saving Tuser instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Tuser persistentInstance) {
log.debug("deleting Tuser instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Tuser findById(java.lang.Integer id) {
log.debug("getting Tuser instance with id: " + id);
try {
Tuser instance = (Tuser) getHibernateTemplate().get(
"cn.com.book.demo.hibernate.dao.Tuser", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Tuser instance) {
log.debug("finding Tuser instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Tuser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Tuser as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByUserName(Object userName) {
return findByProperty(USER_NAME, userName);
}
public List findByName(Object name) {
return findByProperty(NAME, name);
}
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
public List findByPhone(Object phone) {
return findByProperty(PHONE, phone);
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List findAll() {
log.debug("finding all Tuser instances");
try {
String queryString = "from Tuser";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Tuser merge(Tuser detachedInstance) {
log.debug("merging Tuser instance");
try {
Tuser result = (Tuser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Tuser instance) {
log.debug("attaching dirty Tuser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Tuser instance) {
log.debug("attaching clean Tuser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static TuserDAO getFromApplicationContext(ApplicationContext ctx) {
return (TuserDAO) ctx.getBean("TuserDAO");
}
}