Spring+Hibernate集成

来源:互联网 发布:服务器需要开放的端口 编辑:程序博客网 时间:2024/06/06 11:05

 

         通过之前的文章我们已经介绍了关于SpringStruts的集成方案,因为Spring具有良好的开放性,不仅能与Struts整合,而且还能与大部分ORM框架良好整合。下面将总结SpringHibernate的整合,主要流程如下

*配置SessionFactory

*配置事务管理器

*配置事务的传播特性

 

1配置SessionFactory.

        在通过Hibernate进行持久层访问时,Hibernate的SessionFactory是一个非常重要的对象,它是单个数据库映射关系编译后的内存镜像。大部分情况下,一个J2EE应用对应一个数据库,也即对应一个SessionFactory对象。

       在纯粹的Hibernate访问中,应用程序需要手动创建SessionFactory实例,但这并不是一个好的方案。在实际开发中,可以以一种声明式的方式管理SessionFactory实例,直接以配置文件来管理SessionFactory实例.Spring的IoC容器则提供了更好的管理方式,它以声明式的方式配置Session- Factory实例.

配置文件:applicationContext-common.xml


<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns="http://www.springframework.org/schema/beans"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >    <!-- 配置SessionFactory -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <!-- 注入Hibernate的配置文件 -->        <property name="configLocation">            <!-- 配置文件在ClassPath下面. classpath相当于一个协议,相当于http,配置好之后会从classpath下找. -->            <value>classpath:hibernate.cfg.xml</value>        </property>    </bean>


        一旦在Spring的IoC容器中配置了SessionFactoryBean,它将随应用的启动而加载,并可以充分利用IoC容器的功能,将SessionFactoryBean注入任何Bean,比如DAO组件。一旦DAO组件获得了SessionFactory Bean的引用,就可以完成实际的数据库访问。

 

2配置事务管理器和传播特性

        Spring提供了非常简洁的声明式事务控制,只需要在配置文件中增加事务控制片段,业务逻辑代码无须任何改变。Spring的声明式事务逻辑,甚至支持在不同事务策略之间切换.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns="http://www.springframework.org/schema/beans"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >    <!-- 配置SessionFactory -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <!-- 注入Hibernate的配置文件 -->        <property name="configLocation">            <!-- 配置文件在ClassPath下面. classpath相当于一个协议,相当于http,配置好之后会从classpath下找. -->            <value>classpath:hibernate.cfg.xml</value>        </property>    </bean>        <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <!-- 注入SessionFactory -->        <property name="sessionFactory">            <!-- 指向上面配置好的SessionFactory -->            <ref bean="sessionFactory" />        </property>    </bean>    <!--Aspect声明-->    <aop:config>        <!-- 哪些类的那些方法是用事务 -->        <aop:pointcut id="allManagerMethod" expression="execution(* com.tgb.drp.service.*.*(..))"  />        <!-- 将advice应用到pointcut上面 -->        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>    </aop:config>        <!-- 事务执行代码 -->    <tx:advice id="txAdvice"  transaction-manager="transactionManager">        <!-- 细粒度配置 -->        <tx:attributes>            <!-- 配置add开头下的事务传播特性 -->            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="del*" propagation="REQUIRED"/>            <tx:method name="modify*" propagation="REQUIRED"/>            <!-- 提高查询效率 -->            <tx:method name="*" propagation="REQUIRED" read-only="true"/>                    </tx:attributes>    </tx:advice></beans>



 

3、编写业务逻辑方法,实现Dao组件

          Spring提供了一系列的抽象类,这些抽象将被作为应用中DAO实现类的父类。通过继承这些抽象类,Spring简化了DAO的开发步骤,能以一致的方式使用数据库访问技术。不管底层采用JDBC、JDO或Hibernate,应用中都可采用一致的编程模型。应用的DAO类继承这些抽象类,会大大简化应用的开发。最大的好处是,继承这些抽象类的DAO能以一致的方式访问数据库,意味着应用程序可以在不同的持久层访问技术中切换.

        Spring可以使用相同的访问模式、不同的数据库访问技术。就Hibernate的持久层访问技术而言,Spring提供了如下3个工具类(或接口)来支持DAO组件的实现

HibernateDaoSupport 

HibernateTemplate 

HibernateCallBack

以下代码主要是进行增加和查找功能展示,ItemDaoImpl继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HibernateTemplate是HibernateSession的轻量级封装.

 

public class ItemDaoImpl extends HibernateDaoSupport implements ItemDao {//增加public void addItem(Item item) {getHibernateTemplate().save(item);}//查找public Item findItemById(String itemNo) {return (Item) getHibernateTemplate().load(Item.class, itemNo);}}


 

        以上内容是SpringHibernate的核心整合内容.通过对Spring+StrutsSpring+Hibernate的整合我们可以发现Spring处于一个比较特殊的角色位置,它能够通过他良好的开放性跟其他的框架进行整合.通过熟练各个框架之间的整合,帮助我们进一步理解各个框架的作用以及他们之间的关系.

 

 

原创粉丝点击