springmvc中xml配置
spring boot中配置
yml中配置
server:
port: 8080
spring:
profiles:
active: default
---
spring:
profiles: default
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
# 默认
type: com.zaxxer.hikari.HikariDataSource
hikari:
# 不配置默认与maximum-pool-size相同
minimum-idle: 5
# 连接池最大连接数,默认是10
maximum-pool-size: 20
connection-test-query: SELECT 1
---
spring:
profiles: druid
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
# 默认 DruidDataSource 配置,需要自定义配置使用注解方式配置数据源
type: com.alibaba.druid.pool.DruidDataSource
---
spring:
profiles: hikari
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
# 默认
type: com.zaxxer.hikari.HikariDataSource
hikari:
# 不配置默认与maximum-pool-size相同
minimum-idle: 5
# 连接池最大连接数,默认是10
maximum-pool-size: 20
## 空闲连接存活最大时间,默认600000(10分钟)
#idle-timeout: 180000
# 连接池名称 默认 HikariPool-${number}
#pool-name: MyHikariCP
connection-test-query: SELECT 1
---
spring:
profiles: dev
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
# 默认
type: com.zaxxer.hikari.HikariDataSource
# Hikari 连接池配置
# 最小空闲连接数量
hikari:
# 不配置默认与maximum-pool-size相同
minimum-idle: 5
# 空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 180000
# 连接池最大连接数,默认是10
maximum-pool-size: 20
# 此属性控制从池返回的连接的默认自动提交行为,默认值:true
auto-commit: true
# 连接池名称
pool-name: MyHikariCP
# 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
max-lifetime: 1800000
# 数据库连接超时时间,默认30秒,即30000
connection-timeout: 30000
connection-test-query: SELECT 1
---
spring:
profiles: prod
springboot 使用类配置
package com.lanwp.mybatis.demo.mybatisplusdemo.config;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* DruidConfig
*
* @author lanwenping
*/
//@Component
//@Configuration
//@PropertySource("classpath:config/service.properties")
//@PropertySource("classpath:application.properties")
//@ConfigurationProperties(prefix = "spring.datasource")
public class DruidDBConfig {
private String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
// @Value("${spring.datasource.maxActive}")
private int maxActive = 20;
private boolean poolPreparedStatements = true;
private int maxPoolPreparedStatementPerConnectionSize = 20;
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
}
@Bean
// @Primary
public DataSource druidDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
//configuration
dataSource.setInitialSize(initialSize);
dataSource.setMinIdle(minIdle);
dataSource.setMaxActive(maxActive);
dataSource.setValidationQuery("select 1");
// dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// dataSource.setValidationQuery(validationQuery);
// dataSource.setTestWhileIdle(testWhileIdle);
// dataSource.setTestOnBorrow(testOnBorrow);
// dataSource.setTestOnReturn(testOnReturn);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
// String filters = "stat,wall,log4j";
String filters = "stat";
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
// dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ");
return dataSource;
}
/**
* 少的配置
* @return
*/
public DataSource druidDataSource1() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
//configuration
// 默认 0
dataSource.setInitialSize(5);
// 默认0
dataSource.setMinIdle(5);
// 默认8
dataSource.setMaxActive(20);
// 默认false
dataSource.setPoolPreparedStatements(true);
// 默认 10
dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
dataSource.setValidationQuery("select 1");
try {
// String filters = "stat,wall,log4j";
String filters = "stat";
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
// dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ");
return dataSource;
}
/**
* 固定值
* @return
*/
public DataSource druidDataSource2() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
//configuration
// 默认 0
dataSource.setInitialSize(5);
// 默认0
dataSource.setMinIdle(5);
// 默认8
dataSource.setMaxActive(20);
// 默认 -1
// dataSource.setMaxWait(maxWait);
// 默认 60 * 1000L , 1分钟 可以设置为 10分钟
dataSource.setTimeBetweenEvictionRunsMillis(60_000);
// 默认 1000L * 60L * 30L , 30分钟
dataSource.setMinEvictableIdleTimeMillis(1800_000);
// 官方未写 validationQuery = select 1
// dataSource.setValidationQuery(validationQuery);
// 默认值
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setTestOnReturn(false);
// 默认false
dataSource.setPoolPreparedStatements(true);
// 默认 10
dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
try {
// String filters = "stat,wall,log4j";
String filters = "stat";
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
return dataSource;
}
}
可以使用简单配置
public class DruidDBConfig {
private String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
// @Value("${spring.datasource.maxActive}")
private int maxActive = 20;
private boolean poolPreparedStatements = true;
private int maxPoolPreparedStatementPerConnectionSize = 20;
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
}
@Bean
@Primary
public DataSource druidDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
//configuration
dataSource.setInitialSize(initialSize);
dataSource.setMinIdle(minIdle);
dataSource.setMaxActive(maxActive);
dataSource.setValidationQuery("select 1");
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
// String filters = "stat,wall,log4j";
String filters = "stat";
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
// dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ");
return dataSource;
}
}