Spring boot associated druid
First of all, why do we use this database connection pool? A large part of our development is around the database. When we operate on the database, we need to go in and out of the database connection pool to obtain the connection with the database. Therefore, an efficient and safe database connection pool has become our goal.
So we choose druid, which is the implementation of a database connection pool on Alibaba's open source platform.
The most niubi important thing about him is that he can realize background supervision and log monitoring. It's like hiring a housekeeper to keep an eye on the connection pool, making our pool more secure.
After talking for a long time, let's start step by step to use druid in spring boot.
Guide Package
First, of course, import our dependencies
Be sure to remember to import druid launcher!!!!
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency>
These two packages are enough. One is druid, and the other is used by spring to start druid
The next step is to operate in the springboot configuration file
spring: datasource: username: sa password: 123456 url: jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource Remember to change the type of database connection pool Then the following is the configuration of the database connection pool If you can't identify it, it's start Question of druid: 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 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 server: port: 8080 mybatis: mapper-locations: - classpath:dao/*.xml - classpath*:com/**/dao/*.xml
Once configured, you can use it. If you don't feel at ease, you can test it yourself. Because the connection pool is registered in the bean, we can directly use autowird to inject and then output the getclass of the connection pool. You can see that it has indeed been replaced with druid
Because we also need to use the built-in front-end display of the database connection pool
So we still need to customize a configuration
package com.liujiacheng.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; /** * @author Michelle */ @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource.druid") @Bean public DruidDataSource druidDataSource() { return new DruidDataSource(); } @Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername", "admin"); initParams.put("loginPassword", "123456"); initParams.put("allow", ""); bean.setInitParameters(initParams); return bean; } }
Are some dead code, just use it directly
Then go directly when you visit
http://localhost:8080/druid/helloSql
This website can access this page