springboot2.1集成druid连接池

准备:

  1. idea
  2. java1.8
  3. druid 1.1.9
  4. springboot2.1.6

1.创建一个springboot项目:

  • springboot2.1集成druid连接池_第1张图片
  • springboot2.1集成druid连接池_第2张图片
  • springboot2.1集成druid连接池_第3张图片
  • springboot2.1集成druid连接池_第4张图片

 

2.修改pom.xml,引入jar包:


    mysql
    mysql-connector-java
    runtime



    org.springframework.boot
    spring-boot-starter-jdbc



    com.alibaba
    druid
    1.1.9



    org.aspectj
    aspectjweaver
    1.8.13

3.在\src\main\resources目录下新建application.yml文件

server:
  port: 8888

spring:
  druid:
    druidClassName: com.mysql.cj.jdbc.Driver
    url: 你的mysql地址
    username: 用户名
    password: 密码
    #初始化连接池大小
    initialSize: 50
    #最大连接数
    maxActive: 300
    #最小连接数
    minIdle: 5
    #获取连接的最大等待时间
    maxWait: 10000
    #解决mysql8小时的问题
    validationgQuery: SELECT 'X'
    #空闲连接的检查时间间隔
    timeBetweenEvictionRunsMillis: 60000
    #空闲连接最小空闲时间
    minEvictableIdleTimeMillis: 300000
    #不影响性能,并且保证安全性
    testWhileIdle: true
    #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,slf4j

4.创建配置类:

springboot2.1集成druid连接池_第5张图片

  • 创建DruidConfiguration类
package com.druid.demo.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;


@Configuration
public class DruidConfiguration {

    //这个注解读取配置文件前缀为prefix的配置,将外部的配置文件与这里绑定
    @ConfigurationProperties(prefix = "spring.druid")

    //容器的开启与关闭
    @Bean(initMethod = "init",destroyMethod = "close")
    public DruidDataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        return  dataSource;
    }

    /**
     * 注册一个StatViewServlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //添加初始化参数:initParams
        // 白名单:
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
        //登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * 注册一个:filterRegistrationBean
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

  • 创建SpringMethondAspect类(如不使用spring监控功能可忽略)
package com.druid.demo.config;

import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 *  开启druid的spring监控
 */
@Configuration
public class SpringMethodAspect {
    @Bean
    public DruidStatInterceptor druidStatInterceptor() {
        DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
        return dsInterceptor;
    }

    @Bean
    @Scope("prototype")
    public JdkRegexpMethodPointcut druidStatPointcut() {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPatterns("com.druid.demo.test.*");
        return pointcut;
    }

    @Bean
    public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
        DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        defaultPointAdvisor.setPointcut(druidStatPointcut);
        defaultPointAdvisor.setAdvice(druidStatInterceptor);
        return defaultPointAdvisor;
    }
}

5.测试

    创建Test类,为方便测试此处使用jdbcTemplate

package com.druid.demo.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@Slf4j
@RestController
public class Test {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("hello")
    public String hello() {
        //自己创建mysql数据库及表
        List> maps = jdbcTemplate.queryForList("SELECT * FROM u_user ");
        System.out.println(maps.toString());
        return "成功";
    }
}

启动程序访问:http://localhost:8888/hello

springboot2.1集成druid连接池_第6张图片

访问druid页面:http://localhost:8888/druid/index.html

springboot2.1集成druid连接池_第7张图片

springboot2.1集成druid连接池_第8张图片

你可能感兴趣的:(java)