springboot配置连接池
druid 包括很多维度的统计和分析功能.就选择druid作为连接池
(1)添加依赖

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

(2) application.yml配置数据源信息

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/sprboot?useSSL=false
    username: root1
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialization-mode: always
    type: com.alibaba.druid.pool.DruidDataSource
    # 数据源其他配置
    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,去掉后监控界面sql无法统计,'wall'用于防火墙
    #filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

(3)配置监控统计功能

配置有好多方式,可注解,也可以通过web配置,按照规则配置就好。小白使用的是注解,下面就来看一**解怎么配置吧。    

配置servlet
import com.alibaba.druid.support.http.StatViewServlet;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = "/druid/*", initParams = {
        @WebInitParam(name = "allow", value=""),
        @WebInitParam(name = "deny", value= ""),
        @WebInitParam(name = "loginUsername" , value = "admin"),
        @WebInitParam(name="loginPassword",value="admin"),
        @WebInitParam(name = "resetEnable", value = "false")
})
public class DruidStatViewServlet extends StatViewServlet{

}

配置filter

import com.alibaba.druid.support.http.WebStatFilter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
        initParams = {
        @WebInitParam(name = "exclusions" , value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
        }
  )
public class DruidStatFilter extends WebStatFilter {
}

在启动方法上添加注解扫描filter和servlet

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ServletComponentScan
@SpringBootApplication
public class DemoSplitApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoSplitApplication.class, args);
    }

}

springboot系列(三) - springboot之注解配置druid SQL监控

5)启动application

启动程序,访问http://localhost:8080/druid/index.html即可查看数据源及SQL统计等。
使用配置的用户名和密码登录

相关文章: