One, foreword
What is Druid?
Druid is the best database connection pool in the Java language, providing powerful monitoring and extension capabilities.
For more information, please refer to github.com/alibaba/dru…
This paper will be based onSpringboot 2.1.8. RELEASE
+ Mybatis - plus 2.2.0
To integrateDruid
The connection pool
Springboot integration druid start
1.pom.xml
The introduction ofdruid
Rely on
<! Alibaba </groupId> <artifactId> Druid </artifactId> The < version > 1.1.10 < / version > < / dependency >Copy the code
2,application.yml
Configure the druid
Spring: # configuration data source datasource: url: JDBC: mysql: / / 127.0.0.1:3306 / demo? allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # UseSSL =false name: demo username: root password: root platform: MySQL driver-class-name: . Com. Mysql. JDBC Driver # = = = = = = = = = = = = = = = = = = = = = left left left left left left down down down down down using the druid data left left left left left left down down down down down = = = = = = = = = = = = = = = = = = = = = # connection pool type, Druid connection pool springboot temporarily unable to support by default, the need to configure a bean type: com. Alibaba. Druid. Pool. DruidDataSource initialSize: 5 # connection pool initialization minIdle connection Numbers: 5 # connection pool minimum number of idle maxActive: maximum number of active connections maxWait # 20 connection pool: 60000 # configuration for connecting waiting timeout time timeBetweenEvictionRunsMillis: 60000 # configure how long is the interval to conduct a test, testing needs to be closed free connection, unit is a millisecond minEvictableIdleTimeMillis: 300000 # configure a connection in the pool minimum survival time, the unit is a millisecond validationQuery: TestOnBorrow: true testOnBorrow: false testOnReturn: False PSCache # open, and the size of the specified on each connection PSCache poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: ['stat': 'wall' : used to prevent SQL injection into 'slf4j': log] filters: Stat, Wall, slF4J # Enable mergeSql via connectProperties; ConnectionProperties: druid.stat. MergeSql =true; Druid.stat. SlowSqlMillis =5000 #useGlobalDataSourceStat: true Admin # SQL Background login user name loginPassword: admin # SQL background login user passwordCopy the code
The druid core configuration class
/** * <p> Druid </p> ** @author: zhengqing * @description: Druid connection pool monitoring platform http://127.0.0.1:8080/druid/index.html * @ the date: 2019/12/19 18:20 */ @Configuration public class DruidConfig { @Value("${spring.datasource.loginUsername}") private String loginUsername; @Value("${spring.datasource.loginPassword}") private String loginPassword; /** * Configure Druid monitoring ** @param: * @return: org.springframework.boot.web.servlet.ServletRegistrationBean */ @Bean public ServletRegistrationBean druidServlet() { // ServletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); / / IP white list is empty, said all can access and use commas when multiple IP) servletRegistrationBean. AddInitParameter (" allow ", "127.0.0.1"); When there is a common / / IP blacklist (deny prior to allow) servletRegistrationBean. AddInitParameter (" deny ", "127.0.0.2"); / / Settings console login user name and password servletRegistrationBean addInitParameter (" loginUsername ", loginUsername); servletRegistrationBean.addInitParameter("loginPassword", loginPassword); / / whether to reset the data servletRegistrationBean addInitParameter (" resetEnable ", "false"); return servletRegistrationBean; } /** * Configure the web monitoring filter ** @param: * @return: org.springframework.boot.web.servlet.FilterRegistrationBean */ @Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); // Add filter rule Map<String, String> initParams = new HashMap<>(1); / / set to ignore request initParams. Put (" exclusions ", "*. Js, *. GIF, * JPG, *. BMP, *. PNG, *. CSS, *. Ico, / druid / *"); filterRegistrationBean.setInitParameters(initParams); filterRegistrationBean.addInitParameter("profileEnable", "true"); filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE"); filterRegistrationBean.addInitParameter("principalSessionName", ""); filterRegistrationBean.addInitParameter("aopPatterns", "com.example.demo.service"); / / validate all request filterRegistrationBean addUrlPatterns (" / * "); return filterRegistrationBean; } /** * config datasource ** @param: * @return: javax.sql.DataSource */ @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return new DruidDataSource(); } /** * Configure the transaction manager ** @param: * @return: org.springframework.jdbc.datasource.DataSourceTransactionManager */ @Bean(name = "transactionManager") public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } /** * ↓↓↓↓↓ Configure Spring monitoring ↓↓↓↓↓ * DruidStatInterceptor: interceptor provided by Druid ** @param: * @return: com.alibaba.druid.support.spring.stat.DruidStatInterceptor */ @Bean public DruidStatInterceptor druidStatInterceptor() { DruidStatInterceptor dsInterceptor = new DruidStatInterceptor(); return dsInterceptor; } /** * Use regular expressions to configure pointcuts ** @param: * @return: org.springframework.aop.support.JdkRegexpMethodPointcut */ @Bean @Scope("prototype") public JdkRegexpMethodPointcut druidStatPointcut() { JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut(); pointcut.setPattern("com.zhengqing.demo.modules.*.api.*"); return pointcut; } /** * The DefaultPointcutAdvisor class defines advice and pointCut properties. Advice specifies the notification method to use. This is the DruidStatInterceptor class provided by Druid. Pointcut specifies the pointcut * * @param druidStatPointcut * @param druidStatPointcut: * @return: org.springframework.aop.support.DefaultPointcutAdvisor */ @Bean public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) { DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor(); defaultPointAdvisor.setPointcut(druidStatPointcut); defaultPointAdvisor.setAdvice(druidStatInterceptor); return defaultPointAdvisor; }}Copy the code
4, access,http://127.0.0.1:8080/druid/index.htmlViewing Monitoring Information
This article case demo source
Gitee.com/zhengqingya…