资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot如何自定义数据源DruidDataSource代码

这篇文章将为大家详细讲解有关SpringBoot如何自定义数据源DruidDataSource代码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为昭平企业提供专业的网站设计、网站制作,昭平网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

一、添加依赖

   com.alibaba    druid    1.0.26  

二、配置application.yml

spring:  datasource:url: jdbc:MySQL://127.0.0.1:3306/mxntest?characterEncoding=UTF-8username: rootpassword: rootdriver-class-name: com.mysql.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource# 初始化大小,最小,最大initialSize: 5minIdle: 5maxActive: 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数据源配置 */@Configurationpublic 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 */@Beanpublic 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 */@Beanpublic FilterRegistrationBean druidFilterRegistrationBean() {  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();  filterRegistrationBean.setFilter(new WebStatFilter());  Map initParams = new HashMap();  //设置忽略请求  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")@Primarypublic 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");

关于“SpringBoot如何自定义数据源DruidDataSource代码”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


新闻名称:SpringBoot如何自定义数据源DruidDataSource代码
文章分享:http://cdkjz.cn/article/jccccd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220