深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解

8.2 MyBatis-Spring应用

8.2.1 概述

本文主要讲述通过注解配置MyBatis-Spring。

配置分为几个部分:

 配置数据源
配置SqlSessionFactory
配置SqlSessionTemplate
配置Mapper
事务处理

mybatis中,使用SqlSessionFactory来产生SqlSession。

mybatis-spring中,使用SqlSessionTemplate来完成,它封装了对SqlSession的操作。所以通过SqlSessionTemplate可以得到Mapper。

8.2.2 配置SqlSessionFactory

SqlSessionFactoryBean
1.dataSource
2.configLocation

配置示例如下:

 <bean id="dataSource" class="...">
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>

其中配置文件mybatis.xml的配置示例如下:

(注意,因为Spring已经初始化了数据源,就是上面那个id为dataSource的bean,在mybatis的配置文件中就不需要再配置关于数据库的environments节点了。本来mybatis中,environments里配置了datasource和transactionManager等。)

 <configuration>
<settings>...<settings>
<typeAliases>....<typeAliases>
<mappers>
<mapper resource="com\lyh\po\role.xml"/>
<mappers>
</configuration>

8.2.3 配置SqlSessionTemplate

有两种构建方法。

构建方法1:

 <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory">
</bean>

构建方法2:

这里的第二个参数,是执行器类型ExecutorType,他是一个枚举类,有三个值可以选。

 <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory">
<constructor-arg name="1" value="BATCH/SIMPLE/REUSE">
</bean>

8.2.4 配置Mapper

 <!-- 扫描basePackage下所有以@Repository标识的接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lyh.dao"/>
<property name="annotationClass" value="yorg.springframework.stereotype.Repository"/>
<!--显示指定template的名字
<property name="sqlSessionTemplateBeanName" value=""/>
-->
<!--指定实现了何种接口,就被认为是映射器mapper
<property name="markerInterface" value=""/>
-->
</bean>

注意,dao包下的类别忘记加上注解@Repository。

 @Repository
public interface UserDao{
....
}

8.2.5 配置事务

mybatis单独使用时,数据源DataSource和事务管理TransactionManager都是在environments节点下配置的。

mybatis-spring使用时,mybatis的配置文件mybatis.xml不需要再配置DataSource,正如前面所言,因为spring已经配置好了,以bean的形式。

而事务管理,mybatis-spring是使用Spring AOP去管理的。所以同样的,mybatis的配置文件mybatis.xml不需要再配置TransactionManager,而是以bean的形式配置如下:

Spring AOP分为声明式事务和编程式事务,一般使用前者。

 <!-- 使用annotation定义事务,声明式 -->
<tx:annotation-driven transaction-manager="txManager"/> <!-- 事务管理器, Jdbc单数据源事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

到此配置就结束了。

汇总一下,一共有两个文件,mybatis-spring.xml和mybatis.xml。

(1)mybatis-spring.xml


1 配置数据源
2 配置SqlSessionFactory
3 配置SqlSessionTemplate
4 配置Mapper
5 事务处理
1 <bean id="dataSource" class="...">
2 </bean>
3
4 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
5 <property name="dataSource" ref="dataSource"/>
6 <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
7 </bean> 1 <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
2 <constructor-arg index="0" ref="sqlSessionFactory">
3 </bean> 1 <!-- 扫描basePackage下所有以@Repository标识的接口 -->
2 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
3 <property name="basePackage" value="com.lyh.dao"/>
4 <property name="annotationClass" value="yorg.springframework.stereotype.Repository"/>
5 <!--显示指定template的名字
6 <property name="sqlSessionTemplateBeanName" value=""/>
7 -->
8 <!--指定实现了何种接口,就被认为是映射器mapper
9 <property name="markerInterface" value=""/>
10 -->
11 </bean> 1 <!-- 使用annotation定义事务,声明式 -->
2 <tx:annotation-driven transaction-manager="txManager"/>
3
4 <!-- 事务管理器, Jdbc单数据源事务 -->
5 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
6 <property name="dataSource" ref="dataSource"/>
7 </bean>

(2)mybatis.xml

1 <configuration>
2 <settings>...<settings>
3 <typeAliases>....<typeAliases>
4 <mappers>
5 <mapper resource="com\lyh\po\role.xml"/>
6 <mappers>
7 </configuration>

2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解的更多相关文章

  1. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  2. 2MyBatis入门--深入浅出MyBatis技术原理与实践(笔记)

    什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  3. 《深入浅出MyBatis技术原理与实战》——6. MyBatis的解析和运行原理

    MyBatis的运行分为两大部分,第一部分是读取配置文件缓存到Configuration对象,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程. 6.1 涉及的技术 ...

  4. 3MyBatis配置--深入浅出MyBatis技术原理与实践(笔记)

    XML 映射配置文件 configuration 配置 properties 属性 settings 设置 typeAliases 类型命名 typeHandlers 类型处理器 objectFact ...

  5. Atitit.ide技术原理与实践attilax总结

    Atitit.ide技术原理与实践attilax总结 1.1. 语法着色1 1.2. 智能提示1 1.3. 类成员outline..func list1 1.4. 类型推导(type inferenc ...

  6. Atitit.异步编程技术原理与实践attilax总结

    Atitit.异步编程技术原理与实践attilax总结 1. 俩种实现模式 类库方式,以及语言方式,java futuretask ,c# await1 2. 事件(中断)机制1 3. Await 模 ...

  7. Atitit.gui api自动化调用技术原理与实践

    Atitit.gui api自动化调用技术原理与实践 gui接口实现分类(h5,win gui, paint opengl,,swing,,.net winform,)1 Solu cate1 Sol ...

  8. 【沙龙报名中】集结腾讯技术专家,共探AI技术原理与实践

    | 导语 9月7日,上海市长宁区Hello coffee,云+社区邀您参加<AI技术原理与实践>沙龙活动,聚焦人工智能技术在各产业领域的应用落地,共话AI技术带来的机遇与挑战,展望未来. ...

  9. Java 动态调试技术原理及实践

    本文转载自Java 动态调试技术原理及实践 导语 断点调试是我们最常使用的调试手段,它可以获取到方法执行过程中的变量信息,并可以观察到方法的执行路径.但断点调试会在断点位置停顿,使得整个应用停止响应. ...

随机推荐

  1. 数据结构设计 Stack Queue

    之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...

  2. [oldboy-django][5python基础][内置函数]zip

    python3中,把两个或两个以上的迭代器封装成生成器,在循环遍历生成器中,不断产生元组. 如果提供的迭代器长度不对等,生成器的长度为最短迭代器的长度. # coding= utf-8 # zip 多 ...

  3. python技巧:拆分多层嵌套列表

    方法一: >>> import itertools >>> a = [[1, 2], [3, 4], [5, 6]] >>> list(itert ...

  4. Spring整合hibernate -声明事务管理

     目录 1 sessionFactory 注入HibernateTransactionManager 2 XML配置的配置 3 添加annotation-driven 4 引入JAR包 5在servi ...

  5. Hibernate命名策略及配置

    hibernate 表 命名策略         分类:            hibernate2013-02-27 18:46464人阅读评论(0)收藏举报 Hibernate注释下的自定义架构实 ...

  6. hibernate注解配置举例说明

    Hibernate Annotation   (Hibernate 注解)   进入:http://www.hibernate.org 说明文档: 英文:http://docs.jboss.org/h ...

  7. jquery复制当前tr行

    //复制 var vBudgetCompileObj = (function() { /*table新增/移除行,参数:tableId*/ var getMaxIndex; var funGenera ...

  8. 【bzoj2427】[HAOI2010]软件安装 Tarjan+树形背包dp

    题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现 ...

  9. 连接XenServer中VM的Console。

    在XenServer6.2中的虚拟机,连接Guest VM串口的方法: 1. 首先在Guest VM中设置串口: 我的是CentOS6: 修改/etc/grub/grub.conf: 2. 修改完后, ...

  10. 在子页面session过期无法跳转到父页面

    当session过期后可以用过滤器来设置重定向页面 public class ActionFilter extends HttpServlet implements Filter { private ...