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=& ...
随机推荐
- python基础1之 由来、种类、优缺点、安装环境
python基础1之由来.种类.优缺点.安装环境 一.前世今生 Python的创始人是吉多·范罗苏姆(Guido van Rossum),在1989年开发.今年最新的编程语言排行榜中,python名列 ...
- 解锁技能:sass + node-sass多页面应用编译(转载)
传送门:https://blog.csdn.net/wx11408115/article/details/78023466
- 安装版本( 相关关系 Vue SSR 指定版本)
1. 关于 brew mac包管理器 终端安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebr ...
- 20175204 张湲祯 2018-2019-2《Java程序设计》
Arrays和String单元测试 一.类的作用 1.- String类 charAt String的charAt的作用是将字符串中第i个位置上的字符(从0开始计数)赋值给n,其用法为n=string ...
- webpack2 实践
实例gif图: 目录截图: 目录介绍: dist目录(最后生成的目录,里面文件为配置webpack自动生成的): c/:css文件夹; i/:img文件夹; j/:js文件夹; src目录下(开发目录 ...
- Python爬虫基础之UrlError
一.urllib.error python的urllib.error模块主要是应对urllib.request在网络请求过程中出现的异常而定义的异常处理类.主要有URLError和HTTPError两 ...
- python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
正则表达式(regular expression)是一个特殊的字符序列,描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子字符串. 将匹配的子字符串替换或者从某个字符串中取出符合某个条件 ...
- MUI上传图片之选择相册和相机上传
1.因为项目中有三处地方需要上传,所以html中存在三处地方.身份证正反面为上传一张,发票限制上传9张. <div class="action1"> <!--展示 ...
- 导出pip安装的所有放入一个文件中,并把通过这个安装所有的包
导出pip安装的所有的包: pip freeze > piplist.txt 在新的环境中安装导出的包 pip install -r piplist.txt
- 《剑指offer》数组中的逆序对
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结: