使用springboot怎么對(duì)druid連接池進(jìn)行整合-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)使用springboot怎么對(duì)druid連接池進(jìn)行整合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

堅(jiān)守“ 做人真誠(chéng) · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都食品包裝袋小微創(chuàng)業(yè)公司專業(yè)提供企業(yè)網(wǎng)站建設(shè)營(yíng)銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁(yè)布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。

使用springboot默認(rèn)的連接池

導(dǎo)入springboot data-jdbc依賴

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>

配置文件配置連接池

spring:
 datasource:
  username: root
  password: 5201314
  url: jdbc:mysql:///jqmb?serverTimezone=UTC
  driver-class-name: com.mysql.cj.jdbc.Driver

springboot默認(rèn)的連接池

@Autowired
   DataSource dataSource;
  @Test
  void contextLoads() {
    System.out.println(dataSource.getClass());
    System.out.println("____________________________________");
  }

使用springboot怎么對(duì)druid連接池進(jìn)行整合

使用連接池druid


導(dǎo)入druid依賴

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.18</version>
    </dependency>

配置文件配置druid的屬性

spring:
 datasource:
  username: root
  password: 5201314
  url: jdbc:mysql:///jqmb?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
  filters: stat,wall,log4j
  maxPoolPreparedStatementPerConnectionSize: 20
  useGlobalDataSourceStat: true
  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

使用springboot怎么對(duì)druid連接池進(jìn)行整合

配置類中對(duì)druid屬性進(jìn)行綁定

@Configuration
public class DataSource_Druid_Configure {

  @ConfigurationProperties(prefix = "spring.datasource")
  @Bean
  public DruidDataSource getDataSour(){
    return new DruidDataSource();
  }

使用springboot怎么對(duì)druid連接池進(jìn)行整合

配置Druid的監(jiān)控后臺(tái)

//配置Druid的監(jiān)控
  //1、配置一個(gè)管理后臺(tái)的Servlet
  @Bean
  public ServletRegistrationBean statViewServlet(){
    ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    Map<String,String> initParams = new HashMap<>();

    initParams.put("loginUsername","admin");//登錄用戶名
    initParams.put("loginPassword","123456");//登錄密碼
    initParams.put("allow","");//默認(rèn)就是允許所有訪問(wèn)
    initParams.put("deny","192.168.15.21");//拒絕訪問(wèn)

    bean.setInitParameters(initParams);
    return bean;
  }


  //2、配置一個(gè)web監(jiān)控的filter
  @Bean
  public FilterRegistrationBean webStatFilter(){
    FilterRegistrationBean bean = new FilterRegistrationBean();
    bean.setFilter(new WebStatFilter());
    Map<String,String> initParams = new HashMap<>();
    initParams.put("exclusions","*.js,*.css,/druid/*");
    bean.setInitParameters(initParams);
    bean.setUrlPatterns(Arrays.asList("/*"));

    return bean;
  }

訪問(wèn)http://localhost:8090/druid/login.html

使用springboot怎么對(duì)druid連接池進(jìn)行整合

如果sql監(jiān)控失效需要導(dǎo)入log4j 依賴

  <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

看完上述內(nèi)容,你們對(duì)使用springboot怎么對(duì)druid連接池進(jìn)行整合有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前標(biāo)題:使用springboot怎么對(duì)druid連接池進(jìn)行整合-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)于:http://muchs.cn/article16/ejegg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作企業(yè)建站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)