有两种方式:一种是分包的方式、一种是加注解的方式(@DataSource(ref=""))。

  分包方式:项目结构图如下:

                          

分为com.itmayiedu.test01、com.itmayiedu.test02两个包 里面是dao和service层,数据操作。

com.itmayiedu.datasource 里面放置数据源、数据库事物有关。两个配置相同。

两个数据源,需要在其中一个添加@Primary注解,作为主数据源,不然数据库找不到主的会报错;在springboot 2.0的版本后可以不需要添加@Primary注解

package com.itmayiedu.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; //DataSource01
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.itmayiedu.test01", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config { /**
*
* @methodDesc: 功能描述:(配置test1数据库)
* @author: 余胜军
* @param: @return
* @createTime:2017年9月17日 下午3:16:44
* @returnType:@return DataSource
* @copyright:上海每特教育科技有限公司
* @QQ:644064779
*/
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
} /**
*
* @methodDesc: 功能描述:(test1 sql会话工厂)
* @author: 余胜军
* @param: @param
* dataSource
* @param: @return
* @param: @throws
* Exception
* @createTime:2017年9月17日 下午3:17:08
* @returnType:@param dataSource
* @returnType:@return
* @returnType:@throws Exception SqlSessionFactory
* @copyright:上海每特教育科技有限公司
* @QQ:644064779
*/
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(
// new
// PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
} /**
*
* @methodDesc: 功能描述:(test1 事物管理)
* @author: 余胜军
* @param: @param
* dataSource
* @param: @return
* @param: @throws
* Exception
* @createTime:2017年9月17日 下午3:17:08
* @returnType:@param dataSource
* @returnType:@return
* @returnType:@throws Exception SqlSessionFactory
* @copyright:上海每特教育科技有限公司
* @QQ:644064779
*/
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }

  application.properties:

###datasource1
spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test1.url = jdbc:mysql://localhost:3306/chapter3?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = zhaocheng
###datasource2
spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test2.url = jdbc:mysql://localhost:3306/chapter13?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = zhaocheng

  TestController:

  添加  @Transactional(transactionManager = "test1TransactionManager")   添加并且区别事物。

package com.itmayiedu.controller;

import com.itmayiedu.test01.service.UserServiceTest01;
import com.itmayiedu.test02.service.UserServiceTest02;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@Slf4j
public class TestController { @Autowired
private UserServiceTest01 userServiceTest01;
@Autowired
private UserServiceTest02 userServiceTest02; @RequestMapping("/testString")
public String testString(){
log.info("testString");
return "springboot01 test";
} @RequestMapping("/insert")
@Transactional(transactionManager = "test1TransactionManager")
public Integer insertRole(String role_name,String note){
log.info("111111");
Integer a = userServiceTest01.insertRole(role_name, note);
int b = 100/Integer.valueOf(note);
return a;
} @RequestMapping("/update")
@Transactional(transactionManager = "test2TransactionManager")
public Integer updateRole(String role_name,String note){
log.info("222222");
Integer a = userServiceTest02.insertRole(role_name, note);
int b = 100/Integer.valueOf(note);
return a;
}
}

  启动类application:

package com.itmayiedu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@ComponentScan
@MapperScan("com.itmayiedu.*.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

这里面是添加    @MapperScan("com.itmayiedu.*.mapper")    在启动是扫包,或者也可以在 每个dao层添加@Mapper注解。

  UserMapperTest01:

package com.itmayiedu.test01.mapper;

import com.itmayiedu.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository; @Repository
public interface UserMapperTest01 {
@Select("SELECT * FROM T_ROLE WHERE ROLE_NAME = #{role_name}")
User findByName(@Param("role_name") String role_name);
@Insert("INSERT INTO T_ROLE(ROLE_NAME, NOTE) VALUES(#{role_name}, #{note})")
int insert(@Param("role_name") String role_name, @Param("note") String note);
}

  UserServiceTest01:

package com.itmayiedu.test01.service;

import com.itmayiedu.test01.mapper.UserMapperTest01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserServiceTest01 { @Autowired
private UserMapperTest01 userMapperTest01;
public Integer insertRole(String role_name,String note){
return userMapperTest01.insert(role_name, note);
}
}

springboot整合多数据源及事物的更多相关文章

  1. springBoot整合多数据源

    springBoot整合相关 1:springBoot整合多数据源: 应用场景:     项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...

  2. springboot整合多数据源解决分布式事务

    一.前言        springboot整合多数据源解决分布式事务.             1.多数据源采用分包策略              2.全局分布式事务管理:jta-atomikos. ...

  3. SpringBoot整合多数据源实现

    项目架构 1.导入相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  4. SpringBoot整合Druid数据源

    关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...

  5. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  6. SpringBoot整合Mybatis多数据源 (AOP+注解)

    SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...

  7. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  8. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  9. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

随机推荐

  1. WIN10下Java环境变量配置

    首先,你应该已经安装了 Java 的 JDK 了(如果没有安装JDK,请跳转到此网址:http://www.oracle.com/technetwork/java/javase/downloads/i ...

  2. HDOJ-2011

    #include<iostream> #include<cstdio> using namespace std; int main(){ int m,n,i; float su ...

  3. 『高性能模型』HetConv: HeterogeneousKernel-BasedConvolutionsforDeepCNNs

    论文地址:HetConv 一.现有网络加速技术 1.卷积加速技术 作者对已有的新型卷积划分如下:标准卷积.Depthwise 卷积.Pointwise 卷积.群卷积(相关介绍见『高性能模型』深度可分离 ...

  4. Activiti流程变量五步曲 ——by fightingKing

    http://blog.csdn.net/zwk626542417/article/details/46648139 一.前言 上一篇文章我们将流程实例的启动与查询,任务的办理查询都进行了介绍,我们这 ...

  5. 微信小程序如何使用iconfont阿里巴巴图标库?

    步骤: 1.如图先下载:  2.找到iconfont.css改为iconfont.css 3.修改iconfont.wxss文件的内容,如图复制内容至其文件 4.替换到文件页面当中 5.去页面中引入i ...

  6. ASP.NET Razor - 标记

    目录 什么是 Razor? Razor 帮助器 ASP.NET Razor - C# 和 VB 代码语法 主要的 Razor C# 语法规则 它是如何工作的? 使用对象 If 和 Else条件 读取用 ...

  7. java8实战一------解决冗杂,java8真的很便利(抛砖)

    你的代码很容易因为需求而变化,对自己代码改来改去的你一定会觉得烦的.在我看来,java8很容易的解决了这个问题. 先来看看例子!在一堆苹果里,筛选绿色的苹果.当然,Apple类是这样子. class ...

  8. Python3+slowloris安装使用教程

    一.说明 今天提到slowloris,这东西看着很眼熟,应该是以前局方打算用来刷竞赛积分的工具.我总觉得DoS没什么意思,但记不得怎么用了所以还是研究一下. 二.安装 slowloris就是一个pyt ...

  9. 说说Android项目中的armeabi,armeabi-v7a和x86

    1.区别    这三者都表示的是CPU类型,早期的Android系统几乎只支持ARMv5的CPU架构,但是现在已经有7种了.ARMv5,ARMv7 (从2010年起),x86 (从2011年起),MI ...

  10. selenium自动化实例: 多层框架中关于iframe的定位,以及select下拉框选择

    对于一个自动化的初学者来说会很常见的遇到元素明明存在却始终定位不到, 从而导致脚本报错,当然定位不到元素的原因很多, 其中一种就是多层框架iframe导致的 下方截图示意: 下方为写脚本时候的示例并其 ...