Springboot集成mybatis-plus多数据源配置


#pom依赖

<!--多数据源-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

 

#配置文件

spring:
  datasource:
    dynamic:
      druid:
        initial-size: 5 #连接池初始化大小
        min-idle: 10 #最小空闲连接数
        max-active: 20 #最大连接数
      primary: mysql #设置默认的数据源
      datasource:
        mysql:
          url: jdbc:mysql://192.168.1.250:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
          username: root
          password: 123456
        sqlserver-yw:          url: jdbc:sqlserver://192.168.1.250:1433;DatabaseName=test2
          username: sa
          password: 123456
        sqlserver-bz:
          url: jdbc:sqlserver://192.168.1.250:1433;DatabaseName=test3
          username: sa
          password: 123456

 

#配置类

@EnableTransactionManagement
@Configuration
@MapperScan("com.example.test.mapper")
public class MyBatisPlusConfig {
}

 

#启动类

SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class TestApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
 
}

 

#Service层

1、默认数据源

@Service
public class BaseUserInfoServiceImpl extends ServiceImpl<BaseUserInfoMapper, BaseUserInfo> implements IBaseUserInfoService {
 
}

2、sqlserver-bz数据源

@Service
@DS("sqlserver-bz")
public class YhbServiceImpl extends ServiceImpl<YhbMapper, Yhb> implements IYhbService {
 
}

3、sqlserver-yw数据源

@Service
@DS("sqlserver-yw")
public class ZybrxxbServiceImpl extends ServiceImpl<ZybrxxbMapper, Zybrxxb> implements IZybrxxbService {
 
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM