Spring Boot 自定义数据源DruidDataSource代码
这篇文章主要介绍了SpringBoot自定义数据源DruidDataSource代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一、添加依赖
com.alibaba druid 1.0.26
二、配置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数据源配置
packagecom.example.demo.config; /** *@author12084 *@create2018-08-0911:27 */ importcom.alibaba.druid.pool.DruidDataSource; importcom.alibaba.druid.support.http.StatViewServlet; importcom.alibaba.druid.support.http.WebStatFilter; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.boot.web.servlet.FilterRegistrationBean; importorg.springframework.boot.web.servlet.ServletRegistrationBean; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.Primary; importjavax.sql.DataSource; importjava.sql.SQLException; importjava.util.HashMap; importjava.util.Map; /** *Druid数据源配置 */ @Configuration publicclassDataSourceConfig{ privatestaticStringdbUrl; privatestaticStringusername; privatestaticStringpassword; privatestaticStringdriverClassName; privatestaticintinitialSize; privatestaticintminIdle; privatestaticintmaxActive; privatestaticintmaxWait; /** *注册DruidServlet * *@return */ @Bean publicServletRegistrationBeandruidServletRegistrationBean(){ ServletRegistrationBeanservletRegistrationBean=newServletRegistrationBean(); servletRegistrationBean.setServlet(newStatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*"); //登录查看信息的账号密码. servletRegistrationBean.addInitParameter("loginUsername","admin"); servletRegistrationBean.addInitParameter("loginPassword","123456"); returnservletRegistrationBean; } /** *注册DruidFilter拦截 * *@return */ @Bean publicFilterRegistrationBeandruidFilterRegistrationBean(){ FilterRegistrationBeanfilterRegistrationBean=newFilterRegistrationBean(); filterRegistrationBean.setFilter(newWebStatFilter()); MapinitParams=newHashMap (); //设置忽略请求 initParams.put("exclusions","*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); filterRegistrationBean.setInitParameters(initParams); filterRegistrationBean.addUrlPatterns("/*"); returnfilterRegistrationBean; } /** *配置DataSource *@return *@throwsSQLException */ @Bean(initMethod="init",destroyMethod="close") @Primary publicDataSourcedataSource()throwsSQLException{ DruidDataSourcedruidDataSource=newDruidDataSource(); 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); returndruidDataSource; } @Value("${spring.datasource.url}") publicvoidsetDbUrl(StringdbUrl){ DataSourceConfig.dbUrl=dbUrl; } @Value("${spring.datasource.username}") publicvoidsetUsername(Stringusername){ DataSourceConfig.username=username; } @Value("${spring.datasource.password}") publicvoidsetPassword(Stringpassword){ DataSourceConfig.password=password; } @Value("${spring.datasource.driver-class-name}") publicvoidsetDriverClassName(StringdriverClassName){ DataSourceConfig.driverClassName=driverClassName; } @Value(value="${spring.datasource.initialSize}") publicvoidsetInitialSize(intinitialSize){ DataSourceConfig.initialSize=initialSize; } @Value(value="${spring.datasource.minIdle}") publicvoidsetMinIdle(intminIdle){ DataSourceConfig.minIdle=minIdle; } @Value(value="${spring.datasource.maxActive}") publicvoidsetMaxActive(intmaxActive){ DataSourceConfig.maxActive=maxActive; } @Value(value="${spring.datasource.maxWait}") publicvoidsetMaxWait(intmaxWait){ DataSourceConfig.maxWait=maxWait; } }
四、http://localhost:8080/druid/index.html就可以查看,如果配置密码则输入密码
//登录查看信息的账号密码. servletRegistrationBean.addInitParameter("loginUsername","admin"); servletRegistrationBean.addInitParameter("loginPassword","123456");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。