您好,登錄后才能下訂單哦!
本篇文章為大家展示了springboot中怎么引入druid數(shù)據(jù)源,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.5</version> </dependency>
#數(shù)據(jù)源配置 spring.datasource.url=jdbc:mysql://192.168.0.131:3306/hongone?useAffectedRows=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #druid連接池 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 連接池的配置信息 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置獲取連接等待超時的時間 spring.datasource.maxWait=30000 # 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打開PSCache,并且指定每個連接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻 spring.datasource.filters=stat,wall # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=2000
注意 : 這里的配置根據(jù)項目需要去進行自定義,比如 超時連接時間 、慢 sql 查詢時間 等
package com.hone.system.utils.druid; import com.alibaba.druid.pool.DruidDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; import java.sql.SQLException; @Configuration public class DruidDBConfig{ private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class); @Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.initialSize}") private int initialSize; @Value("${spring.datasource.minIdle}") private int minIdle; @Value("${spring.datasource.maxActive}") private int maxActive; @Value("${spring.datasource.maxWait}") private int maxWait; @Value("${spring.datasource.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @Value("${spring.datasource.validationQuery}") private String validationQuery; @Value("${spring.datasource.testWhileIdle}") private boolean testWhileIdle; @Value("${spring.datasource.testOnBorrow}") private boolean testOnBorrow; @Value("${spring.datasource.testOnReturn}") private boolean testOnReturn; @Value("${spring.datasource.poolPreparedStatements}") private boolean poolPreparedStatements; @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.filters}") private String filters; @Value("{spring.datasource.connectionProperties}") private String connectionProperties; /** * @Bean 聲明,DataSource 對象為 Spring 容器所管理; * @Primary 表示這里定義的DataSource將覆蓋其他來源的DataSource。 * StatFilter,用于統(tǒng)計監(jiān)控信息。StatFilter的別名是stat。 * 統(tǒng)計SQL信息,合并統(tǒng)計。mergeStat是的MergeStatFilter縮寫。 * 通過 DataSource 的屬性<property name="filters" value="mergeStat" /> 或者 * connectProperties屬性來打開mergeSql功能 <property name="connectionProperties" value="druid.stat.mergeSql=true" /> * StatFilter屬性slowSqlMillis用來配置SQL慢的標(biāo)準(zhǔn) * * @return */ @Bean @Primary public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setDriverClassName(driverClassName); datasource.setUsername(username); datasource.setPassword(password); //configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { /** * 設(shè)置StatFilter,用于統(tǒng)計監(jiān)控信息。 * StatFilter的別名是stat * */ datasource.setFilters(filters); } catch (SQLException e) { logger.error("druid configuration initialization filter : {0}",e); } datasource.setConnectionProperties(connectionProperties); return datasource; } }
import com.alibaba.druid.support.http.StatViewServlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; /** * StatViewServlet用于展示Druid的統(tǒng)計信息。 * 提供監(jiān)控信息展示的html頁面 * 提供監(jiān)控信息的JSON API * * 內(nèi)置監(jiān)控頁面的首頁是/druid/index.html * * loginUsername、loginPassword 登錄用戶名和密碼 */ /** * @Webservlet * 有兩個屬性可以用來表示Servlet的訪問路徑,分別是value和urlPatterns。value和urlPatterns都是數(shù)組形式, * 表示我們可以把一個Servlet映射到多個訪問路徑,但是value和urlPatterns不能同時使用。 * */ @WebServlet( urlPatterns = {"/druid/*"}, initParams = { @WebInitParam(name = "loginUsername", value = "admin"), @WebInitParam(name = "loginPassword", value = "admin"), @WebInitParam(name = "resetEnable", value = "false") } ) public class DruidStatViewServlet extends StatViewServlet { }
import com.alibaba.druid.support.http.WebStatFilter; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; /** * WebStatFilter用于采集web-jdbc關(guān)聯(lián)監(jiān)控的數(shù)據(jù)。 * 屬性filterName聲明過濾器的名稱,可選 * 屬性urlPatterns指定要過濾 的URL模式,也可使用屬性value來聲明.(指定要過濾的URL模式是必選屬性) */ @WebFilter( urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions",value = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*") } ) public class DruidStatFilter extends WebStatFilter { }
@SpringBootApplication @MapperScan("com.hone.dao") //開啟mapper掃描 @ServletComponentScan //開啟servlet掃描 public class HongoneApplication {.....}
最后,啟動項目進行測試 在瀏覽器中輸入 http://localhost:8080/druid/index.html
(注意: 如果你的項目設(shè)置了訪問前綴 , 訪問地址問 http://localhost:8080/訪問前綴/druid/index.html )
上述內(nèi)容就是springboot中怎么引入druid數(shù)據(jù)源,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。