zoukankan      html  css  js  c++  java
  • 深入浅出Mybatis-与Spring集成

    单独使用mybatis是有很多限制的(比如无法实现跨越多个session的事务),而且很多业务系统本来就是使用spring来管理的事务,因此mybatis最好与spring集成起来使用。

    前置要求

    版本要求

    项目

    版本

    下载地址

    说明

    mybatis

    3.0及以上

    https://github.com/mybatis/mybatis-3/releases

    spring

    3.0及以上

    http://projects.spring.io/spring-framework/

    mybatis-spring

    1.0及以上

    https://github.com/mybatis/spring/releases

    spring事务配置

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- 自动扫描业务包 -->  
    2. <context:component-scan base-package="com.xxx.service" />  
    3.   
    4. <!-- 数据源 -->  
    5. <jee:jndi-lookup id="jndiDataSource" jndi-name="java:comp/env/jdbc/datasource" />  
    6.   
    7. <!-- 配置事务 -->  
    8. <bean id="txManager"  
    9.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    10.     <property name="dataSource" ref="jndiDataSource" />  
    11. </bean>  
    12. <!-- 配置基于注解的事物aop -->  
    13. <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>  

    单个集成

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- 集成mybatis -->  
    2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    3.     <property name="dataSource" ref="jndiDataSource" />  
    4.     <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />  
    5.     <!-- 自动配置别名 -->  
    6.     <property name="typeAliasesPackage" value="com.xxx.dto" />  
    7. </bean>  
    8.   
    9. <!--创建dao bean(只需提供接口不需提供实现类 )-->  
    10. <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    11.     <property name="mapperInterface" value="com.xxx.dao.UserDao" />  
    12.     <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    13. </bean>  

    我们不但要明白如何使用,更要明白为什么要这么使用。

    SqlSessionFactoryBean是一个工厂bean,它的作用就是解析配置(数据源、别名等)。

    MapperFactoryBean是一个工厂bean,在spring容器里,工厂bean是有特殊用途的,当spring将工厂bean注入到其他bean里时,它不是注入工厂bean本身而是调用bean的getObject方法。我们接下来就看看这个getObjec方法干了些什么:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public T getObject() throws Exception {  
    2.   return getSqlSession().getMapper(this.mapperInterface);  
    3. }  

    看到这里大家应该就很明白了,这个方法和我们之前单独使用Mybatis的方式是一样的,都是先获取一个Sqlsession对象,然后再从Sqlsession里获取Mapper对象(再次强调Mapper是一个代理对象,它代理的是mapperInterface接口,而这个接口是用户提供的dao接口)。自然,最终注入到业务层就是这个Mapper对象。

    实际的项目一般来说不止一个Dao,如果你有多个Dao那就按照上面的配置依次配置即可。

    如何使用批量更新

    前一节讲了如何注入一个mapper对象到业务层, mapper的行为依赖于配置,mybatis默认使用单个更新(即ExecutorType默认为SIMPLE而不是BATCH),当然我们可以通过修改mybatis配置文件来修改默认行为,但如果我们只想让某个或某几个mapper使用批量更新就不得行了。这个时候我们就需要使用模板技术:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!--通过模板定制mybatis的行为 -->  
    2. lt;bean id="sqlSessionTemplateSimple" class="org.mybatis.spring.SqlSessionTemplate">     
    3. <constructor-arg index="0" ref="sqlSessionFactory" />  
    4. <!--更新采用单个模式 -->  
    5. <constructor-arg index="1" value="SIMPLE"/>  
    6. </bean>  
    7.       
    8. <!--通过模板定制mybatis的行为 -->  
    9. lt;bean id="sqlSessionTemplateBatch" class="org.mybatis.spring.SqlSessionTemplate">     
    10. <constructor-arg index="0" ref="sqlSessionFactory" />  
    11. <!--更新采用批量模式 -->  
    12. <constructor-arg index="1" value="BATCH"/>  
    13. </bean>  

    这里笔者定义了两个模板对象,一个使用单个更新,一个使用批量更新。有了模板之后我们就可以改变mapper的行为方式了:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    2.     <property name="mapperInterface" value="com.xxx.dao.UserDao" />  
    3.     <property name="sqlSessionTemplate" ref=" sqlSessionTemplateBatch " />  
    4. </bean>  

    跟上一节的mapper配置不同的是,这里不需要配置sqlSessionFactory属性,只需要配置sqlSessionTemplatesqlSessionFactory属性在模板里已经配置好了)。

    通过自动扫描简化mapper的配置

    前面的章节可以看到,我们的dao需要一个一个的配置在配置文件中,如果有很多个dao的话配置文件就会非常大,这样管理起来就会比较痛苦。幸好mybatis团队也意识到了这点,他们利用spring提供的自动扫描功能封装了一个自动扫描dao的工具类,这样我们就可以使用这个功能简化配置:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- 采用自动扫描方式创建mapper bean(单个更新模式) -->  
    2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    3.     <property name="basePackage" value="com.xxx.dao" />  
    4.     <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateSimple" />  
    5.     <property name="markerInterface" value="com.xxx.dao.SimpleDao" />  
    6. </bean>  
    7.        
    8. <!-- 采用自动扫描方式创建mapper bean(批量更新模式) -->  
    9. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    10.     <property name="basePackage" value="com.xxx.dao" />  
    11.     <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />  
    12.     <property name="markerInterface" value="com.xxx.dao.BatchDao" />  
    13. </bean>  

    MapperScannerConfigurer本身涉及的spring的技术我就不多讲了,感兴趣且对spring原理比较了解的可以去看下它的源码。我们重点看一下它的三个属性:

    basePackage:扫描器开始扫描的基础包名,支持嵌套扫描;

    sqlSessionTemplateBeanName:前文提到的模板bean的名称;

    markerInterface:基于接口的过滤器,实现了该接口的dao才会被扫描器扫描,与basePackage是与的作用。

    除了使用接口过滤外,还可使用注解过滤:

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!-- 采用自动扫描方式创建mapper bean(批量更新模式) -->  
    2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    3.     <property name="basePackage" value="com.xxx.dao" />  
    4.     <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />  
    5.     <property name="annotationClass" value="com.xxx.dao.BatchAnnotation" />  
    6. </bean>  

    annotationClass配置了该注解的dao才会被扫描器扫描,与basePackage是与的作用。

    需要注意的是,两个过滤条件只能配一个。

  • 相关阅读:
    洛谷 P1195 口袋的天空
    洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
    Bzoj3277 串
    Bzoj1312 / POJ3155 Neerc2006 Hard Life
    Bzoj2655 calc
    51Nod 1228 序列求和
    洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging
    Bzoj1042 [HAOI2008]硬币购物
    Bzoj3884 上帝与集合的正确用法
    Bzoj4161 Shlw loves matrixI
  • 原文地址:https://www.cnblogs.com/caozengling/p/5320884.html
Copyright © 2011-2022 走看看