您好,登錄后才能下訂單哦!
這篇文章主要講解了SpringBoot如何整合Druid數(shù)據(jù)庫(kù)連接池,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
一,Druid是什么?
Druid是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池。Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能。
二, 在哪里下載druid
maven中央倉(cāng)庫(kù): http://central.maven.org/maven2/com/alibaba/druid/
三, 怎么獲取Druid的源碼
Druid是一個(gè)開(kāi)源項(xiàng)目,源碼托管在github上,源代碼倉(cāng)庫(kù)地址是 https://github.com/alibaba/druid。同時(shí)每次Druid發(fā)布正式版本和快照的時(shí)候,都會(huì)把源碼打包,你可以從上面的下載地址中找到相關(guān)版本的源碼
項(xiàng)目配置
pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--自啟動(dòng)Druid管理后臺(tái)--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
application.yml
server: port: 8080 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 25 filters: stat,wall,slf4j connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 useGlobalDataSourceStat: true cache: type: redis redis: host: 127.0.0.1 port: 6379 password: pool: max-active: 100 max-idle: 10 max-wait: 100000 lettuce: shutdown-timeout: 0 timeout: 5000 database: 0 thymeleaf: cache: false; mybatis: mapper-locations: classpath:zhw.example.zhw.loginModule.loginDao/*.xml
配置JdbcConfig
package zhw.example.zhw.loginModule.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; 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 javax.sql.DataSource; import java.util.Collections; import java.util.HashMap; import java.util.Map; @Configuration public class JdbcConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource dataSource(){ return new DruidDataSource(); } /** * 配置Druid監(jiān)控 * * @return StatViewServlet */ @Bean public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); Map<String, String> map = new HashMap<>(); //訪問(wèn)的用戶名密碼 map.put(StatViewServlet.PARAM_NAME_USERNAME, "root"); map.put(StatViewServlet.PARAM_NAME_PASSWORD, "root"); //允許訪問(wèn)的ip,默認(rèn)是所有ip map.put(StatViewServlet.PARAM_NAME_ALLOW, ""); //禁止訪問(wèn)的ip map.put(StatViewServlet.PARAM_NAME_DENY, "192.168.1.1"); bean.setInitParameters(map); return bean; } /** * 配置一個(gè)監(jiān)控的filter * * @return WebStatFilter */ @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(); bean.setFilter(new WebStatFilter()); Map<String, String> map = new HashMap<>(); //移除這些監(jiān)聽(tīng) map.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*,*.gif,*.jpg,*.png"); bean.setInitParameters(map); //攔截所有請(qǐng)求,全部都要走druid監(jiān)聽(tīng) bean.setUrlPatterns(Collections.singletonList("/*")); return bean; } }
測(cè)試配置url白名單
如果工程中配置了Apache Shiro,需要在配置類(lèi)中添加白名單
看完上述內(nèi)容,是不是對(duì)SpringBoot如何整合Druid數(shù)據(jù)庫(kù)連接池有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。