最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、背景

使用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

三、項(xiàng)目結(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ū)動(dòng) -->
        <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默認(rèn)值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默認(rèn)值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  # 配置獲取連接等待超時(shí)的時(shí)間
        validation-query: select version()
        time-between-eviction-runs-millis: 60000  # 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
        min-evictable-idle-time-millis: 300000  # 指定一個(gè)空閑連接最少空閑多久后可被清除,單位是毫秒
        filters: config,wall,stat  # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'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)閉   個(gè)人建議如果想用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  # 配置獲取連接等待超時(shí)的時(shí)間
        validation-query: select 'x'
        time-between-eviction-runs-millis: 60000  # 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
        min-evictable-idle-time-millis: 300000  # 指定一個(gè)空閑連接最少空閑多久后可被清除,單位是毫秒
        filters: config,wall,stat  # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'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)閉   個(gè)人建議如果想用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 {
    // 將這個(gè)對象放入Spring容器中
    @Bean(name = "oneDataSource")
    // 表示這個(gè)數(shù)據(jù)源是默認(rèn)數(shù)據(jù)源
    @Primary
    // 讀取application.properties中的配置參數(shù)映射成為一個(gè)對象
    // prefix表示參數(shù)的前綴
    @ConfigurationProperties(prefix = "spring.datasource.druid.db1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
 
    @Bean(name = "oneSqlSessionFactory")
    // 表示這個(gè)數(shù)據(jù)源是默認(rèn)數(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")
    // 表示這個(gè)數(shù)據(jù)源是默認(rèn)數(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 {
    // 將這個(gè)對象放入Spring容器中
    @Bean(name = "twoDataSource")
    // 讀取application.properties中的配置參數(shù)映射成為一個(gè)對象
    // prefix表示參數(shù)的前綴
    @ConfigurationProperties(prefix = "spring.datasource.druid.db2")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }
 
    @Bean(name = "twoSqlSessionFactory")
    // 表示這個(gè)數(shù)據(jù)源是默認(rèn)數(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")
    // 表示這個(gè)數(shù)據(jù)源是默認(rèn)數(shù)據(jù)源
    //@Primary
    public SqlSessionTemplate oneSqlSessionTemplate(
            @Qualifier("twoSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
 
}

七、啟動(dòng)類配置

關(guān)鍵點(diǎ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é)

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

相關(guān)文章

  • java自動(dòng)生成編號的實(shí)現(xiàn)(格式:yyMM+四位流水號)

    java自動(dòng)生成編號的實(shí)現(xiàn)(格式:yyMM+四位流水號)

    這篇文章主要介紹了java自動(dòng)生成編號的實(shí)現(xiàn)(格式:yyMM+四位流水號),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • IDEA在創(chuàng)建包時(shí)如何把包分開實(shí)現(xiàn)自動(dòng)分層(方法詳解)

    IDEA在創(chuàng)建包時(shí)如何把包分開實(shí)現(xiàn)自動(dòng)分層(方法詳解)

    這篇文章主要介紹了IDEA在創(chuàng)建包時(shí)如何把包分開實(shí)現(xiàn)自動(dòng)分層,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • idea創(chuàng)建maven項(xiàng)目速度慢的三種解決方案

    idea創(chuàng)建maven項(xiàng)目速度慢的三種解決方案

    這篇文章主要介紹了idea創(chuàng)建maven項(xiàng)目速度慢的三種解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Java中的ByteArrayInputStream詳解

    Java中的ByteArrayInputStream詳解

    Java中,ByteArrayInputStream類是實(shí)現(xiàn)內(nèi)存級別的字節(jié)流讀取的工具,可以從字節(jié)數(shù)組中讀取數(shù)據(jù),這個(gè)類位于java.io包中,繼承自InputStream,ByteArrayInputStream的主要特點(diǎn)有:在內(nèi)存中操作,不涉及磁盤IO,可以重用流讀取數(shù)據(jù)
    2024-09-09
  • mybatis實(shí)現(xiàn)遍歷Map的key和value

    mybatis實(shí)現(xiàn)遍歷Map的key和value

    這篇文章主要介紹了mybatis實(shí)現(xiàn)遍歷Map的key和value方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java獲取任意http網(wǎng)頁源代碼的方法

    Java獲取任意http網(wǎng)頁源代碼的方法

    這篇文章主要介紹了Java獲取任意http網(wǎng)頁源代碼的方法,可實(shí)現(xiàn)獲取網(wǎng)頁代碼以及去除HTML標(biāo)簽的代碼功能,涉及Java正則操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • Springboot中yml文件不生效原因分析及解決

    Springboot中yml文件不生效原因分析及解決

    這篇文章給大家介紹了Springboot中yml文件不生效原因分析及解決方法,通過圖文給大家講解的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • Java獲取隨機(jī)數(shù)的n種方法

    Java獲取隨機(jī)數(shù)的n種方法

    項(xiàng)目中,我們常常會(huì)用到隨機(jī)數(shù),本文主要介紹了Java獲取隨機(jī)數(shù)的n種方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • 淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別

    淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別

    本文主要介紹了淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java 基礎(chǔ)面試真題:String 為什么是不可變的?

    Java 基礎(chǔ)面試真題:String 為什么是不可變的?

    本文主要介紹了String、StringBuffer和StringBuilder的區(qū)別,以及String不可變的原因,String不可變的原因是其內(nèi)部使用final修飾的char數(shù)組保存字符串,同時(shí)String類沒有提供修改字符串的方法,在Java9之后,String的底層實(shí)現(xiàn)由char[]改成了byte[],這是為了節(jié)省內(nèi)存空間
    2025-01-01

最新評論

新田县| 平江县| 沙雅县| 茌平县| 贵南县| 洞口县| 大名县| 南涧| 三明市| 英德市| 偏关县| 林州市| 铜梁县| 沁阳市| 宿州市| 广平县| 独山县| 武宣县| 新河县| 武冈市| 沙河市| 麦盖提县| 静海县| 洛南县| 闻喜县| 临沧市| 芷江| 塔城市| 平原县| 九台市| 酉阳| 兴业县| 南康市| 曲松县| 阳朔县| 耿马| 鄂托克旗| 贺兰县| 靖江市| 晋城| 宁河县|