In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to customize the data source DruidDataSource code for SpringBoot. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Add dependencies
Com.alibaba druid 1.0.26
2. Configure 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# initialization size, minimum, maximum initialSize: 5minIdle: 5maxActive: 5 configuration get connection wait timeout maxWait: 60000
III. Druid data source configuration
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 data source configuration * / @ 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;/** * register DruidServlet * * @ return * / @ Beanpublic ServletRegistrationBean druidServletRegistrationBean () {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean () ServletRegistrationBean.setServlet (new StatViewServlet ()); servletRegistrationBean.addUrlMappings ("/ druid/*"); / / the account password for logging in to view information. ServletRegistrationBean.addInitParameter ("loginUsername", "admin"); servletRegistrationBean.addInitParameter ("loginPassword", "123456"); return servletRegistrationBean;} / * register DruidFilter intercept * * @ return * / @ Beanpublic FilterRegistrationBean druidFilterRegistrationBean () {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean (); filterRegistrationBean.setFilter (new WebStatFilter ()); Map initParams = new HashMap () / / set ignore request initParams.put ("exclusions", "* .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); filterRegistrationBean.setInitParameters (initParams); filterRegistrationBean.addUrlPatterns ("/ *"); return filterRegistrationBean;} / * * configure 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;}}
4. Http://localhost:8080/druid/index.html can be viewed. If you configure a password, enter the password.
/ / log in to view the account password of the information. ServletRegistrationBean.addInitParameter ("loginUsername", "admin"); servletRegistrationBean.addInitParameter ("loginPassword", "123456")
On "SpringBoot how to customize the data source DruidDataSource code" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.