spring boot mybatis多多数据源解决方法
在我们的项目中不免会遇到需要在一个项目中使用多个数据源的问题,像我在得到一个任务将用户的聊天记录进行迁移的时候,就是用到了三个数据源,当时使用的AOP的编程方式根据访问的方法的不同进行动态的切换数据源,觉得性能不太好,先在又新用到了一种使用方式,觉得不错,记录下来。
介绍一下DEMO项目,使用的spring boot集成mybatis,mybatis查询数据库是基于注解形式查询的,目的查询两个数据库test1和test2的用户信息,并在控制台打印。
1.pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.27</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>
**/*.xml
</include>
</includes>
</resource>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
其中添加了alibaba的druid的数据源代替了spring boot默认的hikacp,注意mysql的驱动的版本号要与自己所使用的mysql的版本号保持一致,其中build模块里面的resources里面是为了防止spring boot 过滤掉src/main/java的XML文件,毕竟有的人喜欢mybatsi查询数据库的时候使用的是XML映射文件,不过我们本次使用的 是注解的形式 ,所以<resources>里面的内容在项目中没有用到。如果直接使用注解,可以忽略该内容。
2.用户类
public class User {
public Integer id;
public String name;
public String address;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
//get set方法省略...........
}
用户类没有什么好说的,就是基本的几个属性。
3.application.properties文件配置
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.username=root
spring.datasource.one.password=123456
spring.datasource.one.url=jdbc:mysql://localhost:3306/test1 spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.username=root
spring.datasource.two.password=123456
spring.datasource.two.url=jdbc:mysql://localhost:3306/test2
这里主要配置了两个数据库的访问属性,注意两个的区别为one何two的前缀不同,方便在下面使用spring boot的类安全属性的方式创建不同的数据源。
4.根据不同的前缀创建不同的数据源
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.one")
public DataSource dsOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.two")
public DataSource dsTwo(){
return DruidDataSourceBuilder.create().build();
}
}
该类位于主目录的config目录下面,使用@ConfigurationProperties注解表明对应的DataSource创建的时候使用的配置文件的内容。
5.在主目录下面分别创建mapper1和mapper2包,在对应的包下面创建对应的数据层访问接口UserMapper1和UserMapper2,内容都如下所示
@Mapper
public interface UserMapper1 {
@Select("select * from users")
List<User> getAllUser();
}
这接口里面没有什么好说的就是一个简单的mytatis的基于注解的查询所有用户的接口。
6.在config包下面创建不同的配置类MybatisConfigOne和MybatisConfigTwo两个类分别对应去扫描mapper1和mapper2两个路径下面的dao层接口。
@Configuration
@MapperScan(basePackages = "com.hopec.mybatis.mapper1",sqlSessionFactoryRef = "sqlSessionFactory1",
sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MybatisConfigOne {
@Autowired
@Qualifier("dsOne")
DataSource ds1;
@Bean
SqlSessionFactory sqlSessionFactory1(){
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds1);
try {
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean
SqlSessionTemplate sqlSessionTemplate1(){
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
public class MybatisConfigTwo {
@Autowired
@Qualifier("dsTwo")
DataSource ds2;
@Bean
SqlSessionFactory sqlSessionFactory2(){
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds2);
try {
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean
SqlSessionTemplate sqlSessionTemplate2(){
return new SqlSessionTemplate(sqlSessionFactory2());
}
}
7.测试
@SpringBootTest
class MybatisApplicationTests {
@Autowired
UserMapper1 userMapper1;
@Autowired
UserMapper2 userMapper2;
@Test
void contextLoads() {
List<User> users = userMapper1.getAllUser();
System.out.println(users);
List<User> allUser = userMapper2.getAllUser();
System.out.println(allUser); } }
测试结果:

如果想使用多个数据源,就继续增加就可以了,ok,大功告成了!
spring boot mybatis多多数据源解决方法的更多相关文章
- spring boot mybatis 多数据源配置
package com.xynet.statistics.config.dataresources; import org.springframework.jdbc.datasource.lookup ...
- spring boot Mybatis多数据源配置
关于 有时候,随着业务的发展,项目关联的数据来源会变得越来越复杂,使用的数据库会比较分散,这个时候就会采用多数据源的方式来获取数据.另外,多数据源也有其他好处,例如分布式数据库的读写分离,集成多种数据 ...
- Spring Boot + Mybatis 多数据源配置实现读写分离
本文来自网易云社区 作者:王超 应用场景:项目中有一些报表统计与查询功能,对数据实时性要求不高,因此考虑对报表的统计与查询去操作slave db,减少对master的压力. 根据网上多份资料测试发现总 ...
- spring boot项目自定义数据源,mybatisplus分页、逻辑删除无效解决方法
Spring Boot项目中数据源的配置可以通过两种方式实现: 1.application.yml或者application.properties配置 2.注入DataSource及SqlSessio ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
- Spring Boot + Mybatis 配置多数据源
Spring Boot + Mybatis 配置多数据源 Mybatis拦截器,字段名大写转小写 package com.sgcc.tysj.s.common.mybatis; import java ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
- Spring Boot MyBatis 数据库集群访问实现
Spring Boot MyBatis 数据库集群访问实现 本示例主要介绍了Spring Boot程序方式实现数据库集群访问,读库轮询方式实现负载均衡.阅读本示例前,建议你有AOP编程基础.mybat ...
- spring boot + mybatis + layui + shiro后台权限管理系统
后台管理系统 版本更新 后续版本更新内容 链接入口: springboot + shiro之登录人数限制.登录判断重定向.session时间设置:https://blog.51cto.com/wyai ...
随机推荐
- 性能测试瓶颈判断(LR&Windowns)
性能测试瓶颈判断(LR&Windowns) 一.判断CPU瓶颈(Processor) 1, %processor time 如果该值持续超过95%,表明瓶颈是CPU.可以考虑增加一个处理器或换 ...
- 一步步构建.NET Core Web应用程序---基本项目结构
前言 随着.NET Core日益成熟, 我作为C#&.NET体系中的一份子也加入了.NET Core 这一体系中,随着不断学习, 接触到的各种框架日益庞杂,接下来我会由一个新手的角度把整个基于 ...
- WebApi简介
简单创建.NET Core WebApi:https://www.cnblogs.com/yanbigfeg/p/9197375.html 登陆验证四种方式:https://www.cnblogs.c ...
- Android Studio [RecyclerView/瀑布流显示]
PuRecyclerViewActivity.java package com.xdw.a122.recyclerview; import android.support.v7.app.AppComp ...
- Spring boot 梳理 - 配置eclipse集成maven,并开发Spring boot hello
@RestController @EnableAutoConfiguration public class App { @RequestMapping("/hello") publ ...
- jquery 取得select选中的值
1.取得选中的值 jQuery("#select").val();是取得选中的值 2.取得的文本 jQuery("#select option:selected&quo ...
- CSS3 transform属性
说明: transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行移动(translate).旋转(rotate).缩放(scale)或倾斜(skew) transition属 ...
- 07-简单认识margin
margin 外边距,表示边框到最近盒子的距离. 对于左右两边 <!DOCTYPE html> <html lang="en"> <head> ...
- linux下搭建nginx+mysql+apache
对于开发人员来说,进行Web开发时可以用Apache进行网站测试,然而当一个Web程序进行发布时,Apache中并发性能差就显得很突出,这时配置一台Nginx服务器显得尤为重要. 以下是配置Nginx ...
- Docker4-docker私库的搭建及常用方法-docker-registry方式
一.简单介绍 前面已经介绍,可以使用Docker Hub公共仓库,但是大多数情况企业都需要创建一个本地仓库供自己使用.这里介绍几种搭建私库的方法 私库的好处有几点 1.节约带宽 2.可以自己定制系统 ...