Spring Boot教程(16)-集成Druid阿里数据库连接池

Spring Boot教程(1)创建Spring项目

Spring Boot教程(2) – 运行第一个项目

Spring Boot教程(3)-配置文件-多环境配置

Spring Boot教程(4)-日志配置-logback和使用。

Spring Boot教程(5)-web应用开发-模板引擎Thymeleaf

Spring Boot教程(6)-web应用开发-错误处理

Spring Boot教程(7)-文件上传

Spring Boot教程(8)-使用SQL关系型数据库-JdbcTemplate

Spring Boot教程(9)-Spring-data-jpa

Spring Boot教程(10)-使用SQL关系型数据库-h2嵌入式数据库的使用

Spring Boot教程(11)-redis

Spring Boot教程(12)-redis缓存设置,幂等性防重复提交

Spring Boot教程(13)-使用异步消息服务-JMS(ActiveMQ)

Spring Boot教程(14)-使用异步消息服务-AMQP(RabbitMQ)

Spring Boot教程(15)-集成mybatis

Spring Boot教程(16)-集成Druid阿里数据库连接池

Spring Boot教程(17)-Spring Boot集成Swagger

Druid 的介绍

具体对比可以参考链接

https://github.com/alibaba/druid/wiki/%E5%90%84%E7%A7%8D%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E5%AF%B9%E6%AF%94

一、添加依赖

        
            com.alibaba
            druid
            1.0.15
        

二、配置文件

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Encoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#初始化连接大小
spring.datasource.druid.initial-size=8
#最小空闲连接数
spring.datasource.druid.min-idle=5
#最大连接数
spring.datasource.druid.max-active=10
#查询超时时间
spring.datasource.druid.query-timeout=6000
#事务查询超时时间
spring.datasource.druid.transaction-query-timeout=6000
#关闭空闲连接超时时间
spring.datasource.druid.remove-abandoned-timeout=1800

三、添加Druid配置类

package com.wsj.springbootdemo.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class DruidConfiguration {
    @ConditionalOnClass(DruidDataSource.class)
    @ConditionalOnProperty(name = "spring.datasource.type", havingValue =
            "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true)
    static class Druid extends DruidConfiguration {
        @Bean
        @ConfigurationProperties("spring.datasource.druid")
        public DruidDataSource dataSource(DataSourceProperties properties) {
            DruidDataSource druidDataSource = (DruidDataSource)
                    properties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
            DatabaseDriver databaseDriver =
                    DatabaseDriver.fromJdbcUrl(properties.determineUrl());
            String validationQuery = databaseDriver.getValidationQuery();
            if (validationQuery != null) {
                druidDataSource.setValidationQuery(validationQuery);
            }
            return druidDataSource;
        }
    }
}

四、配置监控servlet和filer(具体使用不做描述)

package com.wsj.springbootdemo.utils.servlet;

import com.alibaba.druid.support.http.StatViewServlet;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

/**
 * 项目名称:DruidStatViewServlet;
 * 类 名 称:DruidStatViewServlet;
 * 类 描 述:监控 ;
 * 创 建 人:Angus;
 * 创建时间:2020/5/7 22:00;
 *
 * @version:1.0
 **/
@WebServlet(urlPatterns = {"/druid/*"}, initParams =
        {@WebInitParam(name = "loginUsername", value = "wsj"),
                @WebInitParam(name = "loginPassword", value = "wsj")})
public class DruidStatViewServlet extends StatViewServlet {
    private static final long serialVersionUID = 1L;
}
package com.wsj.springbootdemo.utils.filter;

import com.alibaba.druid.support.http.WebStatFilter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

/**
 * 项目名称:DruidWebStatFilter;
 * 类 名 称:DruidWebStatFilter;
 * 类 描 述:TODO ;
 * 创 建 人:Angus;
 * 创建时间:2020/5/7 22:02;
 *
 * @version:1.0
 **/
@WebFilter(filterName = "druidWebStatFilter", urlPatterns =
        "/*", initParams = {@WebInitParam(name = "exclusions", value
        = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")})
public class DruidWebStatFilter extends WebStatFilter {
}

github地址:https://github.com/itwsj/springbootdemo

 

你可能感兴趣的:(数据库,SpringBoot)