Rely on

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.1.21</version>
</dependency>
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Copy the code

Configuration properties

spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/mybatis? useUnicdoe=true&characterEncoding=utf-8&serverTimezone=UTC driver-class-name: #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 # configuration monitoring statistics of intercepting filters, stat: monitoring statistics, log4j: logging, wall: defense against SQL injection # if an error is allowed, Java. Lang. ClassNotFoundException: Org, apache Log4j. Properity # import Log4j rely on line filters: stat, wall, Log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionoProperties: druid.stat.mergeSql=true; druid.stat.slowSqlMillis=500Copy the code

use

@Configuration

public class DruidConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(a){
        return new DruidDataSource();
    }
    @Bean
    // Without web.xml, SpringBoot has a built-in servlet container: ServletRegistrationBean
    public ServletRegistrationBean servletRegistrationBean(a){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        HashMap<String ,String> map = new HashMap<>();
        map.put("loginUsername"."admin");
        map.put("loginPassword"."123456");
        // Allow access, if empty, all access
        map.put("allow"."");
        // Disable access
        map.put("name"."192.11.11.11");
        bean.setInitParameters(map);
        return bean;
    }
    //filter
    public FilterRegistrationBean filterRegistrationBean(a){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        HashMap<String ,String> map = new HashMap<>();
        // The following statistics will not be counted
        map.put("exclusions"."*.js,*.css,/druid/**");
        filterRegistrationBean.setInitParameters(map);
        returnfilterRegistrationBean; }}Copy the code