Spring+Hibernate整合框架下DAO的数据库访问方法

来源:互联网 发布:mlb淘宝代购是真的吗 编辑:程序博客网 时间:2024/05/21 06:18
Spring对Hibernate的DAO实现提供了良好的支持,主要有如下两种方式的DAO实现方法:

第一种:基于Hibernate3.0实现DAO
(一)直接使用Hibernate API(使用openSession())
使用openSession()时,需要注意的是,Session使用完后要手动关闭,如下所示:
Book book = new Book();
book.setName(“thinking in java”);

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(book);
tx.commit();
session.close();


(二)直接使用Hibernate API(使用getCurrentSession())
使用getCurrentSession(),不用手动关闭Session,但需要用try…catch()处理hibernateException异常,并且需要在实现类中加入setSessionFactory(SessionFactory sessionFactory)属性, 以接收依赖注入的SessionFactory。
public class DaoImp implate Dao{
private SessionFactory sessionFactory;
private static String hql = "from User u where u.username=? ";

public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}

public boolean isValidUser(String username) {
try{
List userList = sessionFactory.getCurrentSession().creatQuery(hql).setParameter(0,username).list();
if (userList.size() > 0) {
return true;
}
} catch (HibernateException ex){
throw converHibernaterAccessException(ex);
}
}
}

第二种:继承HibernateDaoSupport (Spring 为Hibernate的DAO提供的工具类)
该类主要提供如下两个方法,方便DAO的实现:
public final HibernateTemplate getHibernateTemplate()
public final void setSessionFactory(SessionFactory sessionFactory)
其中,setSessionFactory方法用来接收Spring的ApplicationContext的依赖注入,可接收配置在文件中的SessionFactory实例。getHibernateTemplate方法则用SessionFactory产生Session,最后生成HibernateTemplate来完成数据库访问。
HibernateTemplate 用于持久层的访问,该模板无需打开Session及关闭Session。它只要获得SessionFactory的引用,就可以打开Session,并在持久化访问结束后关闭Session, 程序开发只需要完成持久层的逻辑,而通用的CRUD操作由HibernateTemplate完成。
(一) 继承 Spring 的 HibernateDaoSupport, 使用 HibernateTemplate
public class DaoImp extend HibernateDaoSupport implates Dao{
private static String hql = "from User u where u.username=? ";

public boolean isValidUser(String username) {
List userList = getHibernateTemplate().find(hql, username);

if (userList.size() > 0) {
return true;
}
}
}


不推荐如下使用Hibernate的getSession()方法:
public boolean isValidUser(String username,String password) throw DataAccessException {
Session session = getSession();
String[] userlist=new String[2];
userlist[0]=username;
userlist[1]=password;

try{
List userList = session.find(hql,userlist);
session.close();

if (userList.size() > 0) {
return true;
}
} catch (HibernateException ex){
throw converHibernaterAccessException(ex);
}
}


(二) 对HibernateTemplate 没有提供的功能, 还可以用HibernateCallback 回调的方法管理数据库访问
public List getListForPage ( final String hql , final int offset , final int length ) {
List list = getHibernateTemplate().executeFind ( new HibernateCallback ( ) {
public Object doInHibernate ( Session session ) throws HibernateException, SQLException {
Query query = session.createQuery ( hql ) ;
query.setFirstResult ( offset ) ;
query.setMaxResults ( length ) ;
List list = query.list ( ) ;
return list ;
}
}) ;
return list ;

}


http://www.topera.com.cn/news/guandian4.asp

1 0
原创粉丝点击