springboot整合druid数据源

本文介绍了如何在SpringBoot项目中整合Druid数据源,从新建项目、导入依赖开始,详细讲解了配置DruidConfig类和数据库配置的步骤。启动项目后,通过浏览器可以直观看到Druid的强大功能,暗示其还有更多潜在功能待发掘。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先新建springboot web项目 导入依赖

        <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>

        <!--web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

2、配置类 DruidConfig.java 以下为固定写法 账号、密码、访问权限可以变换

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")//绑定properties / yaml中的配置
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控 web.xml
    //访问 /druid/ 就会进入后台监控页面
    @Bean
    public ServletRegistrationBean StatViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后台账号密码登录
        HashMap<String, String> initParameters = new HashMap<>();
        initParameters.put("loginUsername","root");//账户
        initParameters.put("loginPassword","root");//密码
        //允许谁可以访问
        initParameters.put("allow","");//参数为空 所有都可以
        //禁止谁可以访问
        //initParameters.put("thunder","");
        bean.setInitParameters(initParameters);//初始化参数
        return bean;
    }

    //过滤器
    public FilterRegistrationBean webStartFilter(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        //过滤的请求
        Map<String,String> initParameters=new HashMap<>();
        initParameters.put("exclusions","*.jsp,*.css./druid/*");
        bean.setInitParameters(initParameters);
        return bean;

    }
}

3、数据库、数据源配置 可以写在一起

#数据库
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#使用阿里巴巴的数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring:
#druid数据源
  datasource:
    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    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,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

4、启动项目 打开浏览器 输入http://127.0.0.1:8081/druid/

在这里插入图片描述

5、可见它的功能很强、还有其他的功能等待我们去探究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值