11.Spring Boot 集成Druid

Spring Boot 集成Druid

      Spring Boot 使用的数据源默认的是:org.apache.tomcat.jdbc.pool.DataSource,在上篇的时候有没有发现没有配置数据源居然也行,是因为引入了JDBC Starter Poms 所以Spring Boot会帮我们自动创建,而在application.properties中的属性也是Spring Boot默认的。你可以在获取数据源的时候将数据源名称打印出来就知道了。接下来我们将说的是使用Druid来替换Spring Boot 默认的数据源,以下代码基于( 10.玩转Spring Boot 集成Mybatis


1.在pom.xml加入Druid依赖,代码如下:

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

2.在application.properties中加入以下代码:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#最小连接数量
spring.datasource.minIdle=2
#最大连接数量
spring.datasource.maxActive=5
#获取连接等待超时的时间
spring.datasource.maxWait=60000
#间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
#连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
#验证SQL
spring.datasource.validationQuery=SELECT 'x' FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
#打开PSCache,并且指定每个连接上PSCache的大小如果用Oracle,
#则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#配置监控统计拦截的filters
spring.datasource.filters=stat


3.创建DruidConfigProperties类,代码如下:

package com.chengli.springboot.mybatis.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfigProperties {
	private String driverClassName;
	private String url;
	private String username;
	private String password;
	private Integer minIdle;
	private Integer maxActive;
	private Integer maxWait;
	private Long timeBetweenEvictionRunsMillis;
	private Long minEvictableIdleTimeMillis;
	private String validationQuery;
	private Boolean testWhileIdle;
	private Boolean testOnBorrow;
	private Boolean testOnReturn;
	private Boolean poolPreparedStatements;
	private Integer maxPoolPreparedStatementPerConnectionSize;
	private String filters;

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(Integer minIdle) {
		this.minIdle = minIdle;
	}

	public Integer getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(Integer maxActive) {
		this.maxActive = maxActive;
	}

	public Integer getMaxWait() {
		return maxWait;
	}

	public void setMaxWait(Integer maxWait) {
		this.maxWait = maxWait;
	}

	public Long getTimeBetweenEvictionRunsMillis() {
		return timeBetweenEvictionRunsMillis;
	}

	public void setTimeBetweenEvictionRunsMillis(Long timeBetweenEvictionRunsMillis) {
		this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
	}

	public Long getMinEvictableIdleTimeMillis() {
		return minEvictableIdleTimeMillis;
	}

	public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {
		this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
	}

	public String getValidationQuery() {
		return validationQuery;
	}

	public void setValidationQuery(String validationQuery) {
		this.validationQuery = validationQuery;
	}

	public Boolean getTestWhileIdle() {
		return testWhileIdle;
	}

	public void setTestWhileIdle(Boolean testWhileIdle) {
		this.testWhileIdle = testWhileIdle;
	}

	public Boolean getTestOnBorrow() {
		return testOnBorrow;
	}

	public void setTestOnBorrow(Boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}

	public Boolean getTestOnReturn() {
		return testOnReturn;
	}

	public void setTestOnReturn(Boolean testOnReturn) {
		this.testOnReturn = testOnReturn;
	}

	public Boolean getPoolPreparedStatements() {
		return poolPreparedStatements;
	}

	public void setPoolPreparedStatements(Boolean poolPreparedStatements) {
		this.poolPreparedStatements = poolPreparedStatements;
	}

	public Integer getMaxPoolPreparedStatementPerConnectionSize() {
		return maxPoolPreparedStatementPerConnectionSize;
	}

	public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) {
		this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
	}

	public String getFilters() {
		return filters;
	}

	public void setFilters(String filters) {
		this.filters = filters;
	}

}
 

4.在MybatisConfig类中加入以下代码:

@Bean(initMethod = "init", destroyMethod = "close") 
    public DruidDataSource dataSource() throws SQLException { 
      DruidDataSource druidDataSource = new DruidDataSource(); 
      druidDataSource.setDriverClassName(druidConfigProperties.getDriverClassName()); 
      druidDataSource.setUrl(druidConfigProperties.getUrl()); 
      druidDataSource.setUsername(druidConfigProperties.getUsername()); 
      druidDataSource.setPassword(druidConfigProperties.getPassword()); 
      druidDataSource.setInitialSize(druidConfigProperties.getMinIdle()); 
      druidDataSource.setMinIdle(druidConfigProperties.getMinIdle()); 
      druidDataSource.setMaxActive(druidConfigProperties.getMaxActive()); 
      druidDataSource.setMaxWait(druidConfigProperties.getMaxWait()); 
      druidDataSource.setTimeBetweenEvictionRunsMillis(druidConfigProperties.getTimeBetweenEvictionRunsMillis()); 
      druidDataSource.setMinEvictableIdleTimeMillis(druidConfigProperties.getMinEvictableIdleTimeMillis()); 
      druidDataSource.setValidationQuery(druidConfigProperties.getValidationQuery()); 
      druidDataSource.setTestWhileIdle(druidConfigProperties.getTestWhileIdle()); 
      druidDataSource.setTestOnBorrow(druidConfigProperties.getTestOnBorrow()); 
      druidDataSource.setTestOnReturn(druidConfigProperties.getTestOnReturn()); 
      druidDataSource.setPoolPreparedStatements(druidConfigProperties.getPoolPreparedStatements()); 
      druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidConfigProperties.getMaxPoolPreparedStatementPerConnectionSize()); 
      druidDataSource.setFilters(druidConfigProperties.getFilters()); 
      return druidDataSource; 
    } 

5.加入Druid监控,代码如下:

package com.chengli.springboot.mybatis.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidRegistionConfig {
	@Bean
	public ServletRegistrationBean statViewServlet() {
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
		servletRegistrationBean.setServlet(new StatViewServlet());
		//拦截路径
		servletRegistrationBean.addUrlMappings("/druid/*");
		// 设置访问白名单
		servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
		// 设置访问黑名单
		// reg.addInitParameter("deny", "");
		// 设置是否允许清空计数
		servletRegistrationBean.addInitParameter("resetEnable", "false");
		// 登录用户名
		servletRegistrationBean.addInitParameter("loginUsername", "chengli");
		// 登录密码
		servletRegistrationBean.addInitParameter("loginPassword", "chengli");
		
		return servletRegistrationBean;
	}

	@Bean
	public FilterRegistrationBean webStatFilter() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		//拦截路径
		filterRegistrationBean.addUrlPatterns("/*");
		//排除指定路径
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		
		return filterRegistrationBean;
	}
}
到这里就已经完成了,运行试试吧。完整的示例代码放在QQ交流群中 springboot-mybatis.zip
有兴趣的朋友可以加群探讨相互学习:
Spring Boot QQ交流群:599546061
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值