使用模板模式简化DAO操作Hibernate

本文介绍了在Spring+Hibernate开发中,Spring的HibernateDaoSupport类可简化Hibernate的Session操作。同时提出在不使用Spring时,可模仿其处理方式,通过实现Hibernate模板、回调接口、HibernateSupport类、抽象RootDao等步骤,简化Hibernate开发,实现代码重用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相信使用过Spring+Hibernate开发过的人,在写DAO的时候都使用过Spring的HibernateDaoSupport类,然后在实现的时候就可以很轻松的使用getHibernateTemplate()方法之后就可以调用save()、delete()、update()等Hibernate的Session的操作,很简单。比如:
getHibernateTemplate().save(user);
这样一句话在我们没有Spring的时候就必须使用如下的代码才能完成:
Sessionsession=HibernateUtil.getSession();
Transactiontx=session.beginTransaction();
session.save(user);
tx.commit();
HibernateUtil.colseSession();
这里还省去了异常处理,同时使用了HibernateUtil类来简化从SessionFactory获取Session,以及关闭Session等处理。
但是我们在使用Hibernate的时候不一定会使用Spring,所以我们可以模仿Spring的处理方式,做一个Hibernate的模板,使用模板模式来简化我们的开发,其主要的目的就是为了简化开发,使代码达到最大话的重用。
1.我们现来实现一个Hibernate模板:
packagekick.hibernate;

importnet.sf.hibernate.HibernateException;
importnet.sf.hibernate.Session;
importnet.sf.hibernate.Transaction;

publicclassHibernateTemplate{
publicstaticObjectrun(HibernateCallbackcallback)throwsHibernateException{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateSessionutil.currentSession();
tx=session.beginTransaction();
Objectresult=callback.execute(session);
tx.commit();
session.flush();
returnresult;
}catch(HibernateExceptione){
tx.rollback();
returnnull;
}finally{
HibernateSessionutil.closeSession();
}
}
这里类很简单,就是使用一个实现HibernateCallBack接口的一个回掉类,在调用的时候根据具体的需求实现HibernateCallBack类。
2.回掉接口HibernateCallBack:
packagekick.hibernate;

importnet.sf.hibernate.HibernateException;
importnet.sf.hibernate.Session;

publicinterfaceHibernateCallBack{
Objectexecute(Sessionsession)throwsHibernateException;
}
好了,到此为止我们就可以使用这个模板了,可以用如下的方式使用:
HibernateTemplate.run(newHibernateCallback(){
publicObjectexecute(Sessionsession)throwsHibernateException{
session.save(user);
returnnull;
}
});
看看,是不是省去了很多代码?^_^
不过这还没有达到想Spring里面那样简单,不要着急,“面包会有的”呵呵,我们会达到的。
3.实现我们自己的HibernateSupport类:
从上面的代码可以看出,我们要自己实现HibernateCallback接口,而每次我们实现的时候又重复代码了。因此我们再抽象,讲这些实现放到我们的HibernateSupport类里面去。看看我们上面的代码就知道我们实现HibernateCallback接口的目的就是为了调用session.save()方法,即session的方法。代码如下:
packagekick.hibernate;

importjava.io.Serializable;

importnet.sf.hibernate.HibernateException;
importnet.sf.hibernate.Session;

publicclassHibernateSupport{

publicObjectsave(finalObjectobject)throwsHibernateException{
returnHibernateTemplate.run(newHibernateCallBack(){

publicObjectexecute(Sessionsession)throwsHibernateException{
session.save(object);
returnnull;
}

});
}
publicObjectsave(finalObjectobject,finalSerializableid)throwsHibernateException{
returnHibernateTemplate.run(newHibernateCallBack(){

publicObjectexecute()throwsHibernateException{
session.save(object,id);
returnnull;
}

});
}

publicObjectsaveOrUpdate(finalObjectobject)throwsHibernateException{
returnHibernateTemplate.run(newHibernateCallBack(){

publicObjectexecute(Sessionsession)throwsHibernateException{
session.saveOrUpdate(object);
returnnull;
}

});
}
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………

调用一些其他的session的方法。
}
4.抽象RootDao:
该类为抽象类,在实现自己的DAO类的时候继承该类。该类的有一个HibernateSupport的对象,在子类中使用getHibernateTemplate()方法就可以得到该对象,然后调用它对应的方法。实现代码如下:
packagekick.hibernate.dao;

importnet.sf.hibernate.Session;
importkick.hibernate.HibernateTemplateImpl;

publicabstractclassRootDao{
privateHibernateSupporttemp=null;

/**
*@returnReturnsthetemp.
*/
publicHibernateTemplateImplgetHibernateTemplate(Sessionsession){
returnnewHibernateSupport();
}
}
5.使用例子:
定义一个自己的DAO类,实现代码如下:
publicclassUserDaoImplextendsRootDaoimplementsUserDaoInterface{
publicvoidsaveUser(Useruser)throwsKickException{
getHibernateTemplate().saveOrUpdate(user);
}
……………………………………………………………………………………
实现其他的方法
……………………………………………………………………………………
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值