一文秒懂springboot druid 配置
Druid是阿里巴巴開發的一個連接池,他提供了一個高效、功能強大、可擴展性好的數據庫連接池,區別于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配置參數
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 監控統計,wall 防止sql注入,log4j (yml 要配置,不然會報錯) 日志統計 filters: stat,wall,log4j
然后在項目config下配置參數
import java.util.HashMap;@Configurationpublic class DruidConfig implements WebMvcConfigurer { @Bean @ConfigurationProperties(prefix = 'spring.datasource') public DataSource druidDataSource(){return new DruidDataSource(); } //后臺監控 @Bean public ServletRegistrationBean statViewServlet(){ServletRegistrationBean<StatViewServlet> statViewServlet = new ServletRegistrationBean<>(new StatViewServlet(), '/druid/*');//配置后臺登錄用戶名密碼HashMap<String, String> objectObjectHashMap = new HashMap<>();//用戶名參數密碼不能改變,系統配置objectObjectHashMap.put('loginUsername','admin');objectObjectHashMap.put('loginPassword','admin');//允許誰可以訪問 為空時所有人可以訪問 例如:objectObjectHashMap.put('allow','localhost'); 代表只能自己訪問objectObjectHashMap.put('allow','');//禁止誰訪問 objectObjectHashMap.put('name','192.168.0.1');statViewServlet.setInitParameters(objectObjectHashMap);return statViewServlet; }}
然后就可以在后臺輸入 項目地址/druid進行登錄訪問
到此這篇關于一文秒懂springboot druid 配置的文章就介紹到這了,更多相關springboot druid 配置內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. php網絡安全中命令執行漏洞的產生及本質探究2. 三個不常見的 HTML5 實用新特性簡介3. Angular獲取ngIf渲染的Dom元素示例4. php面向對象程序設計介紹5. ASP調用WebService轉化成JSON數據,附json.min.asp6. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁7. 使用.net core 自帶DI框架實現延遲加載功能8. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析9. php測試程序運行速度和頁面執行速度的代碼10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析
