自动生成的hibernate DAO的几个方法

自动生成的hibernate DAO的几个方法

save

delete

findbyid 按ID查询数据.

findbyexample 相当于"select * from Usertable"实现的功能就是查询所有
* 数据.

findbyproperty  用来灵活的提供一种按条件查询的方法,你可以自己定义要按什么样的方
* 式查询.

* save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法
* 但你可以简单的把save()方法改成具有Update功能:将getSession().save
* (transientInstance);这句改成
* getSession().merge(transientInstance);或者getSession().saveOrUpdate
* (transientInstance);

public class SignupDAO extends HibernateDaoSupport {

private static final Logger log = LoggerFactory.getLogger(SignupDAO.class);
// property constants
public static final String USERNAME = "username";
public static final String PASSWORD = "password";


protected void initDao() {
// do nothing
}


public void save(Signup transientInstance) {
log.debug("saving Signup instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}


public void delete(Signup persistentInstance) {
log.debug("deleting Signup instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}


public Signup findById(int id) {
System.out.println(id);
log.debug("getting Signup instance with id: " + id);
try {
Signup instance = (Signup) getHibernateTemplate().get(
"com.jjbond.model.Signup", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(Signup instance) {
log.debug("finding Signup 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 Signup instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Signup as model where model."
+ propertyName + "= ?";
System.out.println(log);
// HibernateTemplate ht = new HibernateTemplate();
// System.out.println(ht);
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(USERNAME, username);
}


public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}


public List findAll() {
log.debug("finding all Signup instances");
try {
String queryString = "from Signup";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}


public Signup merge(Signup detachedInstance) {
log.debug("merging Signup instance");
try {
Signup result = (Signup) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}


public void attachDirty(Signup instance) {
log.debug("attaching dirty Signup instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}


public void attachClean(Signup instance) {
log.debug("attaching clean Signup instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}


public static SignupDAO getFromApplicationContext(ApplicationContext ctx) {
return (SignupDAO) ctx.getBean("SignupDAO");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值