Spring整合Hibernate

来源:互联网 发布:诺基亚商店软件下载 编辑:程序博客网 时间:2024/06/13 03:04

目标:

* 由IOC容器来管理Hibernate的SessionFactory* 让Hibernate使用Spring的申明式事务

实现:

1. Hibernate的jar包:

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar

2. 添加Hibernate的配置文件: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>        <!-- 配置 hibernate 的基本属性 -->        <!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 -->        <!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例再进行配置 -->        <!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. -->        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>        <property name="hibernate.show_sql">true</property>        <property name="hibernate.format_sql">true</property>        <!-- 自动生成数据表. -->        <property name="hibernate.hbm2ddl.auto">update</property>        <!-- 配置 hibernate 二级缓存相关的属性-暂不配置. -->    </session-factory></hibernate-configuration>

3. 生成bean及与数据库对应关系的 .hbm.xml文件(可由Eclipse安装插件后全自动生成)

4. 配置Spring(jar包、配置文件、连接数据源)

5. 由IOC容器来管理Hibernate的SessionFactory

<!-- 通过Spring提供的LocalSessionFactory配置Hibernate的SessionFactory实例 --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">      <!-- 配置数据源 -->      <property name="dataSource" ref="dataSource"></property>      <!-- 配置Hibernate配置文件的位置及名称 -->      <property name="configLocation" value="classpath:hibernate-cfg.xml"></property>      <!-- 配置Hibernate映射文件的位置及名称 -->      <property name="mappingLocations" value="classpath:com/nifury/spring/hibernate/entities/*.hbm.xml"></property></bean>

6. 配置Hibernate事务

<!-- 配置事务管理器 --><bean id="transactionManger" class="org.springframework.orm.hibernate4.HibernateTransactionManager">      <property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 配置事务管理器 --><tx:advice id="txAdvice" transaction-manager="transactionManager">      <tx:attributes>            <tx:method name="get*" read-only="true"/>            <tx:method name="*"/>      </tx:attributes></tx:advice><!-- 配置事务切点,并把切点和事务属性关联起来 --><aop:config>      <aop:pointcut expression="execution(* com.nifury.spring.hibernate.service.*.*(..))" id="txPointCut"/>      <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/></aop:config>

7. 使用Spring hibernate 事务的流程

* 在方法开始之前 ①. 获取 Session  ②. 把 Session 和当前线程绑定, 这样就可以在 Dao 中使用 SessionFactory 的 getCurrentSession() 方法来获取 Session 了  ③. 开启事务* 若方法正常结束, 即没有出现异常, 则 ①. 提交事务  ②. 使和当前线程绑定的 Session 解除绑定  ③. 关闭 Session* 若方法出现异常, 则: ①. 回滚事务 ②. 使和当前线程绑定的 Session 解除绑定 ③. 关闭 Session
@Repositorypublic class BookDao{      @Autowired      private SessionFactory sessionFactory;      private Session getSession(){            return sessionFactory.getCurrentSession();      }      /**       * 通过isbn查找书的价格       * @param isbn       * @return       * @author Nifury       */      public int findBookPriceByIsbn(String isbn) {            String hql = "SELECT b.price FROM Book b WHERE b.isbn = ?";            Query query = getSession().createQuery(hql).setString(0, isbn);            return (Integer)query.uniqueResult();      }}
0 0
原创粉丝点击