Spring整合Hibernate的步骤
  RBCRsXsWXdIg 2023年11月02日 25 0


[code]

为什么要整合Hibernate?
1、使用Spring的IOC功能管理SessionFactory对象
LocalSessionFactoryBean
2、使用Spring管理Session对象
HibernateTemplate
3、使用Spring的功能实现声明式的事务管理

整合Hibernate的步骤:
1、配置SessionFactory(可以自动完成)

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

 <property name="configLocation" 

 value="classpath:hibernate.cfg.xml"> 

 </property> 

 </bean>


2、配置HibernateTemplate,用于完成数据操作

<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate"> 

 <property name="sessionFactory" ref="sessionFactory"></property> 

 </bean>


3、让我们的类继承HibernateDaoSupport类,该类提供了HibernateTemplate的getter和setter方法。

4、将hibernateTemplete注入到我们的类中

<bean id="common" class="我们的类"> 

 <property name="hibernateTemplate" ref="hibernateTemplete"></property> 

 </bean>


5、将我们的类的方法修改成hibernateTemplete的操作。


public class 我们的类 extends HibernateDaoSupport implements ICommon<POJO> { 

 public void insertObject(Object obj) throws Exception { 

 try { 

 this.getHibernateTemplate().save(obj); 

 } catch (Exception e) { 

 e.printStackTrace(); 

 throw new Exception(e); 

 } 

 }



Spring整合Hibernate的步骤 收藏
为什么要整合Hibernate?
1、使用Spring的IOC功能管理SessionFactory对象
LocalSessionFactoryBean
2、使用Spring管理Session对象
HibernateTemplate
3、使用Spring的功能实现声明式的事务管理

整合Hibernate的步骤:
1、配置SessionFactory(可以自动完成)

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

 <property name="configLocation" 

 value="classpath:hibernate.cfg.xml"> 

 </property> 

 </bean>



2、配置HibernateTemplate,用于完成数据操作

<bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate"> 

 <property name="sessionFactory" ref="sessionFactory"></property> 

 </bean>


3、让Common继承HibernateDaoSupport类,该类提供了HibernateTemplate的getter和setter方法。

4、将hibernateTemplete注入到Common中

<bean id="common" class="com.aptech.common.Common"> 

 <property name="hibernateTemplate" ref="hibernateTemplete"></property> 

 </bean>


5、将Common的方法修改成hibernateTemplete的操作。
package com.aptech.common;

import java.sql.SQLException; 

import java.util.List; 


import org.hibernate.HibernateException; 

import org.hibernate.Session; 

import org.springframework.dao.DataAccessException; 

import org.springframework.orm.hibernate3.HibernateCallback; 

import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 


import com.aptech.exception.CommonException; 

import com.aptech.htm.HibernateSessionFactory; 

/** 

 * 通用类,不再负责事务处理 

 * 目标对象 

 * @author 李赞红 

 * 

 * @param <POJO> 

 */ 

public class Common<POJO> extends HibernateDaoSupport implements ICommon<POJO> { 

 public void insertObject(POJO pojo) throws CommonException { 

 try { 

 this.getHibernateTemplate().save(pojo); 

 } catch (DataAccessException e) { 

 e.printStackTrace(); 

 throw new CommonException(e); 

 } 

 } 


 public void updateObject(POJO pojo) throws CommonException { 

 try { 

 this.getHibernateTemplate().update(pojo); 

 } catch (DataAccessException e) { 

 e.printStackTrace(); 

 throw new CommonException(e); 

 } 

 } 


 public void deleteObject(Class theClass, long id) throws CommonException { 

 try { 

 Object obj = this.getHibernateTemplate().load(theClass, id); 

 this.getHibernateTemplate().delete(obj); 

 } catch (DataAccessException e) { 

 // TODO Auto-generated catch block 

 e.printStackTrace(); 

 throw new CommonException(e); 

 } 

 } 


 public POJO loadObject(Class theClass, long id) throws CommonException { 

 try { 

 Object obj = this.getHibernateTemplate().load(theClass, id); 

 return (POJO) obj; 

 } catch (DataAccessException e) { 

 // TODO Auto-generated catch block 

 e.printStackTrace(); 

 throw new CommonException(e); 

 } 

 } 


 public List queryObjects(final String hql) throws CommonException { 

 class Hc implements HibernateCallback{ 

 public Object doInHibernate(Session session) 

 throws HibernateException, SQLException { 

 return session.createQuery(hql).list(); 

 } 

 } 


 try { 

 return this.getHibernateTemplate().executeFind(new Hc()); 

 } catch (DataAccessException e) { 

 e.printStackTrace(); 

 throw new CommonException(e); 

 } 

 } 


 public List queryObjects(Class theClazz) throws CommonException { 

 return this.queryObjects("from " + theClazz.getSimpleName()); 

 } 

}


6、配置事务
<!-- 事务管理器,相当于TransactionProxy,定义事务的开启、提交、回滚 -->

<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 

 <property name="sessionFactory"> 

 <ref bean="sessionFactory"/> 

 </property> 

 </bean> 

 <bean id="ti" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 

 <property name="transactionManager"> 

 <ref bean="htm"/> 

 </property> 

 <property name="transactionAttributes"> 

 <props> 

 <!-- key:方法名称 --> 

 <prop key="insert*">PROPAGATION_REQUIRED</prop> 

 <prop key="update*">PROPAGATION_REQUIRED</prop> 

 <prop key="delete*">PROPAGATION_REQUIRED</prop> 

 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> 

 <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> 

 </props> 

 </property> 

 </bean> 


 <!-- 自动代理,代理业务对象 --> 

 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 

 <property name="beanNames"> 

 <list> 

 <value>我们的类</value> 

 </list> 

 </property> 

 <property name="interceptorNames"> 

 <list> 

 <value>ti</value> 

 </list> 

 </property> 

 </bean>


7、将我们的类注入Dao

<bean id="baseDao" abstract="true"> 

 <property name="我们的类"> 

 <ref bean="我们的类"/> 

 </property> 

 </bean> 


 <bean id="udao" class="com.aptech.dao.impl.UserDao" parent="baseDao"></bean> 

 <bean id="rdao" class="com.aptech.dao.impl.RoleDao" parent="baseDao"></bean>



8、将Dao注入Service

<bean id="grantService" class="com.aptech.service.impl.GrantService"> 

 <property name="rdao" ref="rdao"></property> 

 <property name="udao" ref="udao"></property> 

 </bean> 


[/code]

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
RBCRsXsWXdIg