多数据源配置方式

与世无争的帅哥 提交于 2020-08-13 13:42:40

一、pom.xml引入配置包

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

二、yml增加配置

spring:
  application:
    name: kzj-erp-bls
  datasource:
    # 使用druid数据源
    druid:
      stat-view-servlet:
        loginUsername: admin
        loginPassword: 123456
    dynamic:
      datasource:
        master:
          url: jdbc:sqlserver://192.168.60.2:1466;DatabaseName=master
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          username: sa
          password: 123456
          druid: #以下均为默认值
            initial-size: 3
            max-active: 8
            min-idle: 2
            max-wait: -1
            min-evictable-idle-time-millis: 30000
            max-evictable-idle-time-millis: 30000
            time-between-eviction-runs-millis: 0
            validation-query: select 1
            validation-query-timeout: -1
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            pool-prepared-statements: true
            max-open-prepared-statements: 100
            filters: stat,wall
            share-prepared-statements: true
        common:
          url: jdbc:sqlserver://192.168.60.2:1466;DatabaseName=common
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          username: sa
          password: 123456
          druid: #以下均为默认值
            initial-size: 3
            max-active: 8
            min-idle: 2
            max-wait: -1
            min-evictable-idle-time-millis: 30000
            max-evictable-idle-time-millis: 30000
            time-between-eviction-runs-millis: 0
            validation-query: select 1
            validation-query-timeout: -1
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            pool-prepared-statements: true
            max-open-prepared-statements: 100
            filters: stat,wall
            share-prepared-statements: true
      primary: master
      mp-enabled: true

三、实现类增加注解

定义接口类和接口实现类:
IERPService

@Slf4j
@Service
public class ERPServiceImpl  implements IERPService {}

IERPMasterService
@Slf4j
@Service
public class ERPMasterServiceImpl  extends ERPServiceImpl  implements IERPMasterService {}

IERPCommonService
@Slf4j
@Service

@DS("common")
public class ERPCommonServiceImpl  extends ERPServiceImpl  implements IERPCommonService {}

 

四、springboot启动类增加以下

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@MapperScan(basePackages = {"com.kzj.erp.mapper"})
@ComponentScan(basePackages = {"com.kzj","org.springframework.web"})
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@EnableScheduling
public class KZJPFErpPlatformApplication {
    public static void main(String[] args) {
        SpringApplication.run(KZJPFErpPlatformApplication.class, args);
    }

}

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!