2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解
深入浅出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(二)-----配置文件详解的更多相关文章
- spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途
Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...
- 2MyBatis入门--深入浅出MyBatis技术原理与实践(笔记)
什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- 《深入浅出MyBatis技术原理与实战》——6. MyBatis的解析和运行原理
MyBatis的运行分为两大部分,第一部分是读取配置文件缓存到Configuration对象,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程. 6.1 涉及的技术 ...
- 3MyBatis配置--深入浅出MyBatis技术原理与实践(笔记)
XML 映射配置文件 configuration 配置 properties 属性 settings 设置 typeAliases 类型命名 typeHandlers 类型处理器 objectFact ...
- Atitit.ide技术原理与实践attilax总结
Atitit.ide技术原理与实践attilax总结 1.1. 语法着色1 1.2. 智能提示1 1.3. 类成员outline..func list1 1.4. 类型推导(type inferenc ...
- Atitit.异步编程技术原理与实践attilax总结
Atitit.异步编程技术原理与实践attilax总结 1. 俩种实现模式 类库方式,以及语言方式,java futuretask ,c# await1 2. 事件(中断)机制1 3. Await 模 ...
- Atitit.gui api自动化调用技术原理与实践
Atitit.gui api自动化调用技术原理与实践 gui接口实现分类(h5,win gui, paint opengl,,swing,,.net winform,)1 Solu cate1 Sol ...
- 【沙龙报名中】集结腾讯技术专家,共探AI技术原理与实践
| 导语 9月7日,上海市长宁区Hello coffee,云+社区邀您参加<AI技术原理与实践>沙龙活动,聚焦人工智能技术在各产业领域的应用落地,共话AI技术带来的机遇与挑战,展望未来. ...
- Java 动态调试技术原理及实践
本文转载自Java 动态调试技术原理及实践 导语 断点调试是我们最常使用的调试手段,它可以获取到方法执行过程中的变量信息,并可以观察到方法的执行路径.但断点调试会在断点位置停顿,使得整个应用停止响应. ...
随机推荐
- Docker Hadoop LAMP安装配置教程
以下教程来自九章算法. 1.How to install Dockerhttps://bupt.quip.com/YehSAR4qnGqB 2.How to set up hadoop environ ...
- ibatis selectKey
<insert id="insert" parameterClass="A"> <selectKey keyProperty="uu ...
- 兼容浏览器 回车键 keydown事件
$("body").keydown(function(event){ if(event.keyCode==13){ //body } }); 重点:$("bo ...
- P4555 最长双回文串
题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n的串 S ,求 S的最长双回文子串 T ,即可将 ...
- Python之时间:time模块
import time 对于时间,使用最频繁的模块 1.获取当前时间 (1)时间戳 time.time() 时间戳:从1970年1月1日0点开始到现在按秒计算的偏移量 (2)时间元组 time.l ...
- 洛谷 [P3388] 割点模版
tarjan 求无向图的割点 割点,即割去此点后原图可变为两个或多个独立的联通块 一个点 x 是割点,当且仅当存在一个x 的子节点 y ,使得 low[y] >= dfn[x] 对于根节点来说, ...
- RocketMq使用注意事项
Topic 一个Topic是一个主题.一个系统中,我们可以将消息划成Topic,这样,将不同的消息发送到不同的queue. Queue 一个topic下,我们可以设置多个queue,每个queue就是 ...
- POJ1716 Integer Intervals
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13984 Accepted: 5943 Description An i ...
- Document类
一.类结构 org.jsoup.nodes Class Document java.lang.Object org.jsoup.nodes.Node org.jsoup.nodes.Element o ...
- DCP port
DCP port: D+ D- short. This doesn't support any data transfer. Without the need ofr enumeration. Can ...