先在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。