Druid is the best database connection pool in Java language. Compared with other database connection pools, Druid has two major features:
Today I will demonstrate Spring Boot integration Druid.
Actual combat
1. Add Maven dependencies.
Spring Boot version uses 1.x, and 2.x version druid starter does not support it. But customization is fine.
<!--starter-web facilitates us to view the effect-> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--It is also possible to use Mybatis, druid provides only connection pool--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.6</version> </dependency>
Configure Druid
2. Configuration of Druid application.
server: port: 9011 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver druid: initial-size: 5 max-active: 10 min-idle: 5 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 validation-query: select 1 validation-query-timeout: 60000 test-on-borrow: false test-on-return: false test-while-idle: true time-between-eviction-runs-millis: 60000 filter: stat: log-slow-sql: true db-type: mysql slow-sql-millis: 2000 stat-view-servlet: login-username: druid login-password: druid allow: 127.0.0.1 url-pattern: /druid/* username: root password: 123456 url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8
The configuration of JavaBean is more flexible, and we configure it through JavaBean.
@Configurationpublic class DruidConfig { @Bean public JdbcTemplate jdbcTemplate(){ return new JdbcTemplate(druidDataSource()); } // ConfigurationProperties can directly inject the value starting from the spring.datasource.druid property of the application configuration into DruidDataSource @ConfigurationProperties(prefix = "spring.datasource.druid") @Bean(initMethod = "init",destroyMethod = "close") public DruidDataSource druidDataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); // Add druid's monitoring filter. Currently, only the monitoring function is demonstrated. Therefore, there is only one filter, which can implement multiple filters. LinkedList<Filter> filtersList = new LinkedList(); filtersList.add(filter()); druidDataSource.setProxyFilters(filtersList); return druidDataSource; } @Bean public Filter filter(){ StatFilter statFilter = new StatFilter(); // SQL execution time exceeds 2s is determined to be slow log statFilter.setSlowSqlMillis(2000); //Show slow log statFilter.setLogSlowSql(true); //MergeSQL, sometimes, too many of the same slow logs affect reading, enable the merge function statFilter.setMergeSql(true); return statFilter; } // Monitoring panel @Bean public ServletRegistrationBean servletRegistrationBean(){ // Register your own Sevlet return new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); }} 3. Create new SQL to execute tests
Use JDBCTeplate to select data in the database, we are just demonstrating the monitoring effect of Druid.
@RestController@SpringBootApplicationpublic class DaoApplication { public static void main(String[] args) { SpringApplication.run(DaoApplication.class,args); } @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/test") public List test(){ final List<Integer> idList = new LinkedList<Integer>(); jdbcTemplate.query("select * from sh_test1", new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { idList.add(rs.getInt(1)); } }); return idList; }}Run to view the effect
5. The demonstration is completed
At this point, Druid can already be used in Spring Boot. Druid provides many monitoring options. The article is limited in length. I will only introduce the usage of Druid integrating Spring Boot.
at last
This article demonstrates the use of Druid in SpringBoot. For the use of Druid, please see the reference below.
refer to
[Common usage of Druid] (https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.