当前位置:Gxlcms > 数据库问题 > 阿里巴巴Druid数据库连接池的使用

阿里巴巴Druid数据库连接池的使用

时间:2021-07-01 10:21:17 帮助过:20人阅读

xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xiangxu</groupId> <artifactId>qcbx</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>qcbx</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--数据库相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> View Code

 

2 配置数据库连接信息

  技术分享图片

技术分享图片
server: # 服务配置
  port: 80
#  context-path: /dev
spring:
  datasource: # 数据库连接池配置
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springdemo?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: asdf

    type: com.alibaba.druid.pool.DruidDataSource
    #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,‘wall‘用于防火墙
    filters: stat, wall, log4j
    #最大活跃数
    maxActive: 20
    #初始化数量
    initialSize: 1
    #最大连接等待超时时间
    maxWait: 60000
    #打开PSCache,并且指定每个连接PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    #通过connectionProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1 from dual
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
#    filters: stat

  jpa: # jpa配置
    hibernate:
#      ddl-auto: create # 会在首次启动的时候根据相应的实体类在数据库中创建相应的表,如果之前有表了,就会删掉在重建
#      ddl-auto: update # 如果没有表会创建,有表会对表结构进行修改
#     ddl-auto:create-drop # 应用停下来时会将表删掉
      ddl-auto: none  # 什么都不做(默认值)
#      ddl-auto: validate # 检查实体类字段和表字段是否一致,如果不一致就会报错
    show-sql: true # 执行数据库操作时会打印出相应的SQL语句

#  mvc:
#    view: # jsp文件路径设置
#      prefix: /WEB-INF/jsp/ # jsp视图前缀
#      suffix: .jsp # jsp视图后缀

  http:
    multipart: # 上传文件大小设置
      max-file-size: 1024Mb # 设置最大上传大小
      max-request-size: 2048Mb # 设置最大请求大小

#  redis: # rediss缓存设置
#    host: 127.0.0.1 # 监听IP地址
#    port: 6379 # 端口号
#    pool:
#      max-idle: 20
#      min-idle: 1
#      max-active: 20
#      max-wait: 60000
#    database: 0

logging: # 日志配置
  level:
#    cn.xiangxu.spring.controller.logger: ‘off‘ # 屏蔽掉相应包的日志控制台输出和日记文件保存
View Code

 

3 配置druid

package cn.xiangxu.qcbx.configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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;

/**
 * Druid配置
 */
@Configuration
public class DruidConfiguration {
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //创建servlet注册实体
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //设置ip白名单
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
        servletRegistrationBean.addInitParameter("deny","192.168.0.19");
        //设置控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername","wys");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        //是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的形式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

 

4 启动成功效果

  技术分享图片

 

阿里巴巴Druid数据库连接池的使用

标签:reg   code   com   star   mini   type   表结构   cti   des   

人气教程排行