SpringBoot教程&笔记|Demo05-整合Druid数据源 有更新!

  heardfate

本文主要讲解如何在springboot下整合druid数据源。

本教程在Demo04基础上添加druid数据源配置信息

添加依赖

引入 druid 依赖:

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

添加依赖

配置

application.yml 配置文件中添加 Druid 相关配置:

# DataSource Config
spring:
  datasource:
	##########################  druid配置   ##########################
	type: com.alibaba.druid.pool.DruidDataSource
	# 初始化大小,最小,最大  
	initialSize: 5
	minIdle: 5
	maxActive: 20
	# 配置获取连接等待超时的时间  
	maxWait: 60000
	# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒  
	timeBetweenEvictionRunsMillis: 60000
	# 配置一个连接在池中最小生存的时间,单位是毫秒  
	minEvictableIdleTimeMillis: 300000
	# 校验SQL,Oracle配置 validationQuery: SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用  
	validationQuery: SELECT 'x'
	testWhileIdle: true
	testOnBorrow: false
	testOnReturn: false
	# 打开PSCache,并且指定每个连接上PSCache的大小  
	poolPreparedStatements: true
	maxPoolPreparedStatementPerConnectionSize: 20
	# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
	filters: stat,wall
	# 通过connectProperties属性来打开mergeSql功能;慢SQL记录  
	connectionProperties:
	  druid.stat.mergeSql: true
	  druid.stat.slowSqlMillis: 5000
	# 合并多个DruidDataSource的监控数据  
	useGlobalDataSourceStat: true

配置

DruidConfig配置

然后在项目中加入DruidConfig配置类
src/main/java/com/heardfate/springboot/demo/demo04/config/DruidConfig.java

package com.heardfate.springboot.demo.demo04.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 javax.sql.DataSource;

/**
 * @since: 2018/10/25
 * @author: Mr.HeardFate
 */
 @Configuration
public class DruidConfig {
	/**
	  * 主要实现WEB监控的配置处理 
	  * @return
	  */
	@Bean
	public ServletRegistrationBean druidServlet() {
	  // 现在要进行druid监控的配置处理操作
	  ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
	  // 白名单
	  //servletRegistrationBean.addInitParameter("allow", "127.0.0.1,192.168.1.159"); 
	  // 黑名单 
	  // servletRegistrationBean.addInitParameter("deny", "192.168.1.200"); 
	  // 用户名  
	  servletRegistrationBean.addInitParameter("loginUsername", "user");
	  // 密码
	  servletRegistrationBean.addInitParameter("loginPassword", "test");
	  // 是否可以重置数据源
	  servletRegistrationBean.addInitParameter("resetEnable", "false");
	  return servletRegistrationBean ;
	}
    
	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		// 所有请求进行监控处理
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
		return filterRegistrationBean;
	}
	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource druidDataSource() {
		return new DruidDataSource();
	}
}

代码生成器

UserController

接着添加UserController控制器
src/main/java/com/heardfate/springboot/demo/demo04/controller/UserController.java

package com.heardfate.springboot.demo.demo04.controller;

import com.heardfate.springboot.demo.demo04.entity.User;
import com.heardfate.springboot.demo.demo04.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

 /**
  *  前端控制器
  * @author Heardfate
  * @since 2018-10-23
  */
  @RestController
  @RequestMapping("/demo04/user")
  public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json")
	public Long saveUser(@RequestBody User user) {
	  System.out.println(user);
	  userService.save(user);
	  return user.getId();
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json")
	public Boolean deleteUser(@PathVariable Long id) {
	  return userService.removeById(id);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = "application/json")
	public User updateUser(@PathVariable Long id, @RequestBody User user) {
	  user.setId(id);
	  userService.updateById(user);
	  return userService.getById(id);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
	public User getUser(@PathVariable Long id) {
	  User user = userService.getById(id);
	  return user;
	}
	
	@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
	public List getUserList() {
	  return userService.list(null);
	}
}

MybatisPlus配置

Druid后台

Druid后台
Druid后台
Druid后台
Druid后台