用模板模式简化DAO操作Hibernate
2006-10-09 00:42
316 查看
相信使用过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);
tx.commit();
HibernateUtil.colseSession();
这里还省去了异常处理,同时使用了HibernateUtil类来简化从SessionFactory获取Session,以及关闭Session等处理。
但是我们在使用Hibernate的时候不一定会使用Spring,所以我们可以模仿Spring的处理方式,做一个Hibernate的模板,使用模板模式来简化我们的开发,其主要的目的就是为了简化开发,使代码达到最大话的重用。
1. 我们现来实现一个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);
tx.commit();
session.flush();
return result;
} catch (HibernateException e) {
tx.rollback();
return null;
} finally {
HibernateSessionutil.closeSession();
}
}
这里类很简单,就是使用一个实现HibernateCallBack接口的一个回掉类,在调用的时候根据具体的需求实现HibernateCallBack类。
2. 回掉接口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里面那样简单,不要着急,“面包会有的”,我们会达到的。
3. 实现我们自己的HibernateSupport类
从上面的代码可以看出,我们要自己实现HibernateCallback接口,而每次我们实现的时候又重复代码了。因此我们再抽象,讲这些实现放到我们的HibernateSupport类里面去。看看我们上面的代码就知道我们实现HibernateCallback接口的目的就是为了调用session.save()方法,即session的方法。代码如下:(点击查看附件)
4. 抽象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();
}
}
5. 使用例子:
定义一个自己的DAO类,实现代码如下:
public class UserDaoImpl extends RootDao implements UserDaoInterface{
public void saveUser(User user) throws KickException {
getHibernateTemplate().saveOrUpdate(user);
}
…………………………………………
实现其他的方法
…………………………………………
}
getHibernateTemplate().save(user);
这样一句话在我们没有Spring的时候就必须使用如下的代码才能完成:
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
HibernateUtil.colseSession();
这里还省去了异常处理,同时使用了HibernateUtil类来简化从SessionFactory获取Session,以及关闭Session等处理。
但是我们在使用Hibernate的时候不一定会使用Spring,所以我们可以模仿Spring的处理方式,做一个Hibernate的模板,使用模板模式来简化我们的开发,其主要的目的就是为了简化开发,使代码达到最大话的重用。
1. 我们现来实现一个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);
tx.commit();
session.flush();
return result;
} catch (HibernateException e) {
tx.rollback();
return null;
} finally {
HibernateSessionutil.closeSession();
}
}
这里类很简单,就是使用一个实现HibernateCallBack接口的一个回掉类,在调用的时候根据具体的需求实现HibernateCallBack类。
2. 回掉接口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里面那样简单,不要着急,“面包会有的”,我们会达到的。
3. 实现我们自己的HibernateSupport类
从上面的代码可以看出,我们要自己实现HibernateCallback接口,而每次我们实现的时候又重复代码了。因此我们再抽象,讲这些实现放到我们的HibernateSupport类里面去。看看我们上面的代码就知道我们实现HibernateCallback接口的目的就是为了调用session.save()方法,即session的方法。代码如下:(点击查看附件)
4. 抽象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();
}
}
5. 使用例子:
定义一个自己的DAO类,实现代码如下:
public class UserDaoImpl extends RootDao implements UserDaoInterface{
public void saveUser(User user) throws KickException {
getHibernateTemplate().saveOrUpdate(user);
}
…………………………………………
实现其他的方法
…………………………………………
}
相关文章推荐
- 用模板模式简化DAO操作Hibernate
- 使用模板模式简化DAO操作Hibernate
- 使用模板模式简化DAO操作Hibernate
- 使用模板模式简化DAO操作Hibernate
- 使用模板模式简化DAO操作Hibernate
- Hibernate中应用dao设计模式进行数据库的增删改查操作
- Struts+Hibernate模板开发笔记---设计模式:DAO
- 使用模板方法模式简化JDBC操作
- 简单就是美 -- 简化hibernate,简化dao
- struts+dao+hibernate模式
- spring入门(12)-spring与hibernate整合完成增删改查的操作(继承HibernateDaoSupport调用hibernateTemplate类)
- 自己扩展封装Hibernate模板DAO-之一简介
- dao注入hibernate模板
- 解析使用enumerator模式简化异步操作的详解
- 继承HibernateDaoSupport进行CURD操作
- j2EE 使用DAO模式对数据库操作(jsp程序)
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- 自定义类获取hibernateDao,实现对数据的操作
- SSH之继承HibernateDaoSupport对数据库的操作和不继承HibernateDaoSupport对数据库的操作的方法
- 模板方法模式(Template Method)-定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。