springboot整合Druid

1.引入阿里巴巴Druid maven引用

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.12</version>
</dependency>

2.Druid配置文件:

#spring boot 数据源配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    # 下面为连接池的补充设置,应用到上面所有数据源中
    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    logSlowSql: true
    
  #添加对JSP的支持
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp
  
#spring boot jpa配置
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
      naming:
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
    properties:
      format-sql: true

3.在springboot启动类同目录创建一个DruidConfig.java配置类

  1 import com.alibaba.druid.pool.DruidDataSource;
  2 import com.alibaba.druid.support.http.StatViewServlet;
  3 import com.alibaba.druid.support.http.WebStatFilter;
  4 import org.slf4j.Logger;
  5 import org.slf4j.LoggerFactory;
  6 import org.springframework.beans.factory.annotation.Value;
  7 import org.springframework.boot.web.servlet.FilterRegistrationBean;
  8 import org.springframework.boot.web.servlet.ServletRegistrationBean;
  9 import org.springframework.context.annotation.Bean;
 10 import org.springframework.context.annotation.Configuration;
 11 
 12 import javax.sql.DataSource;
 13 import java.sql.SQLException;
 14 
 15 /**
 16  * 阿里巴巴DRUID 配置
 17  */
 18 @Configuration
 19 public class DruidConfig {
 20 
 21     private Logger logger = LoggerFactory.getLogger(DruidConfig.class);
 22 
 23     @Value("${spring.datasource.url}")
 24     private String dbUrl;
 25 
 26     @Value("${spring.datasource.username}")
 27     private String username;
 28 
 29     @Value("${spring.datasource.password}")
 30     private String password;
 31 
 32     @Value("${spring.datasource.driver-class-name}")
 33     private String driverClassName;
 34 
 35     @Value("${spring.datasource.initialSize}")
 36     private int initialSize;
 37 
 38     @Value("${spring.datasource.minIdle}")
 39     private int minIdle;
 40 
 41     @Value("${spring.datasource.maxActive}")
 42     private int maxActive;
 43 
 44     @Value("${spring.datasource.maxWait}")
 45     private int maxWait;
 46 
 47     @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
 48     private int timeBetweenEvictionRunsMillis;
 49 
 50     @Value("${spring.datasource.minEvictableIdleTimeMillis}")
 51     private int minEvictableIdleTimeMillis;
 52 
 53     @Value("${spring.datasource.validationQuery}")
 54     private String validationQuery;
 55 
 56     @Value("${spring.datasource.testWhileIdle}")
 57     private boolean testWhileIdle;
 58 
 59     @Value("${spring.datasource.testOnBorrow}")
 60     private boolean testOnBorrow;
 61 
 62     @Value("${spring.datasource.testOnReturn}")
 63     private boolean testOnReturn;
 64 
 65     @Value("${spring.datasource.filters}")
 66     private String filters;
 67 
 68     @Value("${spring.datasource.logSlowSql}")
 69     private String logSlowSql;
 70 
 71     @Bean
 72     public ServletRegistrationBean druidServlet() {
 73         ServletRegistrationBean reg = new ServletRegistrationBean();
 74         reg.setServlet(new StatViewServlet());
 75         reg.addUrlMappings("/druid/*");
 76         reg.addInitParameter("loginUsername", username);
 77         reg.addInitParameter("loginPassword", password);
 78         reg.addInitParameter("logSlowSql", logSlowSql);
 79         return reg;
 80     }
 81 
 82     @Bean
 83     public FilterRegistrationBean filterRegistrationBean() {
 84         FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
 85         filterRegistrationBean.setFilter(new WebStatFilter());
 86         filterRegistrationBean.addUrlPatterns("/*");
 87         filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
 88         filterRegistrationBean.addInitParameter("profileEnable", "true");
 89         return filterRegistrationBean;
 90     }
 91 
 92     @Bean
 93     public DataSource druidDataSource() {
 94         DruidDataSource datasource = new DruidDataSource();
 95         datasource.setUrl(dbUrl);
 96         datasource.setUsername(username);
 97         datasource.setPassword(password);
 98         datasource.setDriverClassName(driverClassName);
 99         datasource.setInitialSize(initialSize);
100         datasource.setMinIdle(minIdle);
101         datasource.setMaxActive(maxActive);
102         datasource.setMaxWait(maxWait);
103         datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
104         datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
105         datasource.setValidationQuery(validationQuery);
106         datasource.setTestWhileIdle(testWhileIdle);
107         datasource.setTestOnBorrow(testOnBorrow);
108         datasource.setTestOnReturn(testOnReturn);
109         try {
110             datasource.setFilters(filters);
111         } catch (SQLException e) {
112             logger.error("druid configuration initialization filter", e);
113         }
114         return datasource;
115     }
116 
117 }
View Code

然后启动项目,可以看到Druid已经加入到了springboot项目中,可以访问:http://localhost:8080/项目contextpath/druid/login.html查看SQL执行情况。

用户名和密码是配置的数据库 用户名和密码。

posted @ 2019-01-04 15:51  xfma  阅读(223)  评论(0编辑  收藏  举报