真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot使用Druid詳解

Druid是Java語言中最好的數(shù)據(jù)庫連接池,并且能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能,下面來說明如何在 SpringBoot 中配置使用Druid。
步驟:

公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出江永免費(fèi)做網(wǎng)站回饋大家。

1. 在pom.xml中加載依賴

2. 在application.properties中加入數(shù)據(jù)源配置

3. 編寫DruidConfiguration,通過@Bean注解的方式注入druid servlet和filter,以便提供監(jiān)控頁面訪問

4. 瀏覽器訪問

一、在pom.xml中加入依賴

 
      com.alibaba 
      druid 
      1.0.18 
   

二、在application.properties中加入數(shù)據(jù)源配置

################## server ################################ 
server.port=8080 
 
################## datasource ################################ 
spring.datasource.url = jdbc:MySQL://localhost:3306/spring 
spring.datasource.username = root 
spring.datasource.password = 123456 
spring.datasource.driverClassName = com.mysql.jdbc.Driver 
 
#使用Druid數(shù)據(jù)源 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 
# 下面為連接池的補(bǔ)充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中 
 
# 初始化大小,最小,最大 
spring.datasource.initialSize=5 
spring.datasource.minIdle=5 
spring.datasource.maxActive=20 
 
# 配置獲取連接等待超時(shí)的時(shí)間 
spring.datasource.maxWait=60000 
 
# 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 
spring.datasource.timeBetweenEvictionRunsMillis=60000 
 
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 
spring.datasource.minEvictableIdleTimeMillis=300000 
spring.datasource.validationQuery=SELECT 1 FROMDUAL 
spring.datasource.testWhileIdle=true 
spring.datasource.testOnBorrow=false 
spring.datasource.testOnReturn=false 
 
# 打開PSCache,并且指定每個(gè)連接上PSCache的大小 
spring.datasource.poolPreparedStatements=true 
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 
 
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻 
spring.datasource.filters=stat,wall,log4j 
 
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 
 
# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù) 
#spring.datasource.useGlobalDataSourceStat=true 
 
 
################ Java Persistence Api ########################### 
# Specify the DBMS 
spring.jpa.database = MYSQL 
# Show or not log for each sql query 
spring.jpa.show-sql = true 
# Hibernate ddl auto (create, create-drop, update) 
spring.jpa.hibernate.ddl-auto = update 
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy] 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 
# stripped before adding them to the entity manager) 
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect 
 
#暫時(shí)不用redis,所以none,spring.session.store-type=reids 
spring.session.store-type=none 

以上是我項(xiàng)目中的配置信息,大家只要關(guān)注datasource部分即可。

三、編寫DruidConfiguration配置類,通過@Bean注解的方式注入druid servlet和filter,以便提供監(jiān)控頁面訪問

package com.example.config; 
 
import java.sql.SQLException; 
 
import javax.sql.DataSource; 
 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Value; 
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 com.alibaba.druid.pool.DruidDataSource; 
import com.alibaba.druid.support.http.StatViewServlet; 
import com.alibaba.druid.support.http.WebStatFilter; 
 
/** 
 * 配置druid數(shù)據(jù)源 
 * 
 */ 
@Configuration 
public class DruidConfiguration { 
   
  private Logger logger = LoggerFactory.getLogger(DruidConfiguration.class); 
 
  @Bean 
  public ServletRegistrationBean druidStatViewServlet() { 
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); 
    registrationBean.addInitParameter("allow", "127.0.0.1"); 
    registrationBean.addInitParameter("deny", "192.168.31.234"); 
    registrationBean.addInitParameter("loginUsername", "admin"); 
    registrationBean.addInitParameter("loginPassword", "123456"); 
    registrationBean.addInitParameter("resetEnable", "false"); 
 
    return registrationBean; 
  } 
 
  @Bean 
  public FilterRegistrationBean druidWebStatViewFilter() { 
    FilterRegistrationBean registrationBean = new FilterRegistrationBean(new WebStatFilter()); 
    registrationBean.addInitParameter("urlPatterns", "/*"); 
    registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); 
 
    return registrationBean; 
 
  } 
 
  @Bean 
  public DataSource druidDataSource(@Value("${spring.datasource.url}") String url, 
      @Value("${spring.datasource.driverClassName}") String driver, 
      @Value("${spring.datasource.username}") String userName, 
      @Value("${spring.datasource.password}") String password, 
      @Value("${spring.datasource.maxActive}") int maxActive, 
      @Value("${spring.datasource.filters}") String filters, 
      @Value("${spring.datasource.initialSize}") 
      int initialSize, 
      @Value("${spring.datasource.minIdle}") 
      int minIdle, 
      @Value("${spring.datasource.maxWait}") 
      int maxWait, 
      @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") 
      int timeBetweenEvictionRunsMillis, 
      @Value("${spring.datasource.minEvictableIdleTimeMillis}") 
      int minEvictableIdleTimeMillis, 
      @Value("${spring.datasource.validationQuery}") 
      String validationQuery, 
      @Value("${spring.datasource.testWhileIdle}") 
      boolean testWhileIdle, 
      @Value("${spring.datasource.testOnBorrow}") 
      boolean testOnBorrow, 
      @Value("${spring.datasource.testOnReturn}") 
      boolean testOnReturn, 
      @Value("${spring.datasource.poolPreparedStatements}") 
      boolean poolPreparedStatements, 
      @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") 
      int maxPoolPreparedStatementPerConnectionSize, 
      @Value("${spring.datasource.connectionProperties}") 
      String connectionProperties, 
      @Value("${spring.datasource.useGlobalDataSourceStat}") 
      boolean useGlobalDataSourceStat 
 
  ) { 
    DruidDataSource dataSource = new DruidDataSource(); 
    /*數(shù)據(jù)源主要配置*/ 
    dataSource.setUrl(url); 
    dataSource.setDriverClassName(driver); 
    dataSource.setUsername(userName); 
    dataSource.setPassword(password); 
     
    /*數(shù)據(jù)源補(bǔ)充配置*/ 
    dataSource.setMaxActive(maxActive); 
    dataSource.setInitialSize(initialSize); 
    dataSource.setMinIdle(minIdle); 
    dataSource.setMaxWait(maxWait); 
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); 
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); 
    dataSource.setValidationQuery(validationQuery); 
    dataSource.setTestOnBorrow(testOnBorrow); 
    dataSource.setTestOnReturn(testOnReturn); 
    dataSource.setTestWhileIdle(testWhileIdle); 
    dataSource.setPoolPreparedStatements(poolPreparedStatements); 
    dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); 
    dataSource.setConnectionProperties(connectionProperties); 
    dataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat); 
     
    try { 
      dataSource.setFilters(filters); 
      logger.info("Druid數(shù)據(jù)源初始化設(shè)置成功......"); 
    } catch (SQLException e) { 
      e.printStackTrace(); 
      logger.info("Druid數(shù)據(jù)源filters設(shè)置失敗......"); 
    } 
    return dataSource; 
 
  } 
} 

四、瀏覽器訪問

在瀏覽器中輸入http://localhost:8080/druid/index.html,首次訪問需要使用賬號(hào)和密碼登錄

Spring Boot 使用Druid詳解

Spring Boot 使用Druid詳解

在數(shù)據(jù)源一欄中,所有加"*"標(biāo)識(shí)的都不能為空,,尤其是filter類名,否則會(huì)看不到SQL監(jiān)控?cái)?shù)據(jù)。

五、查看SQL監(jiān)控

執(zhí)行刪除某條記錄后,查看SQL監(jiān)控

Spring Boot 使用Druid詳解

到這里,我們就把druid配置成功了。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


網(wǎng)站題目:SpringBoot使用Druid詳解
網(wǎng)頁路徑:http://weahome.cn/article/jhehje.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部