Mybatis与Spring结合

来源:互联网 发布:普通话测试软件 编辑:程序博客网 时间:2024/06/06 00:26

我们来看一下Mybatis和Spring的结合使用
首先,我们看一下Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>      <beans xmlns="http://www.springframework.org/schema/beans"              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"              xmlns:aop="http://www.springframework.org/schema/aop"              xmlns:p="http://www.springframework.org/schema/p"              xmlns:tx="http://www.springframework.org/schema/tx"              xmlns:context="http://www.springframework.org/schema/context"              xsi:schemaLocation="                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd                  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties"/>         <context:annotation-config/>  <context:component-scan base-package="com"></context:component-scan><!-- 配置数据源 这里使用C3PO数据源   DBCP数据源为:org.apache.commons.dbcp.BasicDataSource 在实际应用中推荐使用C3P0,--><bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"                 destroy-method="close">        <property name="driverClassName" value="${jdbc.driver}"></property>        <property name="url" value="${jdbc.url}"></property>        <property name="username" value="${jdbc.username}"></property>        <property name="password" value="${jdbc.password}"></property>        </bean>         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">            <property name="dataSource" ref="dataSource"></property>        </bean>        <tx:annotation-driven/>        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="configLocation" value="classpath:mybatis-config.xml"></property>              </bean>   <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">       <constructor-arg index="0" ref="sqlSessionFactory" />    </bean>           </beans>        <!--         有时候我们指定的基包下面的并不全是我们定义的Mapper接口,为此MapperScannerConfigurer还为我们提供了另外两个可以缩小搜索和注册范围的属性。一个是annotationClass,另一个是markerInterface。annotationClass:当指定了annotationClass的时候,MapperScannerConfigurer将只注册使用了annotationClass注解标记的接口。markerInterface:markerInterface是用于指定一个接口的,当指定了markerInterface之后,MapperScannerConfigurer将只注册继承自markerInterface的接口。       如果上述两个属性都指定了的话,那么MapperScannerConfigurer将取它们的并集,而不是交集。即使用了annotationClass进行标记或者继承自markerInterface的接口都将被注册为一个MapperFactoryBean。现在假设我们的Mapper接口都继承了一个SuperMapper接口,那么我们就可以这样来定义我们的MapperScannerConfigurer。       除了用于缩小注册Mapper接口范围的属性之外,我们还可以指定一些其他属性,如: sqlSessionFactory:这个属性已经废弃。当我们使用了多个数据源的时候我们就需要通过sqlSessionFactory来指定在注册MapperFactoryBean的时候需要使用的SqlSessionFactory,因为在没有指定sqlSessionFactory的时候,会以Autowired的方式自动注入一个。换言之当我们只使用一个数据源的时候,即只定义了一个SqlSessionFactory的时候我们就可以不给MapperScannerConfigurer指定SqlSessionFactory。sqlSessionFactoryBeanName:它的功能跟sqlSessionFactory是一样的,只是它指定的是定义好的SqlSessionFactory对应的bean名称。sqlSessionTemplate:这个属性已经废弃。它的功能也是相当于sqlSessionFactory的,因为就像前面说的那样,MapperFactoryBean最终还是使用的SqlSession的getMapper方法取的对应的Mapper对象。当定义有多个SqlSessionTemplate的时候才需要指定它。对于一个MapperFactoryBean来说SqlSessionFactory和SqlSessionTemplate只需要其中一个就可以了,当两者都指定了的时候,SqlSessionFactory会被忽略。sqlSessionTemplateBeanName:指定需要使用的sqlSessionTemplate对应的bean名称。注意:由于使用sqlSessionFactory和sqlSessionTemplate属性时会使一些内容在PropertyPlaceholderConfigurer之前加载,导致在配置文件中使用到的外部属性信息无法被及时替换而出错,因此官方现在新的Mybatis-Spring中已经把sqlSessionFactory和sqlSessionTemplate属性废弃了,推荐大家使用sqlSessionFactoryBeanName属性和sqlSessionTemplateBeanName属性。 SqlSessionTemplate       除了上述整合之后直接使用Mapper接口之外,Mybatis-Spring还为我们提供了一种直接使用SqlSession的方式。Mybatis-Spring为我们提供了一个实现了SqlSession接口的SqlSessionTemplate类,它是线程安全的,可以被多个Dao同时使用。同时它还跟Spring的事务进行了关联,确保当前被使用的SqlSession是一个已经和Spring的事务进行绑定了的。而且它还可以自己管理Session的提交和关闭。当使用了Spring的事务管理机制后,SqlSession还可以跟着Spring的事务一起提交和回滚。       使用SqlSessionTemplate时我们可以在Spring的applicationContext配置文件中如下定义:    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">       <constructor-arg index="0" ref="sqlSessionFactory" />    </bean> -->        <!-- 定义好相应Mapper接口对应的MapperFactoryBean之后,        我们就可以把我们对应的Mapper接口注入到由Spring管理的bean对象中了,        比如Service bean对象。这样当我们需要使用到相应的Mapper接口时,        MapperFactoryBean会从它的getObject方法中获取对应的Mapper接口,        而getObject内部还是通过我们注入的属性调用SqlSession接口的getMapper(Mapper接口)方法来返回对应的Mapper接口的。        这样就通过把SqlSessionFactory和相应的Mapper接口交给Spring管理实现了Mybatis跟Spring的整合。 -->        <!-- 这样的话每个Mapper都需要配置一次 -->        <!--      <bean id="FilmMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  <property name="mapperInterface" value="com.dao.FilmMapper"></property>  <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>        </bean>-->
0 0