springboot 怎么整合Druid ,已经连接了mysql

要在Spring Boot项目中整合Druid连接池,您需要执行以下步骤:

  1. 在pom.xml文件中添加Druid的依赖:
    xml


        com.alibaba
        druid-spring-boot-starter
        1.1.10

    在application.properties或application.yml文件中配置Druid连接池和MySQL数据库连接信息。以下是一个application.properties配置示例:
    arduino

    # 数据源基本配置
    spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
    spring.datasource.username=your_username
    spring.datasource.password=your_password
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    # 使用Druid数据源
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

    # Druid连接池配置
    spring.datasource.druid.initial-size=5
    spring.datasource.druid.min-idle=5
    spring.datasource.druid.max-active=20
    spring.datasource.druid.max-wait=60000
    spring.datasource.druid.time-between-eviction-runs-millis=60000
    spring.datasource.druid.min-evictable-idle-time-millis=300000
    spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
    spring.datasource.druid.test-while-idle=true
    spring.datasource.druid.test-on-borrow=false
    spring.datasource.druid.test-on-return=false
    spring.datasource.druid.pool-prepared-statements=true
    spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
    spring.datasource.druid.filters=stat,wall
    spring.datasource.druid.use-global-data-source-stat=true
    (可选)配置Druid监控。要启用Druid的监控功能,您需要在项目中创建一个配置类,如下所示:
    java

    package com.example.demo.config;

    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    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 java.util.HashMap;
    import java.util.Map;

    @Configuration
    public class DruidConfig {

        @Bean
        public ServletRegistrationBean druidStatViewServlet() {
            ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
            Map initParams = new HashMap<>();
            initParams.put("loginUsername", "admin");
            initParams.put("loginPassword", "123456");
            initParams.put("resetEnable", "false");
            initParams.put("allow", ""); // 默认允许所有访问
            // initParams.put("deny", "192.168.1.100"); // 指定IP黑名单,与allow同时存在时,deny优先于allow
            bean.setInitParameters(initParams);
            return bean;
        }

        @Bean
        public FilterRegistrationBean druidWebStatFilter() {
            FilterRegistrationBean bean = new FilterRegistrationBean<>(new WebStatFilter());
            bean.addUrlPatterns("/*");
            bean.addInitParameter("exclusions", "*.js,*.css,/druid/*,*.gif,*.jpg,*.png,*.ico,*.woff,*.woff2");
            return bean;
        }
    }
    这将启用Druid的监控功能后,您可以通过浏览器访问http://localhost:8080/druid(假设您的应用运行在端口8080上)来查看Druid监控页面。在此页面上,可以查看数据源和SQL统计信息,以帮助了解和优化数据库性能。

    通过上述步骤,已成功将Druid连接池整合到Spring Boot项目中,并启用了Druid的监控功能。

你可能感兴趣的:(mysql,spring,boot,java,Druid)