新建一个Spring Boot项目
操作路径如下,Spring Initializr这里,默认的https://start.spring.io需要科学上网,建议改成自定义的,阿里提供的一个,秒出:https://start.aliyun.com
IDEA–>File–>New–>Project–>Spring Initializr–>Next–>输入自定义Group和Artifact–>Next–>默认,啥都不要勾选–>Finish
修改pom文件
打开pom文件,找到下面这个配置,覆盖掉
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
覆盖成
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
PS:其实在spring体系里,包含starter字样的依赖,默认其实已经包含了很多需要用到的jar包,所以不需要重复引入,比如druid,只需要引入druid-spring-boot-starter就行,建议在pom文件里点入,可以直观的看到
修改配置文件 application.properties
打开application.properties,覆盖成下面这样,这些配置可以看druid官方地址的一个测试案例,地址是:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter/src/test/resources
# 应用服务web访问端口
server.port=8884
######Druid数据源配置,可以不添加druid配置类引入,如果添加druid配置类,注意配置前缀是spring.datasource.druid**
# 参考官方:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter ######
# 官方wiki:https://github.com/alibaba/druid/wiki #
# JDBC配置
spring.datasource.druid.url=jdbc:mysql://localhost:3306/erp?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8
spring.datasource.druid.username=root
spring.datasource.druid.password=password
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
# 连接池配置,下面配置说明请参考Druid Github Wiki,配置_DruidDataSource参考配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=30
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=5000
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=5
spring.datasource.druid.validation-query=select 1
spring.datasource.druid.validation-query-timeout=1
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=true
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=10000
spring.datasource.druid.min-evictable-idle-time-millis=30000
#spring.datasource.druid.max-evictable-idle-time-millis=
spring.datasource.druid.async-close-connection-enable=true
spring.datasource.druid.filters=stat,wall
# 更多配置属性见 DruidDataSource 内成员变量(只要有set方法便支持),或者根据IDE提示,或者查看官方文档
######Druid内置监控页面配置######
#下面配置说明请参考Druid Github 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,*.css,/druid/*
#spring.datasource.druid.web-stat-filter.session-stat-enable=
#spring.datasource.druid.web-stat-filter.session-stat-max-count=
#spring.datasource.druid.web-stat-filter.principal-session-name=
#spring.datasource.druid.web-stat-filter.principal-cookie-name=
#spring.datasource.druid.web-stat-filter.profile-enable=
#下面配置说明请参考Druid Github Wiki,配置_StatViewServlet配置
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=root
spring.datasource.druid.stat-view-servlet.login-password=password
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
spring.datasource.druid.stat-view-servlet.deny=192.168.1.110
#下面配置说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
#spring.datasource.druid.aop-patterns= #Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
#如果spring.datasource.druid.aop-patterns要代理的类没有定义interface请设置spring.aop.proxy-target-class=true
# 自定义StatFilter 配置
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.db-type=mysql
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# 配置WallFilter
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=mysql
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=false
运行,看运行日志
可以看到已经引入了
打开druid内置后台监控页面
地址:http://localhost:8884/druid/login.html
账号:root
密码:password
切换到数据源
可以看到 filter类名这一项能正常显示,拉下去看看配置,也是配置文件里配置的数字。
Over,enjoy!