本文主要是介绍进阶SpringBoot之 Druid 数据源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Druid 是开源平台上一个数据库连接池实现,具备日志监控功能
Druid 可以很好的监控 DB 连接池和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池
Maven 仓库
pom.xml 文件导入 Durid 的 jar 包
<!-- Druid依赖 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.23</version></dependency>
application.yml 文件:
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
指定 DruidDataSource 连接池
spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource
测试类:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;@SpringBootTest
class JdbcApplicationTests {@AutowiredDataSource dataSource;@Testvoid contextLoads() throws SQLException {//查看默认数据源System.out.println(dataSource.getClass());//获得数据库连接Connection connection = dataSource.getConnection();System.out.println(connection);//关闭connection.close();}
}
运行,查看结果:数据源是 Druid
application.yml 文件:
druid 数据源配置
stat:监控统计、log4j:日志记录、wall:防御 sql 注入
spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource#SpringBoot默认不注入这些属性,需要自己绑定#druid数据源专有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true#filters配置监控统计拦截#stat:监控统计 log4j:日志记录 wall:防御sql注入filters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
pom.xml 导入 Log4j 依赖:
<!-- Log4j依赖 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
DruidConfig 配置类:
@ConfigurationProperties(prefix = "spring.datasource") 绑定 yaml
后台监控 ServletRegistrationBean
SpringBoot 内置了 Servlet 容器,所以没有 web.xml,用 ServletRegistrationBean 代替
package com.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.jakarta.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.HashMap;@Configuration
public class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource") //绑定@Beanpublic DataSource druidDataSource(){return new DruidDataSource();}//后台监控ServletRegistrationBean//SpringBoot内置了Servlet容器,所以没有web.xml,用ServletRegistrationBean代替@Beanpublic ServletRegistrationBean statViewServlet(){ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");HashMap<String,String> initParameters = new HashMap<>();//增加配置initParameters.put("loginUsername","admin");initParameters.put("loginPassword","123456");bean.setInitParameters(initParameters); //设置初始化参数return bean;}//filter过滤器@Beanpublic FilterRegistrationBean webStatFilter(){FilterRegistrationBean bean = new FilterRegistrationBean();bean.setFilter(new WebStatFilter());HashMap<String,String> initParameters = new HashMap<>();//exclusions排除,不进行统计initParameters.put("exclusions","*.js,*.css,/druid/*");bean.setInitParameters(initParameters);return bean;}
}
这篇关于进阶SpringBoot之 Druid 数据源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!