您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Spring Boot 中怎么使用DruidDataSource自定義數據源,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
一、添加依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version> </dependency>
二、配置application.yml
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/mxntest?characterEncoding=UTF-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 初始化大小,最小,最大 initialSize: 5 minIdle: 5 maxActive: 50 # 配置獲取連接等待超時的時間 maxWait: 60000
三、Druid數據源配置
package com.example.demo.config; /** * @author 12084 * @create 2018-08-09 11:27 */ import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.beans.factory.annotation.Value; 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 org.springframework.context.annotation.Primary; import javax.sql.DataSource; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; /** * Druid數據源配置 */ @Configuration public class DataSourceConfig { private static String dbUrl; private static String username; private static String password; private static String driverClassName; private static int initialSize; private static int minIdle; private static int maxActive; private static int maxWait; /** * 注冊DruidServlet * * @return */ @Bean public ServletRegistrationBean druidServletRegistrationBean() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new StatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*"); //登錄查看信息的賬號密碼. servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "123456"); return servletRegistrationBean; } /** * 注冊DruidFilter攔截 * * @return */ @Bean public FilterRegistrationBean druidFilterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<String, String>(); //設置忽略請求 initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); filterRegistrationBean.setInitParameters(initParams); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } /** * 配置DataSource * @return * @throws SQLException */ @Bean(initMethod = "init",destroyMethod = "close") @Primary public DataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setUrl(dbUrl); druidDataSource.setFilters("stat,wall"); druidDataSource.setInitialSize(initialSize); druidDataSource.setMinIdle(minIdle); druidDataSource.setMaxActive(maxActive); druidDataSource.setMaxWait(maxWait); druidDataSource.setUseGlobalDataSourceStat(true); druidDataSource.setDriverClassName(driverClassName); return druidDataSource; } @Value("${spring.datasource.url}") public void setDbUrl(String dbUrl) { DataSourceConfig.dbUrl = dbUrl; } @Value("${spring.datasource.username}") public void setUsername(String username) { DataSourceConfig.username = username; } @Value("${spring.datasource.password}") public void setPassword(String password) { DataSourceConfig.password = password; } @Value("${spring.datasource.driver-class-name}") public void setDriverClassName(String driverClassName) { DataSourceConfig.driverClassName = driverClassName; } @Value(value = "${spring.datasource.initialSize}") public void setInitialSize(int initialSize) { DataSourceConfig.initialSize = initialSize; } @Value(value = "${spring.datasource.minIdle}") public void setMinIdle(int minIdle) { DataSourceConfig.minIdle = minIdle; } @Value(value = "${spring.datasource.maxActive}") public void setMaxActive(int maxActive) { DataSourceConfig.maxActive = maxActive; } @Value(value = "${spring.datasource.maxWait}") public void setMaxWait(int maxWait) { DataSourceConfig.maxWait = maxWait; } }
四、http://localhost:8080/druid/index.html 就可以查看,如果配置密碼則輸入密碼
//登錄查看信息的賬號密碼. servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "123456");
關于Spring Boot 中怎么使用DruidDataSource自定義數據源就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。