mirror of https://github.com/qiurunze123/miaosha
220 lines
5.9 KiB
Java
220 lines
5.9 KiB
Java
package com.geekq.miaosha.config;
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
import com.alibaba.druid.support.http.StatViewServlet;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.sql.SQLException;
|
|
|
|
@Configuration
|
|
@ConfigurationProperties(prefix = "spring.datasource")
|
|
public class DruidConfig {
|
|
|
|
private String url;
|
|
private String username;
|
|
private String password;
|
|
private String driverClassName;
|
|
private String type;
|
|
private String filters;
|
|
private int maxActive;
|
|
private int initialSize;
|
|
private int minIdle;
|
|
private long maxWait;
|
|
private long timeBetweenEvictionRunsMillis;
|
|
private long minEvictableIdleTimeMillis;
|
|
private String validationQuery;
|
|
private boolean testWhileIdle;
|
|
private boolean testOnBorrow;
|
|
private boolean testOnReturn;
|
|
private boolean poolPreparedStatements;
|
|
private int maxOpenPreparedStatements;
|
|
|
|
@Bean
|
|
public ServletRegistrationBean druidSverlet() {
|
|
ServletRegistrationBean reg = new ServletRegistrationBean();
|
|
reg.setServlet(new StatViewServlet());
|
|
reg.addUrlMappings("/druid/*");
|
|
reg.addInitParameter("loginUsername", "joshua");
|
|
reg.addInitParameter("loginPassword", "123456");
|
|
reg.addInitParameter("logSlowSql", "true");
|
|
reg.addInitParameter("slowSqlMillis", "1000");
|
|
return reg;
|
|
}
|
|
|
|
@Bean
|
|
public DataSource druidDataSource() {
|
|
DruidDataSource datasource = new DruidDataSource();
|
|
datasource.setUrl(url);
|
|
datasource.setUsername(username);
|
|
datasource.setPassword(password);
|
|
datasource.setDriverClassName(driverClassName);
|
|
datasource.setInitialSize(initialSize);
|
|
datasource.setMinIdle(minIdle);
|
|
datasource.setMaxActive(maxActive);
|
|
datasource.setMaxWait(maxWait);
|
|
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
|
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
|
datasource.setValidationQuery(validationQuery);
|
|
datasource.setTestWhileIdle(testWhileIdle);
|
|
datasource.setTestOnBorrow(testOnBorrow);
|
|
datasource.setTestOnReturn(testOnReturn);
|
|
datasource.setPoolPreparedStatements(poolPreparedStatements);
|
|
datasource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
|
|
try {
|
|
datasource.setFilters(filters);
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return datasource;
|
|
}
|
|
|
|
public String getUrl() {
|
|
return url;
|
|
}
|
|
|
|
public void setUrl(String url) {
|
|
this.url = url;
|
|
}
|
|
|
|
public String getUsername() {
|
|
return username;
|
|
}
|
|
|
|
public void setUsername(String username) {
|
|
this.username = username;
|
|
}
|
|
|
|
public String getPassword() {
|
|
return password;
|
|
}
|
|
|
|
public void setPassword(String password) {
|
|
this.password = password;
|
|
}
|
|
|
|
public String getDriverClassName() {
|
|
return driverClassName;
|
|
}
|
|
|
|
public void setDriverClassName(String driverClassName) {
|
|
this.driverClassName = driverClassName;
|
|
}
|
|
|
|
public String getType() {
|
|
return type;
|
|
}
|
|
|
|
public void setType(String type) {
|
|
this.type = type;
|
|
}
|
|
|
|
public String getFilters() {
|
|
return filters;
|
|
}
|
|
|
|
public void setFilters(String filters) {
|
|
this.filters = filters;
|
|
}
|
|
|
|
public int getMaxActive() {
|
|
return maxActive;
|
|
}
|
|
|
|
public void setMaxActive(int maxActive) {
|
|
this.maxActive = maxActive;
|
|
}
|
|
|
|
public int getInitialSize() {
|
|
return initialSize;
|
|
}
|
|
|
|
public void setInitialSize(int initialSize) {
|
|
this.initialSize = initialSize;
|
|
}
|
|
|
|
public int getMinIdle() {
|
|
return minIdle;
|
|
}
|
|
|
|
public void setMinIdle(int minIdle) {
|
|
this.minIdle = minIdle;
|
|
}
|
|
|
|
public long getMaxWait() {
|
|
return maxWait;
|
|
}
|
|
|
|
public void setMaxWait(long maxWait) {
|
|
this.maxWait = maxWait;
|
|
}
|
|
|
|
public long getTimeBetweenEvictionRunsMillis() {
|
|
return timeBetweenEvictionRunsMillis;
|
|
}
|
|
|
|
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
|
|
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
|
|
}
|
|
|
|
public long getMinEvictableIdleTimeMillis() {
|
|
return minEvictableIdleTimeMillis;
|
|
}
|
|
|
|
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
|
|
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
|
|
}
|
|
|
|
public String getValidationQuery() {
|
|
return validationQuery;
|
|
}
|
|
|
|
public void setValidationQuery(String validationQuery) {
|
|
this.validationQuery = validationQuery;
|
|
}
|
|
|
|
public boolean isTestWhileIdle() {
|
|
return testWhileIdle;
|
|
}
|
|
|
|
public void setTestWhileIdle(boolean testWhileIdle) {
|
|
this.testWhileIdle = testWhileIdle;
|
|
}
|
|
|
|
public boolean isTestOnBorrow() {
|
|
return testOnBorrow;
|
|
}
|
|
|
|
public void setTestOnBorrow(boolean testOnBorrow) {
|
|
this.testOnBorrow = testOnBorrow;
|
|
}
|
|
|
|
public boolean isTestOnReturn() {
|
|
return testOnReturn;
|
|
}
|
|
|
|
public void setTestOnReturn(boolean testOnReturn) {
|
|
this.testOnReturn = testOnReturn;
|
|
}
|
|
|
|
public boolean isPoolPreparedStatements() {
|
|
return poolPreparedStatements;
|
|
}
|
|
|
|
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
|
|
this.poolPreparedStatements = poolPreparedStatements;
|
|
}
|
|
|
|
public int getMaxOpenPreparedStatements() {
|
|
return maxOpenPreparedStatements;
|
|
}
|
|
|
|
public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
|
|
this.maxOpenPreparedStatements = maxOpenPreparedStatements;
|
|
}
|
|
|
|
}
|