SSH

【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必须手动关闭



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值