深入浅出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. 【转】UGUI VS NGUI

    原文:http://gad.qq.com/college/articledetail/7191053 注[1]:该比较是基于15年-16年期间使用NGUI(3.8.0版本)与UGUI(4.6.9版本) ...

  2. 【bzoj4259/bzoj4503】残缺的字符串/两个串 FFT

    bzoj4259 题目描述 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有 ...

  3. linux下编译libmysqlclient, 安装mysql-server mysql-client

    cmake . -DCMAKE_INSTALL_PREFIX=/home/zhangyawei/server/depends make make install 安装 mysql-server mys ...

  4. HDU 4514 湫湫系列故事——设计风景线(并查集+树形DP)

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  5. bzoj 2387: [Ceoi2011]Traffic

    bzoj 2387: [Ceoi2011]Traffic 题目描述 The center of Gdynia is located on an island in the middle of the ...

  6. springboot开启定时任务 添加定时任务 推送

    最近在自学Java的springboot框架,要用到定时推送消息.参考了网上的教程,自己调试,终于调好了.下面将网上的教程归纳下,总结复习下.  springboot开启定时任务  在SpringBo ...

  7. 开发人员为组件添加自定义的className

    在开发过程当中需要给组件写上自己的样式,这个时候怎么办呢? 直接给组件添加className这样是无效的 当给组件添加className之后 在写组件的时候需要对使用你的组件的开发人员提供自定义cla ...

  8. 部署私有Docker Registry

    安装部署一个私有的Docker Registry是引入.学习和使用Docker这门技术的必经之路之一.尤其是当Docker被所在组织接受,更多人.项目和产品开始接触和使用Docker时,存储和分发自制 ...

  9. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  10. RowFilter 对于已获取到的dataset进行过滤

    原文发布时间为:2009-11-12 -- 来源于本人的百度文章 [由搬家工具导入] DataView的属性RowFilter使用方法 p.s. 重点在于DataView是DateTable相关联的一 ...