发布时间:2018-12-11
 
技术:springboot1.5.1 + maven3.0.1+ mybatis-plus-boot-starter2.3.1 + dynamic-datasource-spring-boot-starter2.4.2 + jdk1.8
 

概述

基于springboot的多数据源配置

详细

一、前言

本篇demo实现了springboot项目实现了多数据源切换的功能。对一些有多个数据库的项目来说是一个很好的参考。

关于动态数据源的切换的方案有很多,核心只有两种。一种是构建多套环境,另一种是基于spring原生的 AbstractRoutingDataSource 切换。如果你的数据源较少,场景不复杂,选择以上任意一种都可以。如果你需要更多特性,可以参考本demo。

二、实现过程(使用方法)

  1. 引入dynamic-datasource-spring-boot-starter。

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>

2.配置数据源。

spring:
datasource:
dynamic:
datasource:
master:
username: root
password: root
url: jdbc:mysql://localhost/master?characterEncoding=utf-8&useSSL=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
driver-class-name: com.mysql.jdbc.Driver
slave:
username: root
password: root
url: jdbc:mysql://localhost/slave?characterEncoding=utf-8&useSSL=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
driver-class-name: com.mysql.jdbc.Driver

3.使用 @DS 切换数据源

@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。

注解在service实现或mapper接口方法上,但不建议同时在service和mapper注解。

package com.zxh.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zxh.entity.User;
import com.zxh.mapper.UserMapper;
import com.zxh.service.UserService; import org.springframework.stereotype.Service; @Service
@DS("slave")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override
@DS("master")//这里必须包一层,不能调用mp默认的插入,因为会走到从库去
public void addUser(User user) {
baseMapper.insert(user);
} @Override
@DS("slave")
public User selectSlaveUser(Long id) {
return baseMapper.selectById(id);
}
}

4.集成mybatis-plus

只要进入mybatisPlus相关jar包,项目自动集成。 兼容mybatisPlus 2.x和3.x的版本。

只要注解在mybatisPlus的mapper或serviceImpl上即可完成mp内置方法切换。

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>

5.在本地的mysql分别建master跟slave两个数据库

6.在两个数据库分别建user表,表结构如下:

7.项目演示:

a).执行testAddUser

   @Test
public void testAddUser() {
User user = new User();
user.setName("测试用户" + random.nextInt());
user.setAge(random.nextInt(100));
userService.addUser(user);
}
    @Override
@DS("master")//这里必须包一层,不能调用mp默认的插入,因为会走到从库去
public void addUser(User user) {
baseMapper.insert(user);
}

因为我们在addUser里加了@DS("master")注解,所以会走到主库里面,可以看到master数据库插了一条记录:

b).执行testSelectById,slave数据库里面有一条数据如下:

 @Test
public void testSelectById() {
User user = userService.selectSlaveUser(2L);
System.out.println(user.getName());
}

执行后结果,可以看到后台打印了用户名

这样就实现了动态加载多数据源的效果。

三、项目结构图

三、常见问题

多个库的事物如何处理?

不能 不能 不能,一个业务操作涉及多个库不要加事务。

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

基于springboot的多数据源配置的更多相关文章

  1. springboot+ibatis 多数据源配置

    这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...

  2. springboot之多数据源配置JdbcTemplate

    springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework ...

  3. SpringBoot 的多数据源配置

    最近在项目开发中,需要为一个使用 MySQL 数据库的 SpringBoot 项目,新添加一个 PLSQL 数据库数据源,那么就需要进行 SpringBoot 的多数据源开发.代码很简单,下面是实现的 ...

  4. springboot mybatis 多数据源配置

    首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...

  5. Springboot+Druid 动态数据源配置监控

    一.引入maven依赖,使用 starter 与原生 druid 依赖配置有所不同 <dependency> <groupId>com.alibaba</groupId& ...

  6. springboot mybatis 多数据源配置支持切换以及一些坑

    一 添加每个数据源的config配置,单个直接默认,多个需要显示写出来 @Configuration @MapperScan(basePackages ="com.zhuzher.*.map ...

  7. SpringBoot:阿里数据源配置、JPA显示sql语句、格式化JPA查询的sql语句

    1 数据源和JPA配置 1.1 显示sql配置和格式化sql配置 者两个配置都是属于hibernate的配置,但是springdatajpa给我们简化了:所有hibernate的配置都在jpa下面的p ...

  8. SpringBoot项目多数据源配置

    博主总结的不错,事务也考虑到了,存一下: https://blog.csdn.net/xqnode/article/details/86498507

  9. 基于注解实现SpringBoot多数据源配置

    1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...

随机推荐

  1. 动态SQL(章节摘要)

    1,使用动态SQL能够在依赖对象不存在时创建子程序. 2.动态SQL主要利用EXECUTE IMMEDIATE语句运行DML,DDL,DCL等语句操作. 3,假设使用了绑定变量,则必须在EXECUTE ...

  2. String、StringBuffer与StringBuilder之间区别(转)

    原文链接:String.StringBuffer与StringBuilder之间区别 最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,Stri ...

  3. How to delete team project from TFS visual studio ?

    /* Author: Jiangong SUN */ To delete team project from TFS Visual Studio, you need to use "TFSD ...

  4. 从零开始学C++之模板(三):缺省模板参数(借助标准模板容器实现Stack模板)、成员模板、关键字typename

    一.缺省模板参数 回顾前面的文章,都是自己管理stack的内存,无论是链栈还是数组栈,能否借助标准模板容器管理呢?答案是肯定的,只需要多传一个模板参数即可,而且模板参数还可以是缺省的,如下: temp ...

  5. 异常捕获 UncaughtExceptionHandler MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. Android -- getWidth()与getMeasuredWidth()

    getWidth() Return the width of the your view. Returns The width of your view, in pixels. 源代码: public ...

  7. 用条件随机场CRF进行字标注中文分词(Python实现)

    http://www.tuicool.com/articles/zq2yyi   http://blog.csdn.net/u010189459/article/details/38546115 主题 ...

  8. Linux杀毒软件ClamAV初次体验

    1:官网 http://www.clamav.net 2:Ubuntu下安装ClamAV sudo apt-get update--更新系统 sudo apt-get install clamav-- ...

  9. 一分钟读懂互联网广告竞价策略GFP+GSP+VCG

    原文:http://ju.outofmemory.cn/entry/116780 一分钟读懂互联网广告竞价策略GFP+GSP+VCG 两个广告位,三家广告主竞价,广告平台究竟应该制定广告竞价策略呢?这 ...

  10. [Git] Change the commit message of my last commit

    Did you make a typo in your last commit message? No problem, we can use the git --amend command to c ...