1、首先要引入相关jar包,我这里的数据库是oracle

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		
		
	    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>druid</artifactId>
		    <version>1.1.10</version>
		</dependency>
		
		
		<!-- https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc8 -->
		<dependency>
		    <groupId>com.oracle</groupId>
		    <artifactId>ojdbc8</artifactId>
		    <version>12.2.0.1</version>
		</dependency>

2、配置文件 配置上相关的application.properties

spring.datasource.url=jdbc:oracle:thin:@ 10.15.8.32:1521/GIB
spring.datasource.username=pas
spring.datasource.password=pas
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#db的基本配置
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
#用来检测连接是否有效的sql,要求是一个查询语句。
#如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle
#都不会其作用。
spring.datasource.validationQuery=SELECT 1 FROM DUAL
#建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于
#timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
spring.datasource.testWhileIdle=true
#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
spring.datasource.testOnBorrow=false
#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
#是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,
#比如说oracle。在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。
#5.5及以上版本有PSCache,建议开启。
spring.datasource.poolPreparedStatements=true
#要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
#在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
spring.datasource.maxPoolPreparedStatementPerConnectionSize=100
# 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
#监控统计用的filter:stat  日志用的filter:log4j 防御sql注入的filter:wall
spring.datasource.filters=stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

3、我这里讲述两种方式

3-1、用方式注册 servlet、filter、interceptor

@Configuration
@PropertySource(value ="classpath:application.properties")
public class DruidConfiguration {
	
	
	  @Bean(destroyMethod = "close", initMethod = "init")
	  @ConfigurationProperties(prefix = "spring.datasource")
	  public DataSource druidDataSource() {
	        DruidDataSource druidDataSource = new DruidDataSource();
	        return druidDataSource;
	  }
	
	  /**
	     * 注册一个StatViewServlet
	     * @return
	     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet(){
        //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
        ServletRegistrationBean servletRegistrationBean = 
        		new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

        //添加初始化参数:initParams
        //白名单:
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny","192.168.1.73");
        //登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","admin");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    
    /**
     * 注册一个:filterRegistrationBean
     * @return
     */
 
    @Bean
    public FilterRegistrationBean<WebStatFilter> druidStatFilter(){
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = 
        		new FilterRegistrationBean<WebStatFilter>(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
    
    
    /**
     *--------------------- 配置 druid的spring监控------------------------
     * */
    //DruidStatInterceptor类为druid提供的拦截器
    @Bean
    public DruidStatInterceptor druidStatInterceptor() {
        DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
        return dsInterceptor;
    }
    //JdkRegexpMethodPointcut为使用正则表达式配置切点
    @Bean
    @Scope("prototype")
    public JdkRegexpMethodPointcut druidStatPointcut() {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPatterns("com.byd.*");

        return pointcut;
    }
    //DefaultPointcutAdvisor类定义advice及 pointcut 属性。
    //advice指定使用的通知方式,也就是druid提供的DruidStatInterceptor类,pointcut指定切入点。
    @Bean
    public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
        DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        defaultPointAdvisor.setPointcut(druidStatPointcut);
        defaultPointAdvisor.setAdvice(druidStatInterceptor);
        return defaultPointAdvisor;
    }


}

3-2、用注解形式配置 servlet和filter

@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
    @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
})
public class DruidFilter extends WebStatFilter {
	
}
@WebServlet(urlPatterns = "/druid/*", 
    initParams={
//            @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
//            @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
            @WebInitParam(name="loginUsername",value="admin"),// 用户名
            @WebInitParam(name="loginPassword",value="admin"),// 密码
            @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
})
public class DruidServlet extends StatViewServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7290273811139349944L;

}

单独用一个类配置 interceptor

@Configuration
public class DruidSpringInterceptor{
  //DruidStatInterceptor类为druid提供的拦截器
  @Bean
  public DruidStatInterceptor druidStatInterceptor() {
      DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
      return dsInterceptor;
  }
  //JdkRegexpMethodPointcut为使用正则表达式配置切点
  @Bean
  @Scope("prototype")
  public JdkRegexpMethodPointcut druidStatPointcut() {
      JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
      pointcut.setPatterns("com.byd.controller.*","com.byd.server.*","com.byd.dao.*");


      return pointcut;
  }
  //DefaultPointcutAdvisor类定义advice及 pointcut 属性。
  //advice指定使用的通知方式,也就是druid提供的DruidStatInterceptor类,pointcut指定切入点。
  @Bean
  public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
      DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
      defaultPointAdvisor.setPointcut(druidStatPointcut);
      defaultPointAdvisor.setAdvice(druidStatInterceptor);
      return defaultPointAdvisor;
  }
}