SSH(struts+spring+hibernate)迅速开发--第四章 改造DAO(2)
来源:互联网 发布:mac证书信任在哪 编辑:程序博客网 时间:2024/05/20 04:25
这个类,基本实现了适合所有查询指令的方法,包括分页和联合查询,然后将自动生成的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");
}
}
- SSH(struts+spring+hibernate)迅速开发--第四章 改造DAO(2)
- SSH(struts+spring+hibernate)迅速开发--第四章 改造DAO(1)
- SSH(struts+spring+hibernate)迅速开发--第二章 SSH框架搭建(2)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(2)
- SSH(struts+spring+hibernate)迅速开发--第七章 用户注册的表单校验(2)
- SSH(struts+spring+hibernate)迅速开发--第八章 浏览和选购商品(2)
- SSH(struts+spring+hibernate)迅速开发--第九章 下订单(2)
- SSH(struts+spring+hibernate)迅速开发--第十章 记录日志(2)
- SSH(struts+spring+hibernate)迅速开发--附录2 Struts 常用标签&国际化(2)
- SSH(struts+spring+hibernate)迅速开发--附录1 Struts校验框架(2)
- SSH(struts+spring+hibernate)迅速开发--附录2 Struts 常用标签&国际化(1)
- SSH(struts+spring+hibernate)迅速开发--第二章 SSH框架搭建(1)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(1)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(3)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(4)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(5)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(6)
- SSH(struts+spring+hibernate)迅速开发--第六章 用户登陆和注册(7)
- 弹出窗口参考--IE Firefox下通用
- 基于Linux平台的libpcap源代码分析b
- 基于Linux平台的libpcap源代码分析c
- SSH(struts+spring+hibernate)迅速开发--第四章 改造DAO(1)
- 基于Linux平台的libpcap源代码分析d
- SSH(struts+spring+hibernate)迅速开发--第四章 改造DAO(2)
- script 获取客户端页面信息
- 2008年最热门的七大IT软件技能
- asp.net发送邮件
- 邮箱与论坛的数据恢复
- 用ini文件对基于桌面型数据库(Access)程序的数据库定位方式
- 句柄概念
- tomcat error page
- C#中自定义操作INI文件的类