欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot使用druid配置多數(shù)據(jù)源問題

 更新時間:2023年03月11日 10:02:24   作者:LoveDR_1995  
這篇文章主要介紹了SpringBoot使用druid配置多數(shù)據(jù)源問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、背景

使用spring boot配置多數(shù)據(jù)源,數(shù)據(jù)源分別為postgresql、mysql

二、版本介紹

  • spring boot——2.5.4
  • druid——1.2.11
  • postgresql——12
  • mysql——8.0.16
  • maven——3.0
  • idea——2019

三、項目結(jié)構(gòu)

java package目錄

resource目錄存放mapper.xml文件,按照數(shù)據(jù)源創(chuàng)建package 

 

四、maven依賴 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- MySql驅(qū)動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

五、yaml配置文件

server:
  port: 8081
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      web-stat-filter:
        enabled: true #是否啟用StatFilter默認值true
        url-pattern: /*
        exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
        session-stat-enable: true
        session-stat-max-count: 10
      stat-view-servlet:
        enabled: true #是否啟用StatViewServlet默認值true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: admin
        allow:
 
      db1:
        username: postgres
        password: localhost
        url: jdbc:postgresql://localhost:5432/test
        driver-class-name: org.postgresql.Driver
        initial-size: 5  # 初始化大小
        min-idle: 5  # 最小
        max-active: 100  # 最大
        max-wait: 60000  # 配置獲取連接等待超時的時間
        validation-query: select version()
        time-between-eviction-runs-millis: 60000  # 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
        min-evictable-idle-time-millis: 300000  # 指定一個空閑連接最少空閑多久后可被清除,單位是毫秒
        filters: config,wall,stat  # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻
        # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
        connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false
        test-while-idle: true
        test-on-borrow: true
        test-on-return: false
        # 是否緩存preparedStatement,也就是PSCache  官方建議MySQL下建議關(guān)閉   個人建議如果想用SQL防火墻 建議打開
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
 
      db2:
        username: root
        password: localhost
        url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
        driver-class-name: com.mysql.cj.jdbc.Driver
        initial-size: 5  # 初始化大小
        min-idle: 5  # 最小
        max-active: 100  # 最大
        max-wait: 60000  # 配置獲取連接等待超時的時間
        validation-query: select 'x'
        time-between-eviction-runs-millis: 60000  # 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
        min-evictable-idle-time-millis: 300000  # 指定一個空閑連接最少空閑多久后可被清除,單位是毫秒
        filters: config,wall,stat  # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻
        # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
        connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false
        test-while-idle: true
        test-on-borrow: true
        test-on-return: false
        # 是否緩存preparedStatement,也就是PSCache  官方建議MySQL下建議關(guān)閉   個人建議如果想用SQL防火墻 建議打開
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
 
mybatis:
  mapper-locations: classpath:com/demo/mapper/*.xml
  type-aliases-package: com.demo.entity
  configuration:
    log-impl:
    mapUnderscoreToCamelCase: true
 
#showSql
logging:
  level:
    java.sql: debug
    org.apache.ibatis: debug
    com.demo.mapper: debug
  config: classpath:logback-spring.xml

六、數(shù)據(jù)源配置文件

@Configuration
@MapperScan(basePackages = "com.demo.mapper.postgre.**", sqlSessionFactoryRef = "oneSqlSessionFactory")
public class DataSourceConfig1 {
    // 將這個對象放入Spring容器中
    @Bean(name = "oneDataSource")
    // 表示這個數(shù)據(jù)源是默認數(shù)據(jù)源
    @Primary
    // 讀取application.properties中的配置參數(shù)映射成為一個對象
    // prefix表示參數(shù)的前綴
    @ConfigurationProperties(prefix = "spring.datasource.druid.db1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
 
    @Bean(name = "oneSqlSessionFactory")
    // 表示這個數(shù)據(jù)源是默認數(shù)據(jù)源
    @Primary
    // @Qualifier表示查找Spring容器中名字為oneDataSource的對象
    public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 設(shè)置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:com.demo.mapper.postgre/*.xml"));
        return bean.getObject();
    }
 
    @Bean("oneSqlSessionTemplate")
    // 表示這個數(shù)據(jù)源是默認數(shù)據(jù)源
    @Primary
    public SqlSessionTemplate oneSqlSessionTemplate(
            @Qualifier("oneSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
 
}
@Configuration
@MapperScan(basePackages = "com.demo.mapper.mysql", sqlSessionFactoryRef = "twoSqlSessionFactory")
public class DataSourceConfig2 {
    // 將這個對象放入Spring容器中
    @Bean(name = "twoDataSource")
    // 讀取application.properties中的配置參數(shù)映射成為一個對象
    // prefix表示參數(shù)的前綴
    @ConfigurationProperties(prefix = "spring.datasource.druid.db2")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
 
    @Bean(name = "twoSqlSessionFactory")
    // 表示這個數(shù)據(jù)源是默認數(shù)據(jù)源
    //@Primary
    // @Qualifier表示查找Spring容器中名字為oneDataSource的對象
    public SqlSessionFactory oneSqlSessionFactory(@Qualifier("twoDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 設(shè)置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:com.demo.mapper.mysql/*.xml"));
        return bean.getObject();
    }
 
    @Bean("twoSqlSessionTemplate")
    // 表示這個數(shù)據(jù)源是默認數(shù)據(jù)源
    //@Primary
    public SqlSessionTemplate oneSqlSessionTemplate(
            @Qualifier("twoSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
 
}

七、啟動類配置

關(guān)鍵點:去除  exclude = {DataSourceAutoConfiguration.class} 及掃描 com.demo.mapper目錄

@MapperScan("com.demo.mapper")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication .class, args);
    }
}

八、druid管理頁面

輸入地址 localhost://8081/druid,輸入 admin/admin     

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論