Xml代碼css
<dependency>java
<groupId>mysql</groupId>mysql
<artifactId>mysql-connector-java</artifactId>web
<scope>runtime</scope>spring
</dependency>sql
<dependency>數據庫
<groupId>com.alibaba</groupId>瀏覽器
<artifactId>druid</artifactId>springboot
<version>1.1.9</version>架構
</dependency>
Java代碼
spring.datasource.url=jdbc:mysql://192.168.1.166:3306/study
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.schema=
#數據源其餘配置,須要增長Druid的配置類(否則不生效)
#配置見:com.lqy.springboot.datasource.druid.DruidConfig
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
#配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
spring.datasource.filters=stat,wall,log4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.useGlobalDataSourceStat=true
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
爲何要增長呢?
由於SpringBoot自己提供的配置只是部分的數據庫鏈接配置,不少Druid須要的屬性配置是沒有的,如:
Java代碼
1. spring.datasource.initialSize
這個屬性,SpringBoot是沒有的,只有Druid纔有,因此須要增長相應的配置類,自動注入。
DruidConfig 類:
Java代碼
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* 導入druid數據源,擴展相關的配置屬性
*
*/
@Configuration
public class DruidConfig {
/**
* 配置Druid的屬性,和DataSource進行綁定,前綴設置爲:spring.datasource
* 不配置的話,不少初始化的屬性是沒有綁定的
* @return
*/
@ConfigurationProperties(prefix="spring.datasource")
public DataSource druid() {
return new DruidDataSource();
}
/**
* 配置druid監控
* 配置一個管理後臺的servlet
* 訪問地址:http://localhost:8080/druid/
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParameters = new HashMap<String, String>();
initParameters.put("loginUsername", "admin");//屬性見:com.alibaba.druid.support.http.ResourceServlet
initParameters.put("loginPassword", "123456");
initParameters.put("allow", "");//默認容許全部
initParameters.put("deny", "");
bean.setInitParameters(initParameters);
return bean;
}
/**
* 配置一個web監控的filter
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean filterBean = new FilterRegistrationBean();
filterBean.setFilter(new WebStatFilter());
filterBean.setUrlPatterns(Arrays.asList("/*"));
Map<String, String> initParameters = new HashMap<String, String>();
initParameters.put("exclusions", "*.js,*.css,/druid/*");//屬性見:com.alibaba.druid.support.http.WebStatFilter
filterBean.setInitParameters(initParameters);
return filterBean;
}
}
Java代碼
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DataSourceController {
@Autowired
private DataSource dataSource;
@RequestMapping("/dataSource")
public String dataSource() {
try {
System.out.println("dataSource = "+dataSource);
Connection conn = dataSource.getConnection();
System.out.println("conn = "+conn);
return "success";
} catch (SQLException e) {
e.printStackTrace();
}
return "end.";
}
}
瀏覽器打開:http://localhost:8080/druid
登陸的帳號密碼在DruidConfig類配置有