MybatisPlus dynamic

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: MybatisPlus dynamic

一、整合mp

1、导入坐标

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

2、配置类 用于扫包 (或者或者使用注解在启动类上@MapperScanner)

@Configuration
public class MyBatisPlusConfig {
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();        scannerConfigurer.setBasePackage("com.cloud.demo");
        return scannerConfigurer;
    }
}

3、yml配置文件

mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml
   typeAliasesPackage: com.ftsino.citychallenge.common.entity,com.ftsino.citychallenge.common.po
   configuration:
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl


二、整合mp多数据源

1、引入dynamic依赖(动态数据源)

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

2、yml多数据源配置

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_2:
          url: ENC(xxxxx) # 内置加密,使用请查看详细文档
          username: ENC(xxxxx)
          password: ENC(xxxxx)
          driver-class-name: com.mysql.jdbc.Driver
          schema: db/schema.sql # 配置则生效,自动初始化表结构
          data: db/data.sql # 配置则生效,自动初始化数据
          continue-on-error: true # 默认true,初始化失败是否继续
          separator: ";" # sql默认分号分隔符
       #......省略
       #以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2

3、使用注解@DS 来切换指定的数据库

(指定的是yml中配置的名字 如果不指定 走yml中的默认配置数据库)

@DS("slave_1")
public interface AccountMapper extends BaseMapper<Account> {
}
public interface UserMapper extends BaseMapper<User> {
}



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9月前
TienChin 引入 MyBatisPlus
在父工程当中添加版本号,统一管理:
31 0
|
7月前
|
Java 关系型数据库 API
mybatisPlus之ActiveRecord模式及SimpleQuery使用
mybatisPlus之ActiveRecord模式及SimpleQuery使用
|
9月前
|
负载均衡 druid 算法
SpringBoot-MybatisPlus-Dynamic(多数据源)
SpringBoot-MybatisPlus-Dynamic(多数据源)
302 0
SpringBoot-MybatisPlus-Dynamic(多数据源)
|
10月前
|
缓存 JavaScript 前端开发
NestJS:理解ORM(Object Relational Mapping)
NestJS:理解ORM(Object Relational Mapping)
87 0
|
12月前
|
关系型数据库 MySQL 数据库
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
210 0
Simple TimerCallable Support
我们知道使用Callable可以获得线程中的返回值,它在 java.util.concurrent 包中声明。此接口还包含一个单一的、无参数的cal方法,此方法与Runnable接口的run方法类似,只是它可以返回一个值,并且可以抛出一个已检查的异常。事实上Callable也是Runnable,因为这两个接口都指定了一个有可能被另一个线程执行的类,只是它不受Runnable的限制。
87 0
Simple TimerCallable Support
|
Java Maven
关于org.springframework.cloud.context.named.NamedContextFactory$Specification 的问题!
关于org.springframework.cloud.context.named.NamedContextFactory$Specification 的问题!
333 0
关于org.springframework.cloud.context.named.NamedContextFactory$Specification 的问题!
MybatisPlus 中QueryWrapper 方法介绍
MybatisPlus 中QueryWrapper 方法介绍
115 0
MybatisPlus 中QueryWrapper 方法介绍
|
存储 Android开发 索引
resource.arsc解析之 Dynamic package reference
加载Theme出错 这是一篇补充文章,在做动态替换resId的过程中,我发现bag类型的ResTable_entry在使用过程中存在问题。比如style,其parent解析一直有问题
230 0