SpringBoot整合Druid数据源过程详解
作者:Amy清风 时间:2023-06-03 19:47:14
这篇文章主要介绍了SpringBoot整合Druid数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.数据库结构
2.项目结构
3.pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--引入druid数据源 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<!-- 如果 不加入这依赖 配置监控统计拦截的filters时 这个会报错 filters: stat,wall,log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4.application.yml配置文件
spring:
datasource:
username: root
password: wangqing
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 合并多个DruidDataSource的监控数据
#useGlobalDataSourceStat: true
mybatis:
# 指定全局配置文件位置
#config-location: classpath:mybatis/mybatis-config.xml
# 指定sql映射文件位置
mapper-locations: classpath:mapper/*.xml #如src/main/resources下的mappers文件下的TUserMapper.xml
# schema:
# - classpath:sql/department.sql #根据department.sql 的sql语句创建表
# - classpath:sql/employee.sql
5.创建一个DruidConfig的配置类,实例化Druid Datasource
package com.qingfeng.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
//指定加载appliction.yml文件里面的spring.datasource开头的
// DruidDataSource类里面的属性与appliction.yml文件里面的spring.datasource开头的对应映射
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默认就是允许所有访问
initParams.put("deny","");
bean.setInitParameters(initParams);
return bean;
}
//2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
6.创建一个UserController类测试
package com.qingfeng.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class UserController {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/query")
public Map<String,Object> map(){
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM user");
return list.get(0);
}
}
7.运行项目,通过浏览器访问 http://localhost:8080/query
8.我们DruidConfig类里配置的下面代码可以帮我们实现监控
//配置Druid的监控
//1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//默认就是允许所有访问
initParams.put("deny","");
bean.setInitParameters(initParams);
return bean;
}
9.我们启动项目,打开网址:http://localhost:8080/druid/login.html 可以通过登录,查看druid数据源状态监控
我们上面设置的是用户名:admin 密码:123456
来源:https://www.cnblogs.com/Amywangqing/p/11964707.html
标签:spring,boot,整合,druid,数据源
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用Java+Selenium+OpenCV模拟实现网页滑动验证
2021-07-23 02:45:18
![](https://img.aspxhome.com/file/2023/4/91474_0s.gif)
基于C#实现端口扫描器(单线程和多线程)
2021-10-16 20:32:23
![](https://img.aspxhome.com/file/2023/4/94544_0s.jpg)
C#设计模式之外观模式介绍
2023-03-15 06:22:31
![](https://img.aspxhome.com/file/2023/5/68645_0s.jpg)
Android开发高仿课程表的布局实例详解
2022-02-01 18:33:19
![](https://img.aspxhome.com/file/2023/1/138861_0s.gif)
C++实现LeetCode(205.同构字符串)
2023-06-21 04:06:54
Android Apt之Activity Route的示例
2022-03-31 17:05:18
![](https://img.aspxhome.com/file/2023/6/138246_0s.jpg)
SpringBoot之@Value获取application.properties配置无效的解决
2023-09-15 22:37:55
c#生成高清缩略图的二个示例分享
2023-04-09 23:21:46
C#数据结构之单链表(LinkList)实例详解
2021-09-15 21:40:14
![](https://img.aspxhome.com/file/2023/3/100083_0s.png)
Android ViewPager实现轮播图效果
2023-03-09 20:44:53
![](https://img.aspxhome.com/file/2023/0/137210_0s.jpg)
java用户名密码验证示例代码分享
2021-10-14 08:16:14
C#自定义控件实现TextBox禁止粘贴的方法
2023-08-03 06:00:22
Android多媒体之画画板开发案例分享
2022-11-17 11:05:08
![](https://img.aspxhome.com/file/2023/8/126738_0s.gif)
C# 填充Excel图表、图例背景色的实例代码
2023-08-06 04:01:31
![](https://img.aspxhome.com/file/2023/1/79041_0s.png)
java中List删除时需要的注意事项
2023-11-11 01:28:00
Android编程中的消息机制实例详解
2022-11-14 06:50:05
![](https://img.aspxhome.com/file/2023/0/85970_0s.png)
SpringBoot内置tomcat调优测试优化
2023-04-09 03:01:59
![](https://img.aspxhome.com/file/2023/5/120525_0s.png)
java实现超市商品库存管理平台
2022-05-25 10:09:20
Android 支付宝支付、微信支付、银联支付 整合第三方支付接入方法(后台订单支付API设计)
2023-08-28 01:36:01
![](https://img.aspxhome.com/file/2023/0/139530_0s.png)
关于Unsupported major.minor version 49.0的错误解决办法
2023-06-04 22:57:44
![](https://img.aspxhome.com/file/2023/7/96077_0s.jpg)