spring Boot 2.2.4整合druid数据源

spring Boot 2.2.4整合druid数据源亲测成功,试了无数次,找了无数此错误,终于成功了,玛德。

1.创建一个spring boot项目

spring Boot 2.2.4整合druid数据源_第1张图片

spring Boot 2.2.4整合druid数据源_第2张图片

点Next,选择web下的spring web,SQL下的JDBC,mybatis,mysql

spring Boot 2.2.4整合druid数据源_第3张图片

然后点next,最后点finish,完成创建spring boot项目。

2.pom.xml文件


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0modelVersion>
 <parent>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-parentartifactId>
     <version>2.2.4.RELEASEversion>
     <relativePath/> 
 parent>
 <groupId>com.ztgroupId>
 <artifactId>demo-druidartifactId>
 <version>0.0.1-SNAPSHOTversion>
 <name>demo-druidname>
 <description>Demo project for Spring Bootdescription>

 <properties>
     <java.version>1.8java.version>
 properties>

 <dependencies>
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-jdbcartifactId>
     dependency>
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-webartifactId>
     dependency>
     <dependency>
         <groupId>org.mybatis.spring.bootgroupId>
         <artifactId>mybatis-spring-boot-starterartifactId>
         <version>2.1.1version>
     dependency>

     <dependency>
         <groupId>mysqlgroupId>
         <artifactId>mysql-connector-javaartifactId>
         <scope>runtimescope>
     dependency>

     
     <dependency>
         <groupId>com.alibabagroupId>
         <artifactId>druidartifactId>
         <version>1.1.10version>
     dependency>

     
     <dependency>
         <groupId>log4jgroupId>
         <artifactId>log4jartifactId>
         <version>1.2.17version>
     dependency>


     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-testartifactId>
         <scope>testscope>
         <exclusions>
             <exclusion>
                 <groupId>org.junit.vintagegroupId>
                 <artifactId>junit-vintage-engineartifactId>
             exclusion>
         exclusions>
     dependency>
 dependencies>

 <build>
     <plugins>
         <plugin>
             <groupId>org.springframework.bootgroupId>
             <artifactId>spring-boot-maven-pluginartifactId>
         plugin>
     plugins>
 build>

project>

3.编写application.yml文件

因为yml文件比properties文件更加直观,故编写yml文件

spring:
# 数据源配置
# MYSQL 5 驱动:com.mysql.jdbc.Driver,MYSQL 6+ 驱动:com.mysql.cj.jdbc.Driver
datasource:
 type: com.alibaba.druid.pool.DruidDataSource
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: jdbc:mysql://localhost\:3306/mybatis
 username: root
 password: 123456

 # 初始化大小,最小,最大
 initialSize: 5
 minIdle: 5
 maxActive: 20
 # 配置获取连接等待超时的时间
 maxWait: 60000
 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒
 timeBetweenEvictionRunsMillis: 60000
 # 配置一个连接在池中最小生存时间
 minEvictableIdleTimeMillis: 300000
 validationQuery: SELECT 1 FROM sys_user
 testWhileIdle: true
 testOnBorrow: false
 testOnReturn: false
 # 打开 PSCache,并且指定每个连接上 PSCache 的大小
 poolPreparedStatements: true
 maxPoolPreparedStatementPerConnectionSize: 20
 # 配置监控统计拦截的 Filter,去掉后监控界面 SQL 无法统计,wall 用于防火墙
 filters: stat,wall,log4j
 # 通过 connection-properties 属性打开 mergeSql 功能;慢 SQL 记录
 connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000

4.编写一个DruidConfig类

package com.zt.demodruid.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 {
 /**
     * 创建一个数据源,并且绑定属性
     * @return
     */
    @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", "192.168.6.11");
        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;
    }
}

5.访问localhost:8080/druid

亲测访问localhost:8080/druid成功

spring Boot 2.2.4整合druid数据源_第4张图片

用户名:admin 密码:123456 登陆

spring Boot 2.2.4整合druid数据源_第5张图片

你可能感兴趣的:(spring Boot 2.2.4整合druid数据源)