Spring与Hibernate、Mybatis整合

时间:2023-03-09 13:00:45
Spring与Hibernate、Mybatis整合
在Web项目中一般会把各个web框架结合在一起使用,比如spring+hibernate,spring+ibatis等,如此以来将其他的框架整合到spring中来,便有些少许的不便,当然spring已经把这种整合变得很简单了。本人结合在项目中使用过的spring和ibatis,以及和mybatis的整合,进而小测了一下和hibernate的整合,望批评指正。

一、spring、hibernate整合

Spring中不但可以选择SpringJDBC作为持久化技术,还可以选择Hibernate、iBatis、JPA、JDO等多种类型的持久化技术。Spring提供了方便的模板类对原ORM进行简化封装。让我们先回忆一下单独使用Hibernate时的过程,我们需要编写好一个对象关系的映射文件命名形式如xxx.hbm.xml,然后通过Hibernate的配置文件hibernate.cfg.xml 将所有的xxx.hbm.xml 映射文件组装起来,最后通过两行经典的代码得到SessionFactory的实例,如

Configuration cfg = new Configuration().configure("hiberante.cfg.xml");

SessionFactory sessionFactory = cfg.buildSessionFactory();

如果使用注解的话使用AnnotationConfiguration对象,现在Spring为创建SessionFactory提供了一个好用的FactoryBean工厂类:org.springframework.orm.hibernate3.LocalSessionFactoryBean,通过配置一些必要的属性,即可获取一个SessionFactoryBean。

1.使用HibernateTemplate

Hibernate.cfg.xml文件中配置了数据源、对象关联映射文件以及Hibernate控制属性信息。因此集成到spring中以后要把该文件中内容都拿过来如下:

<context:componet-scan base-package="com.demo" />

    <context:property-placeholder location="classpath:jdbc.properties" />
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
     <property name="driverClassName" value="${jdbc.driverClassName}"/>
     <property name="url" value="${jdbc.url}"/>
     <property name="username" value="${jdbc.username}"/>
     <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.org.hiberante3.LocalSessionFactoryBean" >
     <property name="dataSource" ref="dataSource"/>
     <property name="mappingResources" >
         <list>
                <value>com.demo.hibernate.domain/xxx.hbm.xml</value>
            </list>
     </property>
     <property name="hiberanteProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                 <prop key="hibernate.show_sql">true</prop>
         </props>
     </property>
     </bean>      <bean id="hiberanteTemplate" class="org.springframework.orm.hiberante3.HibernateTemplate">
     <property name="sessionFactory" ref="sessionFactory"/>
     </bean>      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
     <tx:annotation-driven transaction-manager="transactionManager"/> 

如上面的配置所示,基于模板类使用hibernate比较简单,spring提供了使用模板的支持类HibernateDaoSupport类,并通过getHibernateTemplate()方法向子类开放模板类实例的调用。如果不想在每个dao里面都加上HibernateTemplate,可以将其放在一个BaseDao里面,然后通过扩展该类创建一个使用HibernateTemplate的Dao,使用时直接getHibernateTemplate().op(obj) 就可以了。也可以将我们自己的Dao实现直接继承HibernateDaoSupport类同时实现Dao接口,该方式也很简单。剩下的就和单独使用Hibernate区别很小了。

对于lob类型的数据,我们还需要在Spring配置文件重定一个Lob数据处理器,让SessionFactory拥有处理Lob数据的能力。在SessionFactory里引用该bean:

<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />

2.使用原生Hibernate API

我们可以通过注入SessionFactory 直接通过getCurrentSession()能够获取和当前线程绑定的Session。Hibernate自身具备了获取和事务线程绑定的Session对象的功能,这与在Spring的HibernateTemplate中使用和事务绑定的Session相同。因此使用原生的API,同样和spring和事务管理器一起工作。和使用template不同的是原生API抛出的异常是Hibernate异常。

3.使用注解

Hibernate通过AnnotationConfiguration的addAnnotatedClass和addPackage方法加载使用JPA注解的实体类,获取映射的元数据信息,并在此基础上创建SessionFactory实例。spring专门提供了一个配套的AnnotationSessionFactoryBean,用以创建基于JPA注解的SessionFactory,该类扩展了LocalSessionFactoryBean类,增强的功能是可以根据实体类的注解获取ORM的配置信息。也允许混合使用XML配置和注解配置对象关系映射,Hibernate内部自动整合这些元数据信息,并不会产生冲突。

此外,Spring以防Web层访问Service层延迟加载的对象,专门提供了一个OpenSessionInViewFilter过滤器,它的主要功能是使每个请求过程绑定一个Hibernate Session,即使最初的事务已经完成了,也可以在Web层进行延迟加载的操作。OpenSessionInViewFilter过滤器将HibernateSession绑定到请求线程中,它将自动被Spring的事务管理器探测到,因此其使用与使用HibernateTransactionManager或JtaTransactionManager进行事务管理的环境,也可以用于非只读事务的数据操作中。

二、spring集成ibatis/mybatis

ibatis的后续版本改名叫mybatis,现在两者都被大家经常使用。两者的最大不同个人感觉是mybatis提供了接口绑定,你不需要再去实现Dao接口,而直接可在Service层调用,将注意力集中在映射文件上即可。ibatis的核心类是SqlMapClient,XmlSqlMapClientFactoryBean通过读取文件中的sqlMapConfig(当然包括很多的sqlMap),构建一个SqlMapClient,其通过sqlMap来执行映射的每个SQL语句;mybatis的核心类是SqlSessionFactory,SqlSessionFactoryBuilder读取配置文件(或注解)创建一个SqlSessionFactory,其通过SqlSession来执行已经映射的SQL语句。下面是两者和spring集成的配置:

1.Ibatis需要自己在单独注入各个DAO文件,如

    <bean id= "xxxDao" class ="com.demo.ibatis.dao.impl.xxxDaoImpl">
             <property name ="sqlMapClient" ref="sqlMapClient" />
    </bean >
      <bean id ="yyyDao" class= "com.demo.ibatis.dao.impl.yyyDaoImpl" >
             <property name ="sqlMapClient" ref="sqlMapClient" />
    </bean >   
    <bean id ="zzzDao" class= "com.demo.ibatis.dao.impl.zzzDaoImpl" >
             <property name ="sqlMapClient" ref="sqlMapClient" />
    </bean >
    在spring-prop.xml文件中配置sqlMapClient,如
    <bean id= "sqlMapClient" class= "org.springframework.orm.ibatis.SqlMapClientFactoryBean" > 
        <property name ="configLocation" value= "classpath:ibatis/ibatis-config.xml" />  
        <property name ="dataSource" ref="clopsDataSource" />
        <property name ="lobHandler" ref="lobHandler"/>
    </bean >     如property指定的configuration的位置一样,要有<sqlMapConfig>的配置,并将各个xml文件引入进来,ibatis-config.xml如下:
    <sqlMapConfig>
         <settings  useStatementNamespaces= "true"   cacheModelsEnabled= "true"   enhancementEnabled= "true"
             lazyLoadingEnabled= "true"  maxRequests= "32" maxSessions= "10" maxTransactions= "5"  />
         <sqlMap resource ="ibatis/xxx.xml" />
         <sqlMap resource ="ibatis/yyy.xml" />
         <sqlMap resource ="ibatis/zzz.xml" />
......
    </sqlMapConfig>     2.而在Mybatis中则不需要这样配置:
      在spring-prop中指定sqlSessionFactory,如,
      <bean id= "sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean" >
             <property name ="dataSource" ref="dataSource" />
             <property name ="typeAliasesPackage" value= "com.demo.mybatis.model" />
             <property name ="mapperLocations" value= "classpath*:mapper/**/*.xml" />
      </bean >
      <bean id ="sqlSession" class= "org.mybatis.spring.SqlSessionTemplate" >
             <constructor-arg index ="0" ref="sqlSessionFactory" />
             <constructor-arg index ="1" value="BATCH" />
      </bean >
      <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer">
             <property name ="basePackage" value= "com.demo.mybatis.dao" />
             <property name ="sqlSessionTemplate" ref= "sqlSession" />
      </bean >
      其中各种mapper文件在由mapperLocations指定。mybatis相对简单些。

使用时类似上面的Hibernate,ibatis可将sqlMapClientTemplate直接注入,或者继承SqlMapClientDaoSupport类同时实现Dao接口,通过getSqlMapClientTemplate()方法或者sqlMapClient进行操作。注意上述ibatis直接注入的是sqlMapClient,其实sqlMapClientTemplate的构造函数引入了一个sqlMapClient,其使用的方法均来自sqlMapClient。

Mybatis与spring的集成因对数据处理的不同方式而不同,上述方式是采用MapperScannerConfigurer的处理方式,且采用了注解形式,还可以使用MapperFactoryBean、SqlSession、SqlSessionDaoSupport的数据处理方式,使用后两种方式时需要自己实现Dao接口,而对于MapperFactoryBean其直接指定Dao接口mapperInterface。