DAO中对Hibernate的封装

我在程序里也是使用 DAO 模式来封装数据库操作的,这两天仔细的看了这里的文章,也
修改了我的 DAO 的设计,把我的思路和大家共享下:

先介绍下代码(为了方便理解,我把 对参数的检查和异常处理 的相关部分去掉了):

帐户类,一个 PO:

代码
  1. Account {   
  2.     String username;   
  3.     String password;   
  4. }   
<script type="text/javascript">render_code();</script>

 

DAO 接口

代码
  1. interface AccountDAO {   
  2.     public void addAccount( Account account );   
  3.     public void updateAccount( Account account );   
  4.     public void removeAccount( String username );   
  5.     public Account findByUsername( String username );   
  6. }   
<script type="text/javascript">render_code();</script>

 

AccountDAO 的实现

代码
  1. class AccountDAOHibernate {   
  2.     public void addAccount( Account account ) {   
  3.        HibernateUtil.add( account );   
  4.     }   
  5.     public void updateAccount( Account account ) {   
  6.        HibernateUtil.update( account );   
  7.     }   
  8.     public void removeAccount( String username ) {   
  9.        HibernateUtil.remove( Account.class, username );   
  10.     }   
  11.     public Account findByUsername( String username ) {   
  12.        account = (Account) HibernateUtil.findById( Account.class, username );   
  13.     }   
  14. }   
<script type="text/javascript">render_code();</script>

 

HibernateUtil 是对 Hibernate 的基本 crud 操作的封装
我参考了 poll 的实现

代码
  1. class HibernateUtil {   
  2.     public static void add( Object object ) throws HibernateException {   
  3.         Session s = HibernateSessionFactory.currentSession();   
  4.         s.save( object );   
  5.         s.flush();   
  6.     }   
  7.     public static void update( Object object ) throws HibernateException {   
  8.         Session s = HibernateSessionFactory.currentSession();   
  9.         s.saveOrUpdate( object );   
  10.         s.flush();   
  11.     }   
  12.     public static void remove(Class clazz, String id) throws HibernateException {   
  13.         Session s = HibernateSessionFactory.currentSession();   
  14.         Object object = s.load(clazz, id);   
  15.         s.delete( object );   
  16.         s.flush();   
  17.     }   
  18.     public static Object findById( Class clazz, String id ) throws HibernateException {   
  19.         Object obj = null;   
  20.         Session s = HibernateSessionFactory.currentSession();   
  21.         obj = s.load( clazz, id );   
  22.         s.flush();   
  23.         return obj;   
  24.     }   
  25. }   
<script type="text/javascript">render_code();</script>

 

HibernateSessionFactory 使用 ThreadLocal 做 Session 管理,实现和 hibernate 网站上的一样
参考:http://hibernate.bluemars.net/42.html

代码
  1. class HibernateSessionFactory {   
  2.     public static final ThreadLocal session = new ThreadLocal();   
  3.     private static SessionFactory sessionFactory;   
  4.     private static Configuration conf;   
  5.     public static void init() {   
  6.         conf = new Configuration().configure();   
  7.         sessionFactory = conf.buildSessionFactory();   
  8.     }   
  9.     public static Session currentSession() {   
  10.         Session s = (Session) session.get();   
  11.         if (s == null) {   
  12.             s = sessionFactory.openSession();   
  13.             session.set(s);   
  14.         }   
  15.         return s;   
  16.     }   
  17.     public static void closeSession() {   
  18.         Session s = (Session) session.get();   
  19.         session.set(null);   
  20.         if (s != null) s.close();   
  21.     }   
  22. }   
<script type="text/javascript">render_code();</script>

 

说明:
1.DAO 模式的目的是为了把数据库操作封装在 DAO 层下
Session 应该被封装在 DAO 层里

2.何时关闭 Session:
由于使用了 ThreadLocal 来管理 Session,我没有在 DAO 的方法里关闭 Session
这样,同一个 thraed 里的各个 DAO 方法可以访问到 同一个 Session,可以利用
Hibernate 的 lazy load 功能。
在 web 应用程序里可以使用 HibernateFilter 来处理 Session 的关闭:
参考:http://hibernate.bluemars.net/110.html

代码
  1. public class HibernateFilter implements Filter {   
  2.     public void init(FilterConfig filterConfig) throws ServletException {   
  3.         HibernateSessionFactory.init();   
  4.     }   
  5.     public void doFilter( ... ) {   
  6.         try{   
  7.             filterChain.doFilter( servletRequest, servletResponse );   
  8.         } finally {   
  9.             try {   
  10.                 HibernateSessionFactory.closeSession();   
  11.             } catch (HibernateException e) {   
  12.                 log.error( "Error closing current session " + e );   
  13.             }   
  14.         }   
  15.     }   
  16.     public void destroy() {   
  17.     }   
  18. }   
<script type="text/javascript">render_code();</script>


3.对 Transaction 的处理
不在 DAO 层处理 Transaction
如果确实需要 Transaction ,在上一层,比如业务层里的 Session Bean 里来做。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值