SpringBoot 分包方式多数据源
1、引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<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>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2、配置application.properties中配置多个数据源
#slave dataSource数据源
spring.datasource.slave.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.slave.jdbc-url =jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8
spring.datasource.slave.username =root
spring.datasource.slave.password =root
#master dataSource数据源
spring.datasource.master.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.master.jdbc-url =jdbc:mysql://localhost:3306/study01?useUnicode=true&characterEncoding=utf-8
spring.datasource.master.username =root
spring.datasource.master.password =root
3、创建多个配置类
数据源1;
@Configuration
@MapperScan(basePackages =DataSourceMaterConfig.PACKAGE,sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DataSourceMaterConfig {
// master dao所在的包
public static final String PACKAGE = "com.yehui.mapper.master";
//配置文件所在目录
//private static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml"; @Bean(name = "mapper/master")
@Primary //默认访问
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource msaterDataSource() {
return DataSourceBuilder.create().build();
} // 创建Session
@Bean(name = "masterSqlSessionFactory")
@Primary //默认访问
public SqlSessionFactory sqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(msaterDataSource());
/* 使用配置文件是加入
Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources(DataSourceMaterConfig.MAPPER_LOCATION);
sqlSessionFactoryBean.setMapperLocations(mapperLocations);*/
return sqlSessionFactoryBean.getObject();
} // 数据源事务管理器
@Bean(name = "masterDataSourceTransactionManager")
@Primary //默认访问
public DataSourceTransactionManager masterDataSourceTransactionManager() {
return new DataSourceTransactionManager(msaterDataSource());
}
}
数据源2:
@Configuration
@MapperScan(basePackages =DataSourceSalveConfig.PACKAGE,sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class DataSourceSalveConfig {
// slave dao所在的包
public static final String PACKAGE = "com.yehui.mapper.slave";
//配置文件所在目录
private static final String MAPPER_LOCATION = "classpath:mapper/slave/*.xml"; @Bean(name = "mapper/slave")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
} // 创建Session
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(slaveDataSource());
/*使用配置文件时加入 Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources(DataSourceSalveConfig.MAPPER_LOCATION);
sqlSessionFactoryBean.setMapperLocations(mapperLocations);*/
return sqlSessionFactoryBean.getObject();
} // 数据源事务管理器
@Bean(name = "slaveDataSourceTransactionManager")
public DataSourceTransactionManager slaveDataSourceTransactionManager() {
return new DataSourceTransactionManager(slaveDataSource());
}
}
4、创建多个包mapper
数据源1:
package com.yehui.mapper.master; import org.apache.ibatis.annotations.Select; import java.util.List;
import java.util.Map; public interface MasterMapper { @Select("SELECT * FROM tb_user")
public List<Map<String,Object>> selectList();
}
数据源2:
package com.yehui.mapper.slave; import org.apache.ibatis.annotations.Select; import java.util.List;
import java.util.Map; public interface SlaveMapper {
@Select("SELECT * FROM tb_user")
public List<Map<String,Object>> selectList();
}
5、创建service类
@Service
public class UserService { @Autowired
private MasterMapper masterMapper; @Autowired
private SlaveMapper savlesMapper; public List<Map<String,Object>> selectList1(){
return masterMapper.selectList();
} public List<Map<String,Object>> selectList2(){
return savlesMapper.selectList();
}
}
6、测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataSourceTest {
@Autowired
private UserService userService;
@Test
public void test1(){
List<Map<String, Object>> mapList = userService.selectList1();
System.out.println(mapList);
}
@Test
public void test2(){
List<Map<String, Object>> mapList = userService.selectList2();
System.out.println(mapList);
}
}
注意配置过程中如果报此错误
No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found amon
导致原因@Primary这个注解只能在一个包下存在,不能在其他存在
SpringBoot 分包方式多数据源的更多相关文章
- Android开源项目分包方式学习(eoe、oschina、github)
总感觉Android中关于分包的文章很少,或者几乎可以说没有.但是合理地分包,又可以使整个项目模块化,减少包与包之间的依赖,让整个项目的框架更加清晰,更利于后续功能的拓展. 因为没有相关的文章,所以这 ...
- SpringBoot整合阿里Druid数据源及Spring-Data-Jpa
SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...
- springboot 中使用Druid 数据源提供数据库监控
一.springboot 中注册 Servlet/Filter/Listener 的方式有两种,1 通过代码注册 ServletRegistrationBean. FilterRegistration ...
- springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required) 最近在项目中想试一下使用 Hikari 连接池,以前用 ...
- SpringBoot与动态多数据源切换
本文简单的介绍一下基于SpringBoot框架动态多数据源切换的实现,采用主从配置的方式,配置master.slave两个数据库. 一.配置主从数据库 spring: datasource: ty ...
- SpringBoot | 3.1 配置数据源
目录 前言 1. 数据源的自动配置 2. *数据源自动配置源码分析 2.1 DataSourceAutoConfiguration:数据源自动配置类 2.2 JdbcTemplateAutoConfi ...
- Eclipse版本android 65535解决方案(原理等同android studio现在的分包方式)
由于工作的需要看了下Eclipse下android65535的解决方案,查了好多文档,真心的发自内心的说一句请不要再拷贝别人的博客了,害人,真害人. 接下来我说下我的实现方式,首先说下65535的最可 ...
- FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法。。
[原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法. 1.单个NAL包单元 12字节 ...
- SpringBoot+MyBatis配置多数据源
SpringBoot 可以支持多数据源,这是一个非常值得学习的功能,但是从现在主流的微服务的架构模式中,每个应用都具有唯一且准确的功能,多数据源的需求很难用到,考虑到实际情况远远比理论复杂的多,这里还 ...
随机推荐
- 将SpringBoot默认Json解析框架jackson替换成fastjson
步骤一:引入依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...
- Unity脚本执行顺序自研框架
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52372611 作者:car ...
- MySQL之Schema与数据类型优化
选择优化的数据类型 MySQL支持的数据类型非常多,选择正确的数据类型对于获得高性能至关重要.不管存储哪种类型的数据,下面几个简单的原则都有助于做出更好的选择: 更小的通常更好一般情况下,应该尽量使用 ...
- POI-java下载excel-HSSFWorkbook
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- C#编程:正则表达式验证身份证校验码-10
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...
- ios开发学习笔记002-运算符
运算符 C语言有34种运算符,常见的有加减乘除. 算术运算符 1.加 10+2 2.减 20-2 3.乘 12*2 4.除 10/2 5.取余 10%3 = 1; 10%-3 = 1; -10%3 = ...
- 转载 hadoop 伪分布安装
一. 概要 经过几天的调试,终于在Linux Cent OS 5.5下成功搭建Hadoop测试环境.本次测试在一台服务器上进行伪分布式搭建.Hadoop 伪分布式模式是在单机上模拟 Ha ...
- Linux中date命令的各种实用方法
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://521cto.blog.51cto.com/950229/935642 在linu ...
- Unity --yield return
1.yield return null; //暂停协同程序,下一帧再继续往下执行 yield new WaitForFixedUpdate (); //暂停协同程序,等到下一次调用FixedUpdat ...