spring中的HibernateDaoSupport的用法(Session是如何获取的)
2015-03-10 20:53
465 查看
HibernateDaoSupport中自带了hibernate的模板供编程者使用。
前提条件:你的类必须继承HibernateDaoSupport
一:
回调函数:
public List getList(){
return (List )getHibernateTemplate().execute(
new HibernateCallback() {
public List doInHibernate(final Session s)
throws HibernateException, SQLException {
StringBuffer sb = new StringBuffer();
Query query =s.createSQLQuery(sb.toString());//<STRONG>执行普通sql</STRONG>
Query query = s.createQuery(sb.toString());//<STRONG>执行hql</STRONG>
List list = query.list();
return list ;
}
});
}采用这种做法的好处是:不用关心事务。session的创建和销毁,一切都在程序内部完成。看看名字doInHibernate就知道好处了。不好的是程序看起来比较乱!这也是我用spring最乱的地方,初学者看起来太乱了。
二:
getHibernateTemplate().find();
List list = getHibernateTemplate().find(String hql);
这里传进来的是hql不是sql。
三:
自己获取session处理。
获取session的两种方式:this.getSession();和this.getHibernateTemplate().getSessionFactory().openSession();
获取session以后再createQuery操作就一样了。但是这种做法,是需要自己去手动关闭session的。所以你需要配置openSessioninview。
其他:
getHibernateTemplate.delete(Object);
getHibernateTemplate.save(Object);
getHibernateTemplate.update(Object);
总结:推荐使用回调函数。
前提条件:你的类必须继承HibernateDaoSupport
一:
回调函数:
public List getList(){
return (List )getHibernateTemplate().execute(
new HibernateCallback() {
public List doInHibernate(final Session s)
throws HibernateException, SQLException {
StringBuffer sb = new StringBuffer();
Query query =s.createSQLQuery(sb.toString());//<STRONG>执行普通sql</STRONG>
Query query = s.createQuery(sb.toString());//<STRONG>执行hql</STRONG>
List list = query.list();
return list ;
}
});
}采用这种做法的好处是:不用关心事务。session的创建和销毁,一切都在程序内部完成。看看名字doInHibernate就知道好处了。不好的是程序看起来比较乱!这也是我用spring最乱的地方,初学者看起来太乱了。
二:
getHibernateTemplate().find();
List list = getHibernateTemplate().find(String hql);
这里传进来的是hql不是sql。
三:
自己获取session处理。
获取session的两种方式:this.getSession();和this.getHibernateTemplate().getSessionFactory().openSession();
获取session以后再createQuery操作就一样了。但是这种做法,是需要自己去手动关闭session的。所以你需要配置openSessioninview。
其他:
getHibernateTemplate.delete(Object);
getHibernateTemplate.save(Object);
getHibernateTemplate.update(Object);
总结:推荐使用回调函数。
相关文章推荐
- <?>关于spring里HibernateDaoSupport的关于session的获取方法
- Spring 与 Hibernate 的整合DaoImpl中Session如何获取
- spring在service层和dao层如何获取request和session
- spring中的HibernateDaoSupport的用法
- spring中的HibernateDaoSupport的用法
- spring中的HibernateDaoSupport的用法
- spring中的HibernateDaoSupport的用法
- hibernate使用spring的事务管理器在测试dao的时候获取不到session
- 当Dao层继承了HibernateDaoSupport,使用底层SQL语句,session获取的方法,以及解决关联查询no session的问题
- 继承HibernateDaoSupport后怎样获取session&&注入sessionFactory
- Spring 3 Annotations - HibernateDaoSupport - Repository Requires Session Factory
- 由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持
- Session、SessionFactory、HibernateTemplate实例是如何从HibernateDaoSupport抽象类实例取出
- 由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持
- Spring与Hibernate整合的HibernateDaoSupport应用中的“内存泄露”问题(学习心得)
- Spring中使用HibernateDaoSupport来进行数据库的操作
- 如何通过Spring获取到Hibernate的Configuration对象以及Table,Column对象
- Spring结合Hibernate出现的异常The type org.springframework...DaoSupport cannot be resolved. It is indirectly...(
- hibernate 与spring结合,hibernatedaoSupport使用注意事项
- [醒目]Spring入门Blog[十五、Spring中对于HibernateDaoSupport的封装][附带练习项目地址]