一文秒懂springboot druid 配置
Druid是阿里巴巴開發(fā)的一個(gè)連接池,他提供了一個(gè)高效、功能強(qiáng)大、可擴(kuò)展性好的數(shù)據(jù)庫連接池,區(qū)別于hikari。如果選擇高性能可以選hikari,如果要功能多就選,druid。
首先pom引入依賴
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version></dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version></dependency>
然后yml配置參數(shù)
server: port: 8888spring: 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: 10 minIdle: 1 maxActive: 10 maxWait: 10000 timeBetweenEvictionRunsMillis: 6000 minEvictableIdleTimeMillis: 300000 testWhileIdle: true testOnBorrow: true testOnReturn: true poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 validationQuery: select 1# stat 監(jiān)控統(tǒng)計(jì),wall 防止sql注入,log4j (yml 要配置,不然會(huì)報(bào)錯(cuò)) 日志統(tǒng)計(jì) filters: stat,wall,log4j
然后在項(xiàng)目config下配置參數(shù)
import java.util.HashMap;@Configurationpublic class DruidConfig implements WebMvcConfigurer { @Bean @ConfigurationProperties(prefix = 'spring.datasource') public DataSource druidDataSource(){return new DruidDataSource(); } //后臺(tái)監(jiān)控 @Bean public ServletRegistrationBean statViewServlet(){ServletRegistrationBean<StatViewServlet> statViewServlet = new ServletRegistrationBean<>(new StatViewServlet(), '/druid/*');//配置后臺(tái)登錄用戶名密碼HashMap<String, String> objectObjectHashMap = new HashMap<>();//用戶名參數(shù)密碼不能改變,系統(tǒng)配置objectObjectHashMap.put('loginUsername','admin');objectObjectHashMap.put('loginPassword','admin');//允許誰可以訪問 為空時(shí)所有人可以訪問 例如:objectObjectHashMap.put('allow','localhost'); 代表只能自己訪問objectObjectHashMap.put('allow','');//禁止誰訪問 objectObjectHashMap.put('name','192.168.0.1');statViewServlet.setInitParameters(objectObjectHashMap);return statViewServlet; }}
然后就可以在后臺(tái)輸入 項(xiàng)目地址/druid進(jìn)行登錄訪問
到此這篇關(guān)于一文秒懂springboot druid 配置的文章就介紹到這了,更多相關(guān)springboot druid 配置內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))2. .Net反向代理組件Yarp用法詳解3. 解決request.getParameter取值后的if判斷為NULL的問題4. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別5. 詳解JSP 內(nèi)置對象request常見用法6. JSP中param動(dòng)作的實(shí)例詳解7. ASP.NET MVC實(shí)現(xiàn)下拉框多選8. ASP.NET MVC增加一條記錄同時(shí)添加N條集合屬性所對應(yīng)的個(gè)體9. .NET中的MassTransit分布式應(yīng)用框架詳解10. ASP.NET MVC實(shí)現(xiàn)本地化和全球化
