1.HikariCP單數據源
1.1 應用配置文件
在application.yml
文件中配置屬性,分別為:IP地址、用戶名、密碼、Driver
HikariCP連接池及其在springboot中的配置
#IP地址、用戶名、密碼、驅動注冊
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdemo?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
#HikariCP配置
hikari:
minimum-idle: 10 #池中維護的最小連接數,默認是10個
maximum-pool-size: 10 #池中最大連接數,包括空閑和使用的,默認是10個
1.2 測試是否配置成功
@SpringBootApplication
public class Demo03Application implements CommandLineRunner {
private final static Logger logger = LoggerFactory.getLogger(Demo03Application.class);
@Autowired
DataSource dataSource;
public static void main(String[] args) {
SpringApplication.run(Demo03Application.class, args);
}
@Override
public void run(String... args) throws Exception {
try(Connection conn = dataSource.getConnection()) {
logger.info("[run][獲得連接:{}]",conn);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
運行結果如下圖所示:
2.HikariCP多數據源
2.1 應用配置文件
spring:
#datasource配置內容
datasource:
##訂單數據源配置內容
orders:
url: jdbc:mysql://localhost:3306/springbootdemo?useSSL=false&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
minimum-idle: 10 #最小空閑連接數,默認是10
maximum-pool-size: 10 #維護的最大連接數,默認是10
##用戶數據源配置內容
users:
url: jdbc:mysql://localhost:3306/springbootdemo?useSSL=false&useUnicode=true&characterEncoding=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
minimum-idle: 15 #最小空閑連接數
maximum-pool-size: 15 #維護的最大連接數
2.2 創建數據源配置類
在config包下創建DataSourceConfig類
下面這段代碼主要是創建了DataSourceProperties,再通過其去創建DataSource。
DataSourceProperties.initializeDataSourceBuilder()創建一個DataSourceBuilder
DataSource.type(HikariDataSource.class)為DataSource指定類型
DataSource.build()創建type中的指定數據源對象
@Configuration
public class DataSourceConfig {
//創建數據源配置對象,並將一級屬性讀入到配置對象中
@Primary//作為主DataSourceProperties Bean
@Bean(name = "ordersDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.orders")
public DataSourceProperties ordersDataSourceProperties() {
return new DataSourceProperties();
}
//創建數據源對象,並將二級屬性讀入到數據源對象中
@Bean(name = "ordersDataSource")
@ConfigurationProperties(prefix = "spring.datasource.orders.hikari")
public DataSource ordersDataSource() {
DataSourceProperties properties = this.ordersDataSourceProperties();
return createHikariDataSource(properties);
}
@Bean(name = "usersDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.users")
public DataSourceProperties usersDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "usersDataSource")
@ConfigurationProperties(prefix = "spring.datasource.users.hikari")
public DataSource usersDataSource(DataSourceProperties properties) {
return createHikariDataSource(properties);
}
//創建HikariDataSource對象的靜態方法
private static HikariDataSource createHikariDataSource(DataSourceProperties properties) {
HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
if(StringUtils.hasText(properties.getName())) {
dataSource.setPoolName(properties.getName());
}
return dataSource;
}
}
2.3 測試是否配置成功
@Resource是代替setter方法,自動注入對象中,根據byName
@Autowires是代替setter方法,自動注入對象中,根據byType
@SpringBootApplication
public class Demo032Application implements CommandLineRunner {
private final static Logger logger = LoggerFactory.getLogger(Demo032Application.class);
@Resource(name = "ordersDataSource")
private DataSource ordersDataSource;
@Resource(name = "usersDataSource")
private DataSource usersDataSource;
public static void main(String[] args) {
SpringApplication.run(Demo032Application.class, args);
}
@Override
public void run(String... args) throws Exception {
try (Connection conn = ordersDataSource.getConnection()) {
logger.info("獲得ordersDataSource連接:{}",conn);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try (Connection conn = usersDataSource.getConnection()) {
logger.info("獲得usersDataSource連接:{}",conn);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
運行結果如下圖所示:
3. Druid單數據源
3.1 應用配置文件
在application.yml
文件中配置屬性,分別為:IP地址、用戶名、密碼、Driver、監控器
- 添加依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdemo?useSSL=false&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource #自定義數據庫連接池
druid:
min-idle: 0 #池中維護的最小空閑連接數,默認為0
max-active: 20 #池中最大連接數
filter:
stat: #配置StatFilter,統計監控信息
log-slow-sql: true #開啟慢查詢記錄
slow-sql-millis: 5000 #超過5000ms,就是慢查詢
stat-view-servlet: #配置StatViewServlet,展示監控信息
enabled: true
login-username: guowenchao
login-password: 123456
3.2 測試是否配置成功
@Slf4j
@SpringBootApplication
public class Demo033Application implements CommandLineRunner {
@Autowired
private DataSource dataSource;
public static void main(String[] args) {
SpringApplication.run(Demo033Application.class, args);
}
@Override
public void run(String... args) throws Exception {
log.info("獲取連接:{}",dataSource.getClass());
}
}
運行結果如下圖所示:
4. Druid多數據源
4.1 應用配置文件
- 添加依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
spring:
# datasource 數據源配置內容
datasource:
# 訂單數據源配置
orders:
url: jdbc:mysql://127.0.0.1:3306/springbootdemo?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
# Druid 自定義配置,對應 DruidDataSource 中的 setting 方法的屬性
min-idle: 0
max-active: 20
# 用戶數據源配置
users:
url: jdbc:mysql://127.0.0.1:3306/tspringbootdemo?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
type: com.alibaba.druid.pool.DruidDataSource
# Druid 自定義配置,對應 DruidDataSource 中的 setting 方法的屬性
min-idle: 0
max-active: 20
druid:
# 過濾器配置
filter:
stat: # 配置 StatFilter
log-slow-sql: true
slow-sql-millis: 5000
# StatViewServlet 配置
stat-view-servlet:
enabled: true
login-username: guowenchao
login-password: 123456
4.2 創建數據源配置類
在config包下創建DataSourceConfig類
因為druid的默認配置放在orders和users下面,所以可以按照下面方式創建:
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "ordersDataSource")
@ConfigurationProperties(prefix = "spring.datasource.orders")
public DataSource ordersDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "usersDataSource")
@ConfigurationProperties(prefix = "spring.datasource.users")
public DataSource usersDataSource() {
return DruidDataSourceBuilder.create().build();
}
}
4.3 測試是否配置成功
@Slf4j
@SpringBootApplication
public class Demo034Application implements CommandLineRunner {
@Resource(name = "ordersDataSource")
private DataSource ordersDataSource;
@Resource(name = "usersDataSource")
private DataSource usersDataSource;
public static void main(String[] args) {
SpringApplication.run(Demo034Application.class, args);
}
@Override
public void run(String... args) throws Exception {
log.info("獲得連接1:{}",ordersDataSource.getClass());
log.info("獲得連接2:{}",usersDataSource.getClass());
}
}
運行結果如下圖所示:
聲明:上述文章參考自:芋道SpringBoot數據庫連接池入門
環環無敵大可愛
😗