pom.xml
- <dependency>
- <groupId>com.microsoft.sqlserver</groupId>
- <artifactId>mssql-jdbc</artifactId><!--sqlserver 依赖 驱动 jar-->
- <scope>runtime</scope>
- <version>6.4.0.jre8</version>
- </dependency>
- <dependency>
- <groupId>com.microsoft.sqlserver</groupId>
- <artifactId>sqljdbc4</artifactId>
- <version>4.0</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId><!--mybatis 依赖 -->
- <version>2.1.0</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.17</version>
- </dependency>
- application.properties
- # 数据库连接
- spring.datasource.druid.url=jdbc:sqlserver://localhost:1433;DatabaseName=localdemo
- spring.datasource.druid.username=sa
- spring.datasource.druid.password=123456
- spring.datasource.druid.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
- #*************************************** druid 连接池配置 *********************************************
- spring.datasource.druid.initial-size=5
- spring.datasource.druid.min-idle=5
- spring.datasource.druid.maxActive=50
- # 配置获取连接等待超时的时间 (ms)
- spring.datasource.druid.maxWait=60000
- # 打开 PSCache, 并且指定每个连接上 PSCache 的大小
- spring.datasource.druid.pool-prepared-statements=true
- spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
- spring.datasource.druid.validation-query=SELECT 1
- spring.datasource.druid.validation-query-timeout=60000
- spring.datasource.druid.test-on-borrow=false
- spring.datasource.druid.test-on-return=false
- spring.datasource.druid.test-while-idle=true
- # 配置间隔多久才进行一次检测, 检测需要关闭的空闲连接, 单位是毫秒
- spring.datasource.druid.time-between-eviction-runs-millis=60000
- # 配置一个连接在池中最小生存的时间, 单位是毫秒
- spring.datasource.druid.min-evictable-idle-time-millis=100000
- ##******************* 监控配置 begin***************************###
- # 配置监控统计拦截的 filters, 去掉后监控界面 sql 无法统计,'stat'用于 sql 监控,'wall'用于防火墙,'log4j2'监控我本地的日志对于数据库的连接. 其'# 状态监控'配置参照 stat 或 wall 的, 可以不设, 实际中我就没设
- spring.datasource.druid.filters=stat,wall,log4j2
- # 状态监控 (SQL 监控 配置 StatFilter)
- spring.datasource.druid.filter.stat.db-type=sqlserver
- spring.datasource.druid.filter.stat.enabled=true
- spring.datasource.druid.filter.stat.log-slow-sql=true
- spring.datasource.druid.filter.stat.slow-sql-millis=5000
- # 状态监控 (防火墙 配置 WallFilter)
- spring.datasource.druid.filter.wall.db-type=sqlserver
- spring.datasource.druid.filter.wall.enabled=true
- spring.datasource.druid.filter.wall.config.delete-allow=false
- spring.datasource.druid.filter.wall.config.drop-table-allow=false
- # 监控过滤器 (配置_配置 webStatFilter)
- spring.datasource.druid.Web-stat-filter.enabled=true
- spring.datasource.druid.Web-stat-filter.url-pattern=/*
- spring.datasource.druid.Web-stat-filter.exclusions=*.JS,*.gif,*.jpg,*.PNG,*.CSS,*.ico,/druid/*
- #druid 监控页面
- spring.datasource.druid.stat-view-servlet.enabled= true
- spring.datasource.druid.stat-view-servlet.url-pattern= /druid/*
- spring.datasource.druid.stat-view-servlet.reset-enable= false
- spring.datasource.druid.stat-view-servlet.login-username= nsoft
- spring.datasource.druid.stat-view-servlet.login-password= nsoft1122
- #spring.datasource.druid.stat-view-servlet.allow= 127.0.0.1
- ##******************* 监控配置 end*****************************###
然后登陆 (我的本地端口 8082, 上下文根 test):
http://localhost:8082/test/druid (账户密码是自己设置的, nsoft nsoft1122)
注: 网上很多关于 springboot+druid, 还需要配置一些 java 类. 这都是旧版本的 druid. 新版本不需要配置 java 类. 且, 在我安照网上查找的配置 application.properties 后, 监控页面等打开, 但 SQL 监控项无数据, 原因就是用新版本的 druid, 却用旧的方式配置导致的.
来源: http://www.bubuko.com/infodetail-3189419.html