SSH
来源:互联网 发布:什么值得买app源码 编辑:程序博客网 时间:2024/06/04 01:15
【20150407】使用spring注入Hibernate的sessionFactory
Hibernate没有自己的配置文件,使用spring的applicationContext.xml进行配置
方法1:
applicationContext.xml不需要特别配置
在xxxAction里:
private SessionFactory sessionFactory;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;} 。。。。。。 Session session=sessionFactory.openSession();<span style="white-space:pre"></span>String hql="from User u where u.userName=? and u.userPass=?";<span style="white-space:pre"></span>Query query=session.createQuery(hql);同样的代码在DAO中无法使用,报错session为空
方法2:
applicationContext中,
<!-- 配置hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean><bean id="rightColumnDAOImpl" class="com.ccit.dao.impl.RightColumnDAOImpl"><property name="hibernateTemplate" ref="hibernateTemplate" /></bean>xxxDAO IMPL,
public class RightColumnDAOImpl extends SuperDaoSupport implements RightColumnDAO { private static HibernateTemplate hibernateTemplate;public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;} 。。。。。。 List<RightColumn> list = this.getHibernateTemplate().find(hql);。。。
----------此方法需要每个xxxDAO IMPL类都 先声明HibernateTemplate,不如方法3更方便
方法3:
先同方法2配置applicationContext,public class SuperDaoSupport{ @Resource private static HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } }xxxDAO IMPL
public class RightColumnDAOImpl extends SuperDaoSupport implements RightColumnDAO {。。。 List<RightColumn> list = this.getHibernateTemplate().find(hql);。。。}
实现DAO类也可:
Session session = this.getHibernateTemplate().getSessionFactory().openSession();Query query = session.createQuery(hql);List<RightColumn> list = query.list();或者:
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();此报错:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
---------全部extends父类,无法 再继承其它类
?方法4:
其它都同方法3,xxxDAO IMPL中代码
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();Query query = session.createQuery(hql);List<RightColumn> list = query.list();报错:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
6、
getCurrentSession 与 openSession() 的区别
http://blog.csdn.net/loveyout/article/details/4193894
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭
0 0
- SSH...
- ssh
- ssh
- ssh
- SSH
- ssh
- SSH
- ssh
- SSH
- SSH
- SSH
- ssh
- ssh
- ssh
- SSH
- ssh
- ssh
- SSH
- 【USACO3.3.5】一个游戏
- Solr笔记
- DataBaseMeataData--元数据
- BZOJ1176【CDQ分治】【树状数组】
- Java 中hashCode & Equals方法
- SSH
- 多线程通信的小例子
- leetcode003:Longest Substring Without Repeating Characters
- 【USACO3.4.1】闭合的栅栏
- 简单明了:android和IOS学习线路图
- session和cookie
- AJAX总结
- swt学习之WEBKIT的Safari问题
- jQuery1.9+ 废弃的函数和方法 升级Jquery版本遇到的问题