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/01 Report--
This article mainly introduces "how to integrate Druid data sources in SpringBoot". In daily operation, I believe many people have doubts about how to integrate Druid data sources in SpringBoot. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to integrate Druid data sources in SpringBoot". Next, please follow the editor to study!
1. Database structure
two。 Project structure
3.pom.xml file
Org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime com.alibaba druid 1.1.8 log4j log4j 1.2.17 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
4.application.yml profile
Spring: datasource: username: root password: wangqing url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # other configurations initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true# configure filters intercepted by monitoring statistics. Sql cannot do statistics when the monitoring interface is removed. 'wall' is used for firewall filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true Druid.stat.slowSqlMillis=500 # merge monitoring data of multiple DruidDataSource # useGlobalDataSourceStat: true mybatis: # specify global configuration file location # config-location: classpath:mybatis/mybatis-config.xml # specify sql mapping file location mapper-locations: classpath:mapper/*.xml # such as TUserMapper.xml # schema:#-classpath:sql/department.sql # under mappers file under src/main/resources Sql statement to create table #-classpath:sql/employee.sql
5. Create a configuration class for DruidConfig and instantiate Druid Datasource
Package com.qingfeng.config; import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;import org.springframework.boot.context.properties.ConfigurationProperties;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 javax.sql.DataSource;import java.util.Arrays;import java.util.HashMap Import java.util.Map; @ Configurationpublic class DruidConfig {/ / specifies the mapping @ ConfigurationProperties (prefix = "spring.datasource") @ Bean public DataSource druid () {return new DruidDataSource () between the attribute in the / / DruidDataSource class at the beginning of spring.datasource in the appliction.yml file and the spring.datasource in the appliction.yml file. } / / configure Druid monitoring / / 1, configure a management background Servlet @ Bean public ServletRegistrationBean statViewServlet () {ServletRegistrationBean bean = new ServletRegistrationBean (new StatViewServlet (), "/ druid/*"); Map initParams = new HashMap (); initParams.put ("loginUsername", "admin"); initParams.put ("loginPassword", "123456"); initParams.put ("allow", "") / / the default is to allow all access to initParams.put ("deny", "); bean.setInitParameters (initParams); return bean;} / / 2, configure a filter @ Bean public FilterRegistrationBean webStatFilter () for web monitoring {FilterRegistrationBean bean = new FilterRegistrationBean (); bean.setFilter (new WebStatFilter ()); Map initParams = new HashMap (); initParams.put (" exclusions "," * .js,*.css,/druid/* ") Bean.setInitParameters (initParams); bean.setUrlPatterns (Arrays.asList ("/ *")); return bean;}}
6. Create a UserController class test
Package com.qingfeng.controller; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody; import java.util.List;import java.util.Map; @ Controllerpublic class UserController {@ Autowired JdbcTemplate jdbcTemplate; @ ResponseBody @ GetMapping ("/ query") public Map map () {List list = jdbcTemplate.queryForList ("select * FROM user") Return list.get (0);}}
7. Run the project and access http://localhost:8080/query through a browser
8. The following code configured in our DruidConfig class can help us monitor
/ / configure Druid monitoring / / 1, configure a management background Servlet @ Bean public ServletRegistrationBean statViewServlet () {ServletRegistrationBean bean = new ServletRegistrationBean (new StatViewServlet (), "/ druid/*"); Map initParams = new HashMap (); initParams.put ("loginUsername", "admin"); initParams.put ("loginPassword", "123456"); initParams.put ("allow", "") / / the default is to allow all access to initParams.put ("deny", ""); bean.setInitParameters (initParams); return bean;}
9. We start the project and open the URL: http://localhost:8080/druid/login.html can log in to view the status monitoring of druid data sources
What we set above is the user name: admin password: 123456
At this point, the study on "how to integrate Druid data sources in SpringBoot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.