欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring Boot 自定义数据源DruidDataSource代码

时间:2021-01-15 11:16:03|栏目:JAVA代码|点击:

这篇文章主要介绍了Spring Boot 自定义数据源DruidDataSource代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、添加依赖

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

二、配置application.yml

spring:
  datasource:
url: jdbc:mysql://127.0.0.1:3306/mxntest?characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 50
# 配置获取连接等待超时的时间
maxWait: 60000

三、Druid数据源配置

package com.example.demo.config;

/**
 * @author 12084
 * @create 2018-08-09 11:27
 */

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Druid数据源配置
 */
@Configuration
public class DataSourceConfig {

private static String dbUrl;

private static String username;

private static String password;

private static String driverClassName;

private static int initialSize;

private static int minIdle;

private static int maxActive;

private static int maxWait;

/**
 * 注册DruidServlet
 *
 * @return
 */
@Bean
public ServletRegistrationBean druidServletRegistrationBean() {
  ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
  servletRegistrationBean.setServlet(new StatViewServlet());
  servletRegistrationBean.addUrlMappings("/druid/*");
  //登录查看信息的账号密码.
  servletRegistrationBean.addInitParameter("loginUsername", "admin");
  servletRegistrationBean.addInitParameter("loginPassword", "123456");
  return servletRegistrationBean;
}

/**
 * 注册DruidFilter拦截
 *
 * @return
 */
@Bean
public FilterRegistrationBean druidFilterRegistrationBean() {
  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  filterRegistrationBean.setFilter(new WebStatFilter());
  Map<String, String> initParams = new HashMap<String, String>();
  //设置忽略请求
  initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
  filterRegistrationBean.setInitParameters(initParams);
  filterRegistrationBean.addUrlPatterns("/*");
  return filterRegistrationBean;
}

/**
 * 配置DataSource
 * @return
 * @throws SQLException
 */
@Bean(initMethod = "init",destroyMethod = "close")
@Primary
public DataSource dataSource() throws SQLException {
  DruidDataSource druidDataSource = new DruidDataSource();
  druidDataSource.setUsername(username);
  druidDataSource.setPassword(password);
  druidDataSource.setUrl(dbUrl);
  druidDataSource.setFilters("stat,wall");
  druidDataSource.setInitialSize(initialSize);
  druidDataSource.setMinIdle(minIdle);
  druidDataSource.setMaxActive(maxActive);
  druidDataSource.setMaxWait(maxWait);
  druidDataSource.setUseGlobalDataSourceStat(true);
  druidDataSource.setDriverClassName(driverClassName);
  return druidDataSource;
}

@Value("${spring.datasource.url}")
public void setDbUrl(String dbUrl) {
  DataSourceConfig.dbUrl = dbUrl;
}

@Value("${spring.datasource.username}")
public void setUsername(String username) {
  DataSourceConfig.username = username;
}

@Value("${spring.datasource.password}")
public void setPassword(String password) {
  DataSourceConfig.password = password;
}

@Value("${spring.datasource.driver-class-name}")
public void setDriverClassName(String driverClassName) {
  DataSourceConfig.driverClassName = driverClassName;
}

@Value(value = "${spring.datasource.initialSize}")
public void setInitialSize(int initialSize) {
  DataSourceConfig.initialSize = initialSize;
}

@Value(value = "${spring.datasource.minIdle}")
public void setMinIdle(int minIdle) {
  DataSourceConfig.minIdle = minIdle;
}

@Value(value = "${spring.datasource.maxActive}")
public void setMaxActive(int maxActive) {
  DataSourceConfig.maxActive = maxActive;
}

@Value(value = "${spring.datasource.maxWait}")
public void setMaxWait(int maxWait) {
  DataSourceConfig.maxWait = maxWait;
}

}

四、http://localhost:8080/druid/index.html 就可以查看,如果配置密码则输入密码

  //登录查看信息的账号密码.
  servletRegistrationBean.addInitParameter("loginUsername", "admin");
  servletRegistrationBean.addInitParameter("loginPassword", "123456");

上一篇:java中文传值乱码问题的解决方法

栏    目:JAVA代码

下一篇:mybatis中oracle实现分页效果实例代码

本文标题:Spring Boot 自定义数据源DruidDataSource代码

本文地址:http://www.codeinn.net/misctech/45784.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有