好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

一文秒懂springboot druid 配置

Druid是阿里巴巴开发的一个连接池,他提供了一个高效、功能强大、可扩展性好的数据库连接池,区别于hikari。如果选择高性能可以选hikari,如果要功能多就选,druid。

首先pom引入依赖

?

1

2

3

4

5

6

7

8

9

10

<dependency>

           <groupId>log4j</groupId>

           <artifactId>log4j</artifactId>

           <version> 1.2 . 17 </version>

       </dependency>

      <dependency>

           <groupId>com.alibaba</groupId>

           <artifactId>druid</artifactId>

           <version> 1.2 . 6 </version>

       </dependency>

然后yml配置参数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

server:

   port: 8888

spring:

   datasource:

     username: root

     password: root

     url: jdbc:mysql: //localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC

     driver- class -name: com.mysql.cj.jdbc.Driver

     type: com.alibaba.druid.pool.DruidDataSource

     initialSize: 10

     minIdle: 1

     maxActive: 10

     maxWait: 10000

     timeBetweenEvictionRunsMillis: 6000

     minEvictableIdleTimeMillis: 300000

     testWhileIdle: true

     testOnBorrow: true

     testOnReturn: true

 

     poolPreparedStatements: true

     maxPoolPreparedStatementPerConnectionSize: 20

     validationQuery: select 1

#    stat 监控统计,wall 防止sql注入,log4j   (yml 要配置,不然会报错)  日志统计

     filters: stat,wall,log4j

然后在项目config下配置参数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

import java.util.HashMap;

 

@Configuration

public class DruidConfig implements WebMvcConfigurer {

 

     @Bean

     @ConfigurationProperties (prefix = "spring.datasource" )

     public DataSource druidDataSource(){

         return new DruidDataSource();

     }

 

     //后台监控

     @Bean

     public ServletRegistrationBean statViewServlet(){

         ServletRegistrationBean<StatViewServlet> statViewServlet = new ServletRegistrationBean<>( new StatViewServlet(), "/druid/*" );

         //配置后台登录用户名密码

         HashMap<String, String> objectObjectHashMap = new HashMap<>();

         //用户名参数密码不能改变,系统配置

         objectObjectHashMap.put( "loginUsername" , "admin" );

         objectObjectHashMap.put( "loginPassword" , "admin" );

         //允许谁可以访问  为空时所有人可以访问 例如:objectObjectHashMap.put("allow","localhost"); 代表只能自己访问

         objectObjectHashMap.put( "allow" , "" );

         //禁止谁访问 objectObjectHashMap.put("name","192.168.0.1");

 

         statViewServlet.setInitParameters(objectObjectHashMap);

         return statViewServlet;

     }

 

}

然后就可以在后台输入 项目地址/druid进行登录访问


到此这篇关于一文秒懂springboot druid 配置的文章就介绍到这了,更多相关springboot druid 配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_41820986/article/details/119719429

查看更多关于一文秒懂springboot druid 配置的详细内容...

  阅读:20次