Spring与Hibernate整合关键点:

           1) Hibernate的SessionFactory对象交给Spring创建;

           2) hibernate事务交给spring的声明式事务管理。

 

SH整合步骤:

           1)引入jar包

                     连接池/数据库驱动包

                     Hibernate相关jar

                     Spring 核心包(5个)

                     Spring aop 包(4个)

                     spring-orm-3.2.5.RELEASE.jar            【spring对hibernate的支持】

          spring-tx-3.2.5.RELEASE.jar                           【事务相关】

           2)配置

                     hibernate.cfg.xml

                     bean.xml

           3)搭建环境、单独测试

                    

 

步骤实现

  1 // 数据访问层
  2 public class DeptDao {
  3 
  4     // Spring与Hibernate整合: IOC容器注入
  5     private SessionFactory sessionFactory;
  6     public void setSessionFactory(SessionFactory sessionFactory) {
  7         this.sessionFactory = sessionFactory;
  8     }
  9 
 10     // 保存一个记录
 11     // Spring与Hibernate整合:事务管理交给Spring
 12     public void save(Dept dept) {
 13         sessionFactory.getCurrentSession().save(dept);
 14     }
 15 }
 16 2. DeptService
 17 public class DeptService {
 18 
 19     private DeptDao deptDao;
 20     public void setDeptDao(DeptDao deptDao) {
 21         this.deptDao = deptDao;
 22     }
 23     
 24     public void save(Dept dept){
 25         deptDao.save(dept);
 26     }
 27 }
 28 
 29 3. App.java  测试
 30 public class App {
 31     
 32     // 容器
 33     private ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
 34 
 35     @Test
 36     public void testApp() throws Exception {
 37         DeptService deptServie = (DeptService) ac.getBean("deptService");
 38         System.out.println(deptServie.getClass());
 39         
 40         deptServie.save(new Dept());
 41     }
 42 }
 43 4. bean.xml 配置  【Spring管理SessionFactory的3中方式】
 44 <?xml version="1.0" encoding="UTF-8"?>
 45 <beans xmlns="http://www.springframework.org/schema/beans"
 46     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 47     xmlns:p="http://www.springframework.org/schema/p"
 48     xmlns:context="http://www.springframework.org/schema/context"
 49     xmlns:aop="http://www.springframework.org/schema/aop"
 50     xmlns:tx="http://www.springframework.org/schema/tx"
 51     xsi:schemaLocation="http://www.springframework.org/schema/beans
 52          http://www.springframework.org/schema/beans/spring-beans.xsd
 53           http://www.springframework.org/schema/context
 54          http://www.springframework.org/schema/context/spring-context.xsd
 55          http://www.springframework.org/schema/aop
 56          http://www.springframework.org/schema/aop/spring-aop.xsd
 57          http://www.springframework.org/schema/tx
 58           http://www.springframework.org/schema/tx/spring-tx.xsd">
 59 
 60     <!-- dao 实例 -->
 61     <bean id="deptDao" class="cn.itcast.dao.DeptDao">
 62         <property name="sessionFactory" ref="sessionFactory"></property>
 63     </bean>
 64     
 65     <!-- service 实例 -->
 66     <bean id="deptService" class="cn.itcast.service.DeptService">
 67         <property name="deptDao" ref="deptDao"></property>
 68     </bean>
 69     
 70     <!-- 数据源配置 -->
 71     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 72         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
 73         <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
 74         <property name="user" value="root"></property>
 75         <property name="password" value="root"></property>
 76         <property name="initialPoolSize" value="3"></property>
 77         <property name="maxPoolSize" value="10"></property>
 78         <property name="maxStatements" value="100"></property>
 79         <property name="acquireIncrement" value="2"></property>
 80     </bean>
 81     
 82     <!-- ###########Spring与Hibernate整合  start########### -->
 83     
 84     <!-- 方式(1)直接加载hibernate.cfg.xml文件的方式整合
 85     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 86         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
 87     </bean>    -->
 88     
 89     <!-- 方式(2)连接池交给spring管理  【一部分配置写到hibernate中,一份分在spring中完成】 
 90     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 91         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
 92         <property name="dataSource" ref="dataSource"></property>
 93     </bean> -->
 94     
 95     <!-- 【推荐】方式(3)所有的配置全部都在Spring配置文件中完成 -->
 96     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 97         <!-- 注入连接池对象 -->
 98         <property name="dataSource" ref="dataSource"></property>
 99         
100         <!-- hibernate常用配置 -->
101         <property name="hibernateProperties">
102             <props>
103                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
104                 <prop key="hibernate.show_sql">true</prop>
105                 <prop key="hibernate.hbm2ddl.auto">update</prop>
106             </props>
107         </property>
108         
109         <!-- hibernate映射配置 
110         <property name="mappingLocations">
111             <list>
112                 <value>classpath:cn/itcast/entity/*.hbm.xml</value>
113             </list>
114         </property>
115         -->
116         <property name="mappingDirectoryLocations">
117             <list>
118                 <value>classpath:cn/itcast/entity/</value>
119             </list>
120         </property>
121     </bean>
122     
123     
124     
125     <!-- ###########Spring与Hibernate整合  end########### -->
126     
127     <!-- 事务配置 -->
128     <!-- a. 配置事务管理器类 -->
129     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
130         <property name="sessionFactory" ref="sessionFactory"></property>
131     </bean>
132     <!-- b. 配置事务增强(拦截到方法后如果管理事务?) -->
133     <tx:advice id="txAdvice" transaction-manager="txManager">
134         <tx:attributes>
135             <tx:method name="*" read-only="false"/>
136         </tx:attributes>
137     </tx:advice>
138     <!-- c. Aop配置 -->
139     <aop:config>
140          <aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="pt"/>
141          <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
142     </aop:config>
143     
144 </beans>