首先对于Mybatis的主配置文件,只需要修改一处地方,将事务交给Spring管理,其它地方可以原封不动。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<properties resource="jdbc.properties"></properties> <typeAliases>
<package name="com.sunwii.mybatis.bean" />
</typeAliases> <environments default="development">
<environment id="development">
       <!--将事务交张Spring管理-->
<transactionManager type="org.mybatis.spring.transaction.SpringManagedTransactionFactory" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>

<!--这里mappers块可以保留或删除或配置不存在的包也无所谓-->
<mappers>
<package name="Xcom/sunwii/mybatis/mapper"></package>
</mappers>
</configuration>

然后修改一下Mybatis工具类,重新命令为SqlSessionFactoryBuilder:

public class SessionFactoryBuilder {
private String mybatisConfigPath; public String getMybatisConfigPath() {
return mybatisConfigPath;
} public void setMybatisConfigPath(String mybatisConfigPath) {
this.mybatisConfigPath = mybatisConfigPath;
} public SqlSessionFactory createSqlSessionFactory() {
SqlSessionFactory factory = null;
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(this.mybatisConfigPath);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return factory;
}
}

Spring的配置文件也就比较简洁:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 动态工厂实例化一个SqlSessionFactory -->
<bean id="sessionFactoryBuilder" class="com.sunwii.mybatis.util.SessionFactoryBuilder">
<property name="mybatisConfigPath" value="mybatis-config.xml" />
</bean>
<bean id="sessionFactory" factory-bean="sessionFactoryBuilder" factory-method="createSqlSessionFactory" /> <!-- Mapper动态代理开发扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sunwii.mybatis.mapper" />
</bean> <!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"
value="#{sessionFactory.configuration.environment.dataSource}" />
</bean> <!-- 注解事务 -->
<tx:annotation-driven
transaction-manager="transactionManager" /> <!-- Service扫描 -->
<context:component-scan
base-package="com.sunwii.mybatis.service.impl" /> </beans>

关键点就在于:将 org.apache.ibatis.session.SqlSessionFactory  用Spring容器创建出来

有3种方式:

1.   mybatis.spring整合包的方式:正常来说可以直接配置 org.mybatis.spring.SqlSessionFactoryBean 来创建,但本例为了减少配置量,减少Mybatis的变动,以及简洁起见,使用了自定义方式。

2.   自定义动态工厂方式:使用了自定义的动态工厂SessionFactoryBuilder(从单独使用的Mybatis工具类中修改而来)方法来创建SqlSessionFactory。

这样的话,原先在Mybatis主配置里配置的JDBC,数据源之类的,不需移动到Spring中,而事务管理器中只需要引Spring表达式引用即可: #{sessionFactory.configuration.environment.dataSource}

3.   自定义FactoryBean<SqlSessionFactory>接口方式:这就跟mybatis-spring.jar包整合的创建方式相类似(主要代码跟自定义动态工厂方法差不多,但需要实现好几个FactoryBean的方法,代码稍多,为简洁起见,不使用)

最后就是Service中直接使用Mapper接口了:

@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;
@Autowired
private RolePermissionMapper rolePermissionMapper;
@Autowired
private PermissionMapper permissionMapper; @Override
@Transactional
public int insertRole(Role role) { int rs = 0; rs = roleMapper.insert(role); //Permission permission = permissionMapper.selectById(56);
//role.setPermissions(Arrays.asList(permission)); Permission permission = new Permission();
permission.setName("permission-" + 1);
permissionMapper.insert(permission); // 0 / 0 测试事务回滚
new Integer(0 / 1); RolePermission rolePermission = new RolePermission();
rolePermission.setRole(role);
rolePermission.setPermission(permission);
rolePermissionMapper.insert(rolePermission); return rs;
} @Override
public Role getRoleById(Integer id) {
return roleMapper.selectById(id);
} }

说明:

由于采用了Mapper接口的方式来进行开发,org.mybatis.spring.mapper.MapperScannerConfigurer 在处理接口的时候,

经处理后的Mapper接口层用到了SqlSessionDaoSupportSqlSessionTemplate,所以不需要担心SqlSession线程安全问题,并且也不需要直接使用SqlSession,直接使用的是Mapper接口。

如果不采用Mapper接口开发,为了SqlSession线程安全问题,可以有几种处理方式:

1。可以自定义ThreadLocal<SqlSession>,代码较多,事务管理麻烦,不推荐。上边所有配置不变

2。让daoImpl 实现SqlSessionDaoSupport并注入SqlSessionFactory就可以了。上边所有配置不变

3。让daoImpl 中注入SqlSessionTemplate就可以了。上边所有配置不变,但多加一个SqlSessionTemplate的配置。

----------END OF 不需要怎么修改配置的Mybatis整合Spring要点------------------

最新文章

  1. PHP基础知识2
  2. POJ 3691 (AC自动机+状态压缩DP)
  3. Python开发者最常犯的10个错误
  4. xml中的特殊符号
  5. SQL cmd 实用工具学习 -1
  6. 多线程下不反复读取SQL Server 表的数据
  7. 学习C++语言的50条忠告
  8. Android EditText 无法换行
  9. qt博客
  10. CA认证和颁发吊销证书
  11. SqlServer varchar数据中类似于1.1.1.1这种值的排序方法
  12. 使用cocopod管理第三方
  13. [原创]基于Zynq PS与PL之间寄存器映射 Standalone &amp; Linux 例程
  14. java递归算法提前返回值带出
  15. base64图片
  16. MyEclipse免费使用
  17. ReferenceQueue
  18. 【原】Coursera—Andrew Ng机器学习—Week 4 习题—Neural Networks 神经网络
  19. 启动hadoop集群
  20. 洛谷 P2002 消息扩散

热门文章

  1. C语言入门-指针
  2. 使用maven开发javaweb项目
  3. hadoop入门之海量Web日志分析 用Hadoop提取KPI统计指标
  4. 【SQL server基础】判断数据库、表格、视图、存储过程、函数书否存在
  5. Spring5源码解析-前奏:本地构建Spring5源码
  6. 使用sp_getAppLock引发的一个小问题
  7. html5一些特性
  8. 【百度地图】如何去掉百度LOGO
  9. 痞子衡嵌入式:飞思卡尔i.MX RTyyyy系列MCU硬件那些事(2.1)- 玩转板载OpenSDA,Freelink调试器
  10. bootstrap-table 页脚总计(自定义统计总数)