springboot整合druid连接池的步骤

网友投稿 620 2023-03-06

springboot整合druid连接池的步骤

springboot整合druid连接池的步骤

使用springboot默认的连接池

导入springboot data-jdbc依赖

org.springframework.boot

spring-boot-starter-data-jdbc

配置文件配置连接池

spring:

datasource:

username: root

password: 5201314

url: jdbc:mysql:///jqmb?serverTimezone=UTC

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

springboot默认的连接池

@Autowired

DataSource dataSource;

@Test

void contextLoads() {

System.out.println(dataSource.getClass());

System.out.println("____________________________________");

}

使用连接池druid

导入druid依赖

com.alibaba

druid

1.0.18

配置文件配置druid的属性

spring:

datasource:

username: root

password: 5201314

url: jdbc:mysql:///jqmb?serverTimezone=UTC

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

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: stat,wall,log4j

maxPoolPreparedStatementPerConnectionSize: 20

useGlobalDataSourceStat: true

connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

配置类中对druid属性进行绑定

@Configuration

public class DataSource_Druid_Configure {

@ConfigurationProperties(prefix = "spring.datasource")

@Bean

public DruidDataSource getDataSour(){

return new DruidDataSource();

}

配置Druid的监控后台

//配置Druid的监控

//1、配置一个管理后台的Servlet

@Bean

public ServletRegistrationBean statViewServlet(){

ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

Map initParams = new HashMap<>();

initParams.put("loginUsername","admin");//登录用户名

initParams.put("loginPassword","123456");//登录密码

initParams.put("allow","");//默认就是允许所有访问

initParams.put("deny","192.168.15.21");//拒绝访问

bean.setInitParameters(initParams);

return bean;

}

//2、配置一个web监控的filter

@Bean

public FilterRegistrationBean webStatFilter(){

FilterRegistrationBean bean = new FilterRegistrationBean();

bean.setFilter(new WebStatFilter());

Map initParams = new HashMap<>();

initParams.put("exclusions","*.js,*.css,/druid/*");

bean.setInitParhttp://ameters(initParams);

bean.setUrlPatterns(Arrays.asList("/*"));

return bean;

}

访问http://localhost:8090/druid/login.html

如果sql监控失效需要导入log4j 依赖

log4j

log4j

1.2.17

以上就是springboot整合druid连接池的步骤的详细内容,更多关于springboot整合druid连接池的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信开发者工具(微信开发者工具怎么开发小程序)
下一篇:讯飞的插件 小程序(讯飞的插件 小程序在哪)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~