您好,登錄后才能下訂單哦!
先在gradle里配置相應的包
compile group: 'com.alibaba', name: 'druid', version: '1.0.27'
在web.xml中配置
<!-- 連接池 啟用 Web 監控統計功能 start--> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> <init-param> <param-name>sessionStatMaxCount</param-name> <param-value>1000</param-value> </init-param> <init-param> <param-name>profileEnable</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!-- 連接池 啟用 Web 監控統計功能 end-->
spring上下文配置增加,
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/config/dbconfig.properties</value> </list> </property> </bean>
datasource配置 注釋掉的是原來的配置信息
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!-- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" --> <!-- destroy-method="close"> --> <!-- <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> --> <!-- <property name="url" --> <!-- value="jdbc:mysql://localhost:3306/myweibo?serverTimezone=UTC" /> --> <!-- <property name="username" value="root" /> --> <!-- <property name="password" value="123456" /> --> <!-- <property name="initialSize" value="5" /> --> <!-- </bean> --> <!-- 阿里 druid 數據庫連接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據庫基本信息配置 --> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="driverClassName" value="${driverClassName}" /> <property name="filters" value="${filters}" /> <!-- 最大并發連接數 --> <property name="maxActive" value="${maxActive}" /> <!-- 初始化連接數量 --> <property name="initialSize" value="${initialSize}" /> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="${maxWait}" /> <!-- 最小空閑連接數 --> <property name="minIdle" value="${minIdle}" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" /> <!-- 打開 removeAbandoned 功能 --> <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 1800 秒,也就是 30 分鐘 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 關閉 abanded 連接時輸出錯誤日志 --> <property name="logAbandoned" value="${logAbandoned}" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publickey};druid.stat.slowSqlMillis=5000" /> </bean> </beans>
dbconfig.properties, 這里使用了密碼加密.加密方法也很簡單
https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter
url:jdbc:mysql://localhost:3306/myweibo?serverTimezone=UTC driverClassName:com.mysql.cj.jdbc.Driver username:root password:UwJJ7yszUBhKKPdIv6ihaO+USCeNhJ+u2l4lagPLYWvAWkQN9dvAt4hxo1j6MI5h7qb6n0eaDxOLZI/kGm9kYw== filters:config,stat,wall maxActive:20 initialSize:1 maxWait:60000 minIdle:10 maxIdle:15 timeBetweenEvictionRunsMillis:60000 minEvictableIdleTimeMillis:300000 validationQuery:SELECT'x' testWhileIdle:true testOnBorrow:false testOnReturn:false maxOpenPreparedStatements:20 removeAbandoned:true removeAbandonedTimeout:1800 logAbandoned:true publickey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALFxQkYrqVkDjqqedvqBe0RSOkewypkss5JX3kdoodmy38llEQaDhJfLwCfQEjAZtQy7xmJ6okmyf9MmepZ77rcCAwEAAQ==
代碼方面不用變
最終效果
drui配置相關的問題https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。