spring整合hibernate,主要达到的目的有以下几点
1、使用Spring的IOC功能管理SessionFactory对象 --LocalSessionFactoryBean
对于SessionFactory对象的管理可以分为两类,一类是通过spring直接管理,另一类是通过hibernate自身的配置文件hibernate.cfg.xml进行配置,通过spring配置文件引入到spring框架中。
2、使用Spring管理Session对象 -- HibernateTemplate
对于HibernateTemplate的配置也有两种情况,一种是在spring中显示的配置hibernate操作模板类HibernateTemplate,程序中声明HibernateTemplate属性,并在spring中注入该属性。
private HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; }
<!-- 配置hibernate操作模板类 --> <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<!-- 向dao中注入hibernateTemplate模板对象 --> <bean name="hibernateDao" class="com.hibernate.dao.impl.HibernateDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean>
另一种情况是不在spring配置文件中声明HibernateTemplate,程序中不必声明HibernateTemplate,而是继承HibernateDaoSupport类。采用这种方法之后,在使用时只需向程序中注入SessionFactory即可。
java类如下,继承HibernateDaoSupport类,程序中不必声明HibernateTemplate,也不必声明SessionFactory,spring配置文件中直接向该类中注入SessionFactory。(被继承的HibernateDaoSupport中声明了SessionFactory属性)
public class ListMPByGroupIdDao extends HibernateDaoSupport implements IListMPByGroupIdDao {...}
配置文件如下
<!-- 向dao中注入SessionFactory --> <bean id="listMPByGroupIdDao" class="com.ai.rms.dao.impl.ListMPByGroupIdDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
3、使用Spring的功能实现声明式的事务管理
接下来按照对SessionFactory对象的两种管理方法列出详细配置,首先是通过spring管理SessionFactory
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!-- 配置hibernate操作模板类 --> <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置sessionFactory对象 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="MydataSource"></property> <property name="mappingLocations"> <list> <value>com/hibernate/bean/User.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- 配置数据源 --> <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" name="MydataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:xe</value> </property> <property name="username"> <value>aduser</value> </property> <property name="password"> <value>123456</value> </property> </bean> <!-- 向dao中注入hibernateTemplate模板对象 --> <bean name="hibernateDao" class="com.hibernate.dao.impl.HibernateDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 构建service对象 --> <bean name="service" class="com.hibernate.service.impl.UserServiceImpl"> <property name="dao" ref="hibernateDao"></property> </bean> <!-- 配置事务管理器 --> <bean name="tranManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务拦截器 --> <bean name="tranInterceptor" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="tranManager"></property> <property name="transactionAttributes"> <props> <!-- 事务传播属性,事务隔离级别, 方法属性值,控制提交回滚操作 (+Exception强制提交,-Exception回滚)--> <!-- <prop key="*">PROPAGATION_REQUIRED,,,</prop> --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> <property name="target" ref="service"></property> </bean> </beans>
使用hibernate自身的配置文件hibernate.cfg.xml配置SessionFactory时,在spring配置文件中不必声明数据源datasource 配置如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!-- 配置hibernate操作模板类 --> <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置sessionFactory对象 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocations"> <list> <value>classpath:hibernate.cfg.xml</value> </list> </property> </bean> <!-- 向dao中注入hibernateTemplate模板对象 --> <bean name="hibernateDao" class="com.hibernate.dao.impl.HibernateDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 构建service对象 --> <bean name="service" class="com.hibernate.service.impl.UserServiceImpl"> <property name="dao" ref="hibernateDao"></property> </bean> <!-- 配置事务管理器 --> <bean name="tranManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务拦截器 --> <bean name="tranInterceptor" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="tranManager"></property> <property name="transactionAttributes"> <props> <!-- 事务传播属性,事务隔离级别, 方法属性值,控制提交回滚操作 (+Exception强制提交,-Exception回滚)--> <!-- <prop key="*">PROPAGATION_REQUIRED,,,</prop> --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> <property name="target" ref="service"></property> </bean> </beans>
hibernate.cfg.xml的配置如下
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="myeclipse.connection.profile">runnew</property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:XE </property> <property name="connection.username">aduser</property> <property name="connection.password">123456</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <mapping resource="com/hibernate/bean/User.hbm.xml" /> </session-factory> </hibernate-configuration>