深入浅出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. 获取完整的URL request.getQueryString()

    public String codeToString(String str) { String strString = str; try { byte tempB[] = strString.getB ...

  2. 观15级K班团队作业有感

    1.指尖加密 特点:通过可移动设备手机参与电脑文件的解密,使加密更加安全. 缺点:跟柯逍老师的想法差不多,UI简陋,操作不是很友好,或许可以加一个帮助文档. 2.youreyes 特点:可以检测路过的 ...

  3. Linux性能分析调优工具介绍

    1.常用性能分析工具 1)CPU性能分析工具 vmstat ps sar time strace pstree top 2)Memory性能分析工具 vmstat strace top ipcs ip ...

  4. inline-block元素垂直对齐

    多个inline-block元素使用vertical-align:middle无法对齐,必须有个height:100%的子元素才行,通常使用伪元素.另一种方法是添加line-height:normal ...

  5. vue的v-for循环普通数组、对象数组、对象、数字

    如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  6. Repeater用ul li,一行显示多条数据

    原文发布时间为:2009-08-26 -- 来源于本人的百度文章 [由搬家工具导入] .rep {         width:680px;         float:left;         l ...

  7. c#.net前台调用JS文件中的函数[.net与JavaScript的应用]

    原文发布时间为:2008-10-10 -- 来源于本人的百度文章 [由搬家工具导入] <%@ Page Language="C#" AutoEventWireup=" ...

  8. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---41

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  9. final、finalize()、finally、static

    一.final final的三种情况: 1.变量 1)对于基本类型,final使数值恒定不变:而对于对象引用,final使引用恒定不变,即一旦引用被初始化指向一个对象,就无法再把它改为指向另一个对象, ...

  10. HttpModel 和 HttpHandle

    页面的请求过程: HttpRequest-> inetinfo.exe-> aspnet_isapi.dll-> Http pipeline(命名管道)-> aspnet_wp ...