spring boot系列之動態資料來源

2021-09-11 01:24:19 字數 2477 閱讀 5514

// 動態資料來源配置檔案

spring:

datasource:

type: com.alibaba.druid.pool.druiddatasource // druid資料庫連線池

driverclassname: com.mysql.jdbc.driver

druid:

first: 

url: jdbc:mysql://localhost:3306/test1

username: root

password: root

second: 

url: jdbc:mysql://localhost:3306/test2

username: root

password: root

initial-size: 10

max-active: 100

min-idle: 10

max-wait: 60000

// 預快取設定,pscache對支援游標的資料庫oracle效能提公升巨大,jdbc的標準引數用以控制資料源內載入的preparedstatements數量,預快取的statements屬單個connection而不是整個連線池。

pool-prepared-statements: true

//單個連線擁有的最大快取數:要啟用pscache必須配置大於0,當大於0時,poolpreparedstatements自動觸發修改為true。在druid中,不會存在oracle下pscache占用記憶體過多的問題,可以把這個數值配置大一些。

max-pool-prepared-statement-per-connection-size: 20

time-between-eviction-runs-millis: 60000 // 每1分鐘執行一次空閒連線**執行緒

min-evictable-idle-time-millis: 300000 // 池中的連線空閒30分鐘後被**

validation-query: select 1 from dual

test-while-idle: true

test-on-borrow: false

test-on-return: false

stat-view-servlet:

enabled: true

url-pattern: /druid/*

#login-username: admin

#login-password: admin

filter:

stat:

log-slow-sql: true

slow-sql-millis: 1000 // 慢查詢1秒

merge-sql: true // 合併sql,增強統計效果

wall:

config:

multi-statement-allow: true // 允許一次執行多條sql

/*** 動態資料來源配置類

*/@configuration

public class dynamicdatasourceconfig

@bean

@configurationproperties("spring.datasource.druid.second")

public datasource seconddatasource()

@bean

@primary

public dynamicdatasource datasource(datasource firstdatasource, datasource seconddatasource)

}/**

* 動態資料來源

*/public class dynamicdatasource extends abstractroutingdatasource

@override

protected object determinecurrentlookupkey()

public static void setdatasource(string datasource)

public static string getdatasource()

public static void cleardatasource()

}/**

* 資料來源名稱

*/public inte***ce datasourcenames

/*** 多資料來源註解

*/@target(elementtype.method)

@retention(retentionpolicy.runtime)

@documented

public @inte***ce datasource

/*** 多資料來源切面類

*/@aspect

@component

public class datasourceaspect implements ordered else

try finally

}

SpringBoot之小彩蛋 動態Banner

玩過springboot的小夥伴對下面這張圖一定不陌生 沒錯,這是springboot啟動時console中列印的圖形。springboot還有乙個小功能,更換banner,這個圖示是可以換的。在resources資料夾下新建banner.txt。可以到這個 去,將將自己想要列印的文字轉成上面這種a...

Spring Boot 系列教程

spring boot 系列教程 spring boot 快速入門教程 spring boot 整合swagger文件 spring boot 整合mybatis框架 spring boot 實現mybatis分頁 spring boot 整合druid資料來源 spring boot 實現myba...

SpringBoot靜態動態

在src main resources下面有兩個資料夾,static和templates springboot預設 static中放靜態頁面,而templates中放動態頁面 靜態頁面 這裡我們直接在static放乙個hello.html,然後直接輸入http localhost 8080 hell...