首页 > 代码库 > Spring 整合 Hibernate

Spring 整合 Hibernate

Spring 整合 Hibernate

•Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA。
Spring 对这些 ORM 框架的支持是一致的, 因此可以把和 Hibernate 整合技术应用到其他 ORM 框架上.
•Spring 2.0 同时支持 Hibernate 2.x 和 3.x. 但 Spring 2.5 只支持 Hibernate 3.1 或更高版本
 

1.Spring 整合 Hibernate 整合什么 ?

  1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
  2). 让 Hibernate 使用上 Spring 的声明式事务

2. 整合步骤:

  1). 加入 hibernate

    ①. jar 包
    ②. 添加 hibernate 的配置文件: hibernate.cfg.xml
技术分享
 1 <hibernate-configuration> 2     <session-factory>    3         <!-- 配置 hibernate 的基本属性 --> 4         <!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 --> 5         <!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 --> 6         <!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. --> 7         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 8  9         <property name="hibernate.show_sql">true</property>10         <property name="hibernate.format_sql">true</property>11         12         <property name="hibernate.hbm2ddl.auto">update</property>13         14         <!-- 配置 hibernate 二级缓存相关的属性. -->                15     </session-factory>16 </hibernate-configuration>
sessionFactory
    ③. 编写了持久化类对应的 .hbm.xml 文件。

  2). 加入 Spring

    ①. jar 包
    ②. 加入 Spring 的配置文件
技术分享
 1     <!-- 配置自动扫描的包 --> 2     <context:component-scan base-package="com.atguigu.spring.hibernate"></context:component-scan> 3      4     <!-- 配置数据源 --> 5     <!-- 导入资源文件 --> 6     <context:property-placeholder location="classpath:db.properties"/> 7      8     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 9         <property name="user" value="http://www.mamicode.com/${jdbc.user}"></property>10         <property name="password" value="http://www.mamicode.com/${jdbc.password}"></property>11         <property name="driverClass" value="http://www.mamicode.com/${jdbc.driverClass}"></property>12         <property name="jdbcUrl" value="http://www.mamicode.com/${jdbc.jdbcUrl}"></property>13 14         <property name="initialPoolSize" value="http://www.mamicode.com/${jdbc.initPoolSize}"></property>15         <property name="maxPoolSize" value="http://www.mamicode.com/${jdbc.maxPoolSize}"></property>16     </bean>17     18     <!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->19     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">20         <!-- 配置数据源属性 -->21         <property name="dataSource" ref="dataSource"></property>22         <!-- 配置 hibernate 配置文件的位置及名称 -->23         <!--  24         <property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"></property>25         -->26         <!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 -->27         <property name="hibernateProperties">28             <props>29                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>30                 <prop key="hibernate.show_sql">true</prop>31                 <prop key="hibernate.format_sql">true</prop>32                 <prop key="hibernate.hbm2ddl.auto">update</prop>33             </props>34         </property>35         <!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 -->36         <property name="mappingLocations" 37             value="http://www.mamicode.com/classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml"></property>38     </bean>39 40     <!-- 配置 Spring 的声明式事务 -->41     <!-- 1. 配置事务管理器 -->42     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">43         <property name="sessionFactory" ref="sessionFactory"></property>44     </bean>45 46     <!-- 2. 配置事务属性, 需要事务管理器 -->47     <tx:advice id="txAdvice" transaction-manager="transactionManager">48         <tx:attributes>49             <tx:method name="get*" read-only="true"/>50             <tx:method name="purchase" propagation="REQUIRES_NEW"/>51             <tx:method name="*"/>52         </tx:attributes>53     </tx:advice>54 55     <!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->56     <aop:config>57         <aop:pointcut expression="execution(* com.atguigu.spring.hibernate.service.*.*(..))" 58             id="txPointcut"/>59         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>60     </aop:config>
applicationContext.xml

  3). 整合.

3. 编写代码

  1). 在daoImpl 中使用 session

	private SessionFactory sessionFactory;		//不推荐使用 HibernateTemplate 和 HibernateDaoSupport	//因为这样会导致 Dao 和 Spring 的 API 进行耦合	//可以移植性变差//	private HibernateTemplate hibernateTemplate;		//获取和当前线程绑定的 Session. 	private Session getSession(){		return sessionFactory.getCurrentSession();	}

   2) 

	/**	 * Spring hibernate 事务的流程	 * 1. 在方法开始之前	 * ①. 获取 Session	 * ②. 把 Session 和当前线程绑定, 这样就可以在 Dao 中使用 SessionFactory 的	 * getCurrentSession() 方法来获取 Session 了	 * ③. 开启事务	 * 	 * 2. 若方法正常结束, 即没有出现异常, 则	 * ①. 提交事务	 * ②. 使和当前线程绑定的 Session 解除绑定	 * ③. 关闭 Session	 * 	 * 3. 若方法出现异常, 则:	 * ①. 回滚事务	 * ②. 使和当前线程绑定的 Session 解除绑定	 * ③. 关闭 Session	 */

   使用事务和在spring中一样。注解和声明式。

Spring 整合 Hibernate