springboot使用Druid作为数据源开发步骤

第一:Druid的配置类

@Configuration
public class DruidConfig {

  @ConfigurationProperties(prefix = "spring.druid")
  @Bean(initMethod = "init", destroyMethod = "close")
  public DruidDataSource dataSource(){
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setProxyFilters(Lists.newArrayList(startFilter()));
    return druidDataSource;
  }
  @Bean
  public Filter startFilter(){
    //设置开启慢日志 5000毫秒 并且合并sql语句
    StatFilter startFilter = new StatFilter();
    startFilter.setLogSlowSql(true);
    startFilter.setSlowSqlMillis(5000);
    startFilter.setMergeSql(true);
    return startFilter;
  }

  @Bean
  public ServletRegistrationBean servletRegistrationBean(){
    return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
  }
}

第二:application.properties 

spring.druid.driverClassName=com.mysql.jdbc.Driver
spring.druid.url=jdbc:mysql://localhost:3306/houses?characterEncoding=UTF-8&useSSL=false
spring.druid.password=root
spring.druid.username=root
#最大连接数
spring.druid.maxActive=30
#最小连接数
spring.druid.minIdle=5
#获取连接的最大等待时间
spring.druid.maxWait=10000
#解决mysql8小时的问题
spring.druid.validationQuery=SELECT 'X'

 

你可能感兴趣的:(springboot使用Druid作为数据源开发步骤)