springboot 2.0以上默认使用 com.zaxxer.hikari.HikariDataSource 数据源,但是我们可以通过 spring.datasource.type 指定数据源。
一、Maven 引入和Druid配置
首先添加maven配置druid依赖:
复制代码
1
2
3
4
5
6
7<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.5</version> </dependency>
然后在Application.properties 添加如下配置:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#数据库连接配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/yao?autoReconnect=true&failOverReadOnly=false;useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #Spring Boot 2.0 以上默认使用 com.zaxxer.hikari.HikariDataSource 数据源,但可以 通过 spring.datasource.type 指定数据源。 #Spring Boot 默认是不注入这些属性值的,需要自己绑定 需要自己創建Config文件綁定屬性 #druid 数据源专有配置 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j spring.datasource.filters=stat,wall spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.useGlobalDataSourceStat=true spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
使用spring.datasource.type指定数据源为Druid:
复制代码
1
2spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
其他配置为druid私有配置,可以直接拿来用。
二、创建Druid配置类
创建DruidConfig 配置文件:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63package com.cpown.demo.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.omg.CORBA.PUBLIC_MEMBER; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; import java.util.HashMap; /** * druid私有化屬性配置 */ @Configuration public class DruidConfig { /** * 加載私有化配置,并重新返回数据源 * ConfigurationProperties注解可以加载到配置信息 * @return */ @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource DruidDataSource(){ return new DruidDataSource(); }; /** * 后台监控 * //配置 Druid 监控管理后台的Servlet; * //内置 Servlet 容器时没有web.xml文件,所以使用 Spring Boot 的注册 Servlet 方式 * @return */ @Bean public ServletRegistrationBean ServletRegistrationBean(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); HashMap<String,String> initHashMap = new HashMap<>(); //设置账号密码 固定,不可变的 initHashMap.put("loginUsername","admin"); initHashMap.put("loginPassword","123456"); //允许谁访问 “” 代表所有人 initHashMap.put("allow",""); bean.setInitParameters(initHashMap); return bean; } /** * 配置监控过滤器 * @return */ @Bean public FilterRegistrationBean get(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); HashMap<String,String> initHashMap = new HashMap<>(); //配置哪些请求不需要监控 initHashMap.put("exclusions","*.css,/druid/*"); bean.setInitParameters(initHashMap); return bean; } }
- ServletRegistrationBean 可以配置driuid监控, 这里设置的loginUsername和loginPassword为监控页面的登录名和密码。
- FilterRegistrationBean 可以设置哪些请求需要被监控,如果配置了不需要被监控,则不会看到调用情况。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35/** * 后台监控 * //配置 Druid 监控管理后台的Servlet; * //内置 Servlet 容器时没有web.xml文件,所以使用 Spring Boot 的注册 Servlet 方式 * @return */ @Bean public ServletRegistrationBean ServletRegistrationBean(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); HashMap<String,String> initHashMap = new HashMap<>(); //设置账号密码 固定,不可变的 initHashMap.put("loginUsername","admin"); initHashMap.put("loginPassword","123456"); //允许谁访问 “” 代表所有人 initHashMap.put("allow",""); bean.setInitParameters(initHashMap); return bean; } /** * 配置监控过滤器 * @return */ @Bean public FilterRegistrationBean get(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); HashMap<String,String> initHashMap = new HashMap<>(); //配置哪些请求不需要监控 initHashMap.put("exclusions","*.css,/druid/*"); bean.setInitParameters(initHashMap); return bean; }
三、查看结果
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
422020-07-28 16:16:05.214 INFO 15544 --- [nio-8080-exec-4] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited 2020-07-28 16:37:56.263 DEBUG 15544 --- [nio-8080-exec-2] c.c.d.m.S.selectSysUserByName : ==> Parameters: admin(String) 2020-07-28 16:37:56.265 DEBUG 15544 --- [nio-8080-exec-2] c.c.d.m.S.selectSysUserByName : <== Total: 1 2020-07-28 16:38:00.090 DEBUG 15544 --- [nio-8080-exec-7] c.c.d.mapper.DepartmentMapper.selectAll : ==> Preparing: select id, department_name from department 2020-07-28 16:38:00.091 DEBUG 15544 --- [nio-8080-exec-7] c.c.d.mapper.DepartmentMapper.selectAll : ==> Parameters: 2020-07-28 16:38:00.094 DEBUG 15544 --- [nio-8080-exec-7] c.c.d.mapper.DepartmentMapper.selectAll : <== Total: 4 2020-07-28 16:38:00.094 INFO 15544 --- [nio-8080-exec-7] c.c.d.controller.DepartmentController : 查询部门列表:数量=4 2020-07-28 16:38:00.876 DEBUG 15544 --- [nio-8080-exec-8] c.c.demo.mapper.SysUserMapper.selectAll : ==> Preparing: select sysuser.id, sysuser.name,sysuser.password, sysuser.email, sysuser.sex, sysuser.department_id,dep.department_name from sys_user sysuser left join department dep on dep.id = sysuser.department_id 2020-07-28 16:38:00.878 DEBUG 15544 --- [nio-8080-exec-8] c.c.demo.mapper.SysUserMapper.selectAll : ==> Parameters: 2020-07-28 16:38:00.882 DEBUG 15544 --- [nio-8080-exec-8] c.c.demo.mapper.SysUserMapper.selectAll : <== Total: 3 2020-07-28 16:38:00.883 INFO 15544 --- [nio-8080-exec-8] c.c.demo.controller.SysUserController : 查询用户列表:数量=3 2020-07-28 16:38:51.030 DEBUG 15544 --- [io-8080-exec-10] c.c.demo.mapper.SysUserMapper.selectAll : ==> Preparing: select sysuser.id, sysuser.name,sysuser.password, sysuser.email, sysuser.sex, sysuser.department_id,dep.department_name from sys_user sysuser left join department dep on dep.id = sysuser.department_id 2020-07-28 16:38:51.030 DEBUG 15544 --- [io-8080-exec-10] c.c.demo.mapper.SysUserMapper.selectAll : ==> Parameters: 2020-07-28 16:38:51.033 DEBUG 15544 --- [io-8080-exec-10] c.c.demo.mapper.SysUserMapper.selectAll : <== Total: 3 2020-07-28 16:38:51.034 INFO 15544 --- [io-8080-exec-10] c.c.demo.controller.SysUserController : 查询用户列表:数量=3
可以看到启动时初始化了Druid数据源。我们使用admin登录,并查看了用户数量。
在浏览器输入http://localhost:8080/druid/login.html可以进入sql监控页面,使用我们配置的用户名密码登录:
可以看到我们的sql,执行了一次登录,以及两次查询。
最后
以上就是唠叨洋葱最近收集整理的关于springboot + mysql + druid 整合的全部内容,更多相关springboot内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复