廢話少說,按SpringBoot的老套路來。css
【step1】:添加依賴java
<!-- 數據庫鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency>
【step2】:application.yml中添加對應配置mysql
spring: datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://對應本身的數據庫鏈接 username: xxx password: xxx ######################### Druid鏈接池的配置信息 ################# spring.druid.initialSize: 5 #初始化鏈接大小 spring.druid.minIdle: 5 #最小鏈接池數量 spring.druid.maxActive: 20 #最大鏈接池數量 spring.druid.maxWait: 60000 #獲取鏈接時最大等待時間,單位毫秒 spring.druid.timeBetweenEvictionRunsMillis: 60000 #配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 spring.druid.minEvictableIdleTimeMillis: 300000 #配置一個鏈接在池中最小生存的時間,單位是毫秒 spring.druid.validationQuery: SELECT 1 FROM DUAL #測試鏈接 spring.druid.testWhileIdle: true #申請鏈接的時候檢測,建議配置爲true,不影響性能,而且保證安全性 spring.druid.testOnBorrow: false #獲取鏈接時執行檢測,建議關閉,影響性能 spring.druid.testOnReturn: false #歸還鏈接時執行檢測,建議關閉,影響性能 spring.druid.poolPreparedStatements: false #是否開啓PSCache,PSCache對支持遊標的數據庫性能提高巨大,oracle建議開啓,mysql下建議關閉 spring.druid.maxPoolPreparedStatementPerConnectionSize: 20 #開啓poolPreparedStatements後生效 spring.druid.filters: stat,wall,log4j #配置擴展插件,經常使用的插件有=>stat:監控統計 log4j:日誌 wall:防護sql注入 spring.druid.connectionProperties: 'druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000' #經過connectProperties屬性來打開mergeSql功能;慢SQL記錄
【step3】:java代碼中讀取配置,並作相關轉換(好比數據庫密碼加解密等,這裏省略)spring
import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.embedded.FilterRegistrationBean; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; @Configuration public class DruidConfiguration { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driverClassName}") private String driverClassName; @Value("${spring.druid.initialSize}") private int initialSize; @Value("${spring.druid.minIdle}") private int minIdle; @Value("${spring.druid.maxActive}") private int maxActive; @Value("${spring.druid.maxWait}") private int maxWait; @Value("${spring.druid.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.druid.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @Value("${spring.druid.validationQuery}") private String validationQuery; @Value("${spring.druid.testWhileIdle}") private boolean testWhileIdle; @Value("${spring.druid.testOnBorrow}") private boolean testOnBorrow; @Value("${spring.druid.testOnReturn}") private boolean testOnReturn; @Value("${spring.druid.poolPreparedStatements}") private boolean poolPreparedStatements; @Value("${spring.druid.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.druid.filters}") private String filters; @Value("{spring.druid.connectionProperties}") private String connectionProperties; @Bean @Primary public DataSource dataSource() { 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.setMaxWait(maxWait); 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 { datasource.setFilters(filters); } catch (SQLException e) { } datasource.setConnectionProperties(connectionProperties); return datasource; } @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //設置ip白名單 servletRegistrationBean.addInitParameter("deny","192.168.0.19");//設置ip黑名單,優先級高於白名單 //設置控制檯管理用戶 servletRegistrationBean.addInitParameter("loginUsername","root"); servletRegistrationBean.addInitParameter("loginPassword","root"); //是否能夠重置數據 servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter(){ //建立過濾器 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //設置過濾器過濾路徑 filterRegistrationBean.addUrlPatterns("/*"); //忽略過濾的形式 filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
【step4】:測試sql
登陸成功後以下:數據庫