SpringBoot整合多数据源实现
项目架构

1.导入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--freemarker支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2.application.properties
## test1 数据源配置
#spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8
#spring.datasource.test1.username=root
#spring.datasource.test1.password=123 ## test2 数据源配置
#spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8
#spring.datasource.test2.username=root
#spring.datasource.test2.password=123
3.创建datasource包 下TestMyBatisConfig1
@Configuration
@MapperScan(basePackages = "cn.happy.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
} @Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//读取mybatis小配置文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
} @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);
}
}
@Primary注解标识默认使用的数据源
如果不加启动会报如下错误:意思是有两个数据源 不知需要使用哪个数据源

3.创建datasource包 下TestMyBatisConfig2
@Configuration
@MapperScan(basePackages = "cn.happy.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
} @Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//读取mybatis小配置文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
return bean.getObject();
} @Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }
4.entity层
/**
* author: 刘涛
*
* @create 2018-04-04 11:18
*/
public class Book { private Integer bookid; private String bookname; private Integer bookprice; get set省略。。
}
5.test1下的dao中BookMapperTest1
public interface BookMapperTest1 {
@Select("select * from book where bookname=#{bookname}")
public Book findByName(@Param("bookname") String bookname);
@Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);
@Select("select * from book")
public Book findBook(@Param("Book") Book book);
}
6.test2下的dao中BookMapperTest2
public interface BookMapperTest2 {
@Select("select * from book where bookname=#{bookname}")
public Book findByName(@Param("bookname") String bookname);
@Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);
}
7.controller层
@RestController
public class BookController {
@Autowired
private BookMapperTest1 bookMapperTest1; @Autowired
private BookMapperTest2 bookMapperTest2; @RequestMapping("insert001")
public String insert001(String bookname,Double bookprice){
bookMapperTest1.insertBook(bookname,bookprice);
return "success";
} @RequestMapping("insert002")
public String insert002(String bookname,Double bookprice){
bookMapperTest2.insertBook(bookname,bookprice);
return "success";
}
}
8.启动项目

test1数据库

test2数据库

SpringBoot整合多数据源实现的更多相关文章
- springBoot整合多数据源
springBoot整合相关 1:springBoot整合多数据源: 应用场景: 项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...
- springboot整合多数据源解决分布式事务
一.前言 springboot整合多数据源解决分布式事务. 1.多数据源采用分包策略 2.全局分布式事务管理:jta-atomikos. ...
- springboot整合多数据源及事物
有两种方式:一种是分包的方式.一种是加注解的方式(@DataSource(ref="")). 分包方式:项目结构图如下: 分为com.itmayiedu.test01.com.it ...
- SpringBoot整合Druid数据源
关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合阿里Druid数据源及Spring-Data-Jpa
SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- SpringBoot整合Mybatis多数据源 (AOP+注解)
SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...
随机推荐
- mac 配置jdk,maven环境变量
Java和maven环境变量配置: 1.打开终端:输入命令:vi ~/.bash_profile 2.再输入 i 进入编辑模式 输入以下: export JAVA_HOME=/Library/Java ...
- Android Studio安装后配置默认新工程目录以及.gradle,.android,.m2和system,config目录
关于在哪里设置:以下所有设置都是在没有打开工程的前提下设置的, Configure > Settings 如图: 不要使用 Configure > Project Defaults> ...
- mysql5.x安装脚本
直接贴出来: #!/bin/bash #linux安装mysql服务分两种安装方法: #①源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: #②使 ...
- Mysql5.7数据导出提示--secure-file-priv选项问题的解决方法
mysql可使用into outfile参数把表中的数据到处到csv,示例如下: select user_id from weibo_comment into outfile '/home/dazha ...
- adb pull 报错处理:adb: error: cannot create file/directory 'E:\': No such file or directory
adb pull /sdcard/1.txt e:/ 报错:adb: error: cannot create file/directory 'E:\': No such file or direct ...
- kafka单机安装和启动
1.下载并解压到/usr/local/src目录下 2.运行kafka需要使用Zookeeper,先启动Zookeeper,如果没有Zookeeper,可以使用kafka自带打包和配置好的Zookee ...
- 一个 戴尔 dell 笔记本 bios Preparing to begin setup 问题
昨天帮亲戚安装系统,是一个dell 笔记本,原本想的很简单,但是在修改了bios里的 SATA 模式后,不但系统启动不了,连bios都进不去了,就像一直在检测一个错误的硬件.google了很多,也没有 ...
- vue环境下新建项目
1.之前电脑上安装了node和npm,查看下版本信息. 2.现在安装vue-cli脚手架,可以全局安装: npm install --global vue-cli 之前自己电脑没有安装过webpac ...
- swoole异步群发模板消息
1.用的是TP5.1的框架,swoole分成一个客户端发送接收消息,一个服务器负责处理信息 服务端代码,服务器要先安装swoole拓展,用 php server.php 启动进程监听 <?php ...
- 生产宕机dunp配置
修改线程数 <self-tuning-thread-pool-size-min>100</self-tuning-thread-pool-size-min> <self- ...