SpringBoot1.5.10.RELEASE整合druid

Posted mYunYu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot1.5.10.RELEASE整合druid相关的知识,希望对你有一定的参考价值。

1、先在pom文件中导入druid的jar包

1         <dependency>
2             <groupId>com.alibaba</groupId>
3             <artifactId>druid</artifactId>
4             <version>1.1.10</version>
5         </dependency>    

 

2、新建DruidDataSourceConfig 配置类

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

3、再在application.properties配置文件中配置相关参数

 1 #配置druid数据源
 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 3 spring.datasource.url= jdbc:mysql://localhost:3306/dbName?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=round&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
 4 spring.datasource.username=root
 5 spring.datasource.password=123456
 6 
 7 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 8 # 下面为连接池的补充设置,应用到上面数据源中
 9 spring.datasource.initialSize=5
10 spring.datasource.minIdle=5
11 spring.datasource.maxActive=20
12 # 配置获取连接等待超时的时间
13 spring.datasource.maxWait=60000
14 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
15 spring.datasource.timeBetweenEvictionRunsMillis=60000
16 # 配置一个连接在池中最小生存的时间,单位是毫秒
17 spring.datasource.minEvictableIdleTimeMillis=300000
18 spring.datasource.validationQuery=SELECT 1 FROM DUAL
19 spring.datasource.testWhileIdle=true
20 spring.datasource.testOnBorrow=false
21 spring.datasource.testOnReturn=false
22 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,\'wall\'用于防火墙
23 spring.datasource.filters=stat,wall,log4j
24 spring.datasource.logSlowSql=true

访问:http://localhost:8080/springboot/druid进行登录访问。

可以看出,是使用了druid连接池的

 

以上是关于SpringBoot1.5.10.RELEASE整合druid的主要内容,如果未能解决你的问题,请参考以下文章

我试图在 h2 嵌入式数据库中插入 blob 数据,但它从类路径/URI 失败。但是它适用于绝对路径

将浏览器时区传递到后端spring引导应用程序,以根据浏览器时区生成包含日期的报告

PHP取整四舍五入取整向上取整向下取整小数截取

PHP取整,四舍五入取整向上取整向下取整小数截取

c语言声明函数函数19

PHP取整,四舍五入取整向上取整向下取整小数截取