一、Maven项目依赖
<!-- 开发者工具(热部署 修改classpath下的文件springboot自动重启) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Web场景启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <!-- SpringBoot测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
二、配置文件
由于使用了SpringBoot,所以大部分不需要我们手动配置,只需要进行简单的指定数据源等必要的配置:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.url=jdbc:mysql://172.xx.xx.26:3306/risk?useUnicode=true&characterEncoding=utf-8 spring.datasource.druid.username=root spring.datasource.druid.password=123456 # 初始化时建立的连接数 spring.datasource.druid.initial-size=5 # 最大连接数 spring.datasource.druid.max-active=20 # 最小连接数 spring.datasource.druid.min-idle=5 # 获取连接最大等待时间,单位:毫秒 spring.datasource.druid.max-wait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.druid.time-between-eviction-runs-millis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.druid.min-evictable-idle-time-millis=300000 # 是否缓存preparedStatement spring.datasource.druid.pool-prepared-statements=true # 最大preparedStatement缓存数,当pool-prepared-statements=true时需要大于0 spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 # 检测连接是否失效的sql spring.datasource.druid.validation-query=SELECT 'x' # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.druid.filters=wall,stat # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=3000 # WebStatFilter配置,说明请参考Druid Wiki,配置_配置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/* spring.datasource.druid.web-stat-filter.session-stat-enable=false spring.datasource.druid.web-stat-filter.session-stat-max-count=1000 # StatViewServlet配置:展示Druid的统计信息,StatViewServlet的用途包括:1.提供监控信息展示的html页面2.提供监控信息的JSON API 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=true spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=admin
三、测试
在配置中,我们把监控平台的路径设置为/druid/*,用户和密码为:admin/admin