今天就跟大家聊聊有關SpringBoot中怎么整合Druid數據源,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
創(chuàng)新互聯建站是一家專注網站建設、網絡營銷策劃、成都微信小程序、電子商務建設、網絡推廣、移動互聯開發(fā)、研究、服務為一體的技術型公司。公司成立十多年以來,已經為上千多家成都門簾各業(yè)的企業(yè)公司提供互聯網服務?,F在,服務的上千多家客戶與我們一路同行,見證我們的成長;未來,我們一起分享成功的喜悅。
1.數據庫結構
2.項目結構
3.pom.xml文件
org.springframework.boot spring-boot-starter-jdbc MySQL mysql-connector-java runtime com.alibaba druid 1.1.8 log4j log4j 1.2.17 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
4.application.yml配置文件
spring: datasource: username: root password: wangqing url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.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 # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # 合并多個DruidDataSource的監(jiān)控數據 #useGlobalDataSourceStat: true mybatis: # 指定全局配置文件位置 #config-location: classpath:mybatis/mybatis-config.xml # 指定sql映射文件位置 mapper-locations: classpath:mapper/*.xml #如src/main/resources下的mappers文件下的TUserMapper.xml # schema: # - classpath:sql/department.sql #根據department.sql 的sql語句創(chuàng)建表 # - classpath:sql/employee.sql
5.創(chuàng)建一個DruidConfig的配置類,實例化Druid Datasource
package com.qingfeng.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.Arrays; import java.util.HashMap; import java.util.Map; @Configuration public class DruidConfig { //指定加載appliction.yml文件里面的spring.datasource開頭的 // DruidDataSource類里面的屬性與appliction.yml文件里面的spring.datasource開頭的對應映射 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } //配置Druid的監(jiān)控 //1、配置一個管理后臺的Servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); MapinitParams = new HashMap<>(); initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow","");//默認就是允許所有訪問 initParams.put("deny",""); bean.setInitParameters(initParams); return bean; } //2、配置一個web監(jiān)控的filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map initParams = new HashMap<>(); initParams.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
6.創(chuàng)建一個UserController類測試
package com.qingfeng.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; import java.util.Map; @Controller public class UserController { @Autowired JdbcTemplate jdbcTemplate; @ResponseBody @GetMapping("/query") public Mapmap(){ List
7.運行項目,通過瀏覽器訪問 http://localhost:8080/query
8.我們DruidConfig類里配置的下面代碼可以幫我們實現監(jiān)控
//配置Druid的監(jiān)控 //1、配置一個管理后臺的Servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); MapinitParams = new HashMap<>(); initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow","");//默認就是允許所有訪問 initParams.put("deny",""); bean.setInitParameters(initParams); return bean; }
9.我們啟動項目,打開網址:http://localhost:8080/druid/login.html 可以通過登錄,查看druid數據源狀態(tài)監(jiān)控
我們上面設置的是用戶名:admin 密碼:123456
看完上述內容,你們對SpringBoot中怎么整合Druid數據源有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯行業(yè)資訊頻道,感謝大家的支持。