知识大全 使用模板模式简化DAO操作Hibernate
Posted 知
篇首语:学乃身之宝,儒为席上珍。君看为宰相,必用读书人。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 使用模板模式简化DAO操作Hibernate相关的知识,希望对你有一定的参考价值。
使用模板模式简化DAO操作Hibernate 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
相信使用过Spring + Hibernate开发过的人 在写DAO的时候都使用过Spring的HibernateDaoSupport类 然后在实现的时候就可以很轻松的使用getHibernateTemplate()方法之后就可以调用save() delete() update()等Hibernate的Session的操作 很简单 比如 getHibernateTemplate() save(user); 这样一句话在我们没有Spring的时候就必须使用如下的代码才能完成 Session session = HibernateUtil getSession(); Transaction tx = session beginTransaction(); session save(user); mit(); lseSession(); 这里还省去了异常处理 同时使用了HibernateUtil类来简化从SessionFactory获取Session 以及关闭Session等处理 但是我们在使用Hibernate的时候不一定会使用Spring 所以我们可以模仿Spring的处理方式 做一个Hibernate的模板 使用模板模式来简化我们的开发 其主要的目的就是为了简化开发 使代码达到最大话的重用 .我们现来实现一个Hibernate模板 package kick hibernate; import net sf hibernate HibernateException; import net sf hibernate Session; import net sf hibernate Transaction; public class HibernateTemplate public static Object run(HibernateCallback callback) throws HibernateException Session session = null; Transaction tx = null; try session = HibernateSessionutil currentSession(); tx = session beginTransaction(); Object result = callback execute(session); mit(); session flush(); return result; catch (HibernateException e) tx rollback(); return null; finally HibernateSessionutil closeSession(); 这里类很简单 就是使用一个实现HibernateCallBack接口的一个回掉类 在调用的时候根据具体的需求实现HibernateCallBack类 .回掉接口HibernateCallBack package kick hibernate; import net sf hibernate HibernateException; import net sf hibernate Session; public interface HibernateCallBack Object execute(Session session)throws HibernateException; 好了 到此为止我们就可以使用这个模板了 可以用如下的方式使用 HibernateTemplate run(new HibernateCallback() public Object execute(Session session) throws HibernateException session save(user); return null; ); 看看 是不是省去了很多代码?^_^ 不过这还没有达到想Spring里面那样简单 不要着急 面包会有的 呵呵 我们会达到的 .实现我们自己的HibernateSupport类 从上面的代码可以看出 我们要自己实现HibernateCallback接口 而每次我们实现的时候又重复代码了 因此我们再抽象 讲这些实现放到我们的HibernateSupport类里面去 看看我们上面的代码就知道我们实现HibernateCallback接口的目的就是为了调用session save()方法 即session的方法 代码如下 package kick hibernate; import java io Serializable; import net sf hibernate HibernateException; import net sf hibernate Session; public class HibernateSupport public Object save(final Object object) throws HibernateException return HibernateTemplate run(new HibernateCallBack() public Object execute(Session session) throws HibernateException session save(object); return null; ); public Object save(final Object object final Serializable id) throws HibernateException return HibernateTemplate run(new HibernateCallBack() public Object execute() throws HibernateException session save(object id); return null; ); public Object saveOrUpdate(final Object object) throws HibernateException return HibernateTemplate run(new HibernateCallBack() public Object execute(Session session) throws HibernateException session saveOrUpdate(object); return null; ); …………………………………………………………………………………… …………………………………………………………………………………… …………………………………………………………………………………… 调用一些其他的session的方法 .抽象RootDao 该类为抽象类 在实现自己的DAO类的时候继承该类 该类的有一个HibernateSupport的对象 在子类中使用getHibernateTemplate()方法就可以得到该对象 然后调用它对应的方法 实现代码如下 package kick hibernate dao; import net sf hibernate Session; import kick hibernate HibernateTemplateImpl; public abstract class RootDao private HibernateSupport temp = null; /** * @return Returns the temp */ public HibernateTemplateImpl getHibernateTemplate(Session session) return new HibernateSupport(); .使用例子 定义一个自己的DAO类 实现代码如下 public class UserDaoImpl extends RootDao implements UserDaoInterface public void saveUser(User user) throws KickException getHibernateTemplate() saveOrUpdate(user); …………………………………………………………………………………… 实现其他的方法 …………………………………………………………………………………… 看到没有?红色的代码 就实现了Spring的HibernateSupport了吧?^_^ cha138/Article/program/Java/ky/201311/28277相关参考