SoFunction
Updated on 2025-03-08

SpringBoot Integration Druid Instance Code

Start quickly

rely

<dependency>
            <groupId></groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

If you need to configure the log:

 <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

Configuration

spring:
  datasource:
    name: druidDataSource
    type: 
    druid:
      driver-class-name: 
      url: jdbc:mysql://localhost:3306/databasename?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=UTF-8
      username: root
      password: 12341234
      #Attribute type is a string, and the extension plug-in is configured through alias. Common plug-ins are:      #Filter:stat for monitoring statistics      #filter for logging:log4j      #Defense against sql injection filter:wall      filters: stat,wall,log4j
      #The number of physical connections established during initialization.  Initialization occurs when the display calls the init method, or the first time the getConnection is      initialSize: 10
      #Minimum number of connection pools      minIdle: 10
      #Maximum number of connection pools      maxActive: 100
      # Get the maximum waiting time when connecting, in milliseconds.  After maxWait is configured, fair lock is enabled by default, and the concurrency efficiency will decrease. If necessary, you can use unfair lock by configuring the useUnfairLock property to true.      maxWait: 60000
      # has two meanings:      #1) The Destroy thread will detect the connection interval time, and close the physical connection if the connection idle time is greater than or equal to minEvictableIdleTimeMillis.      #2) The basis for judging testWhileIdle, please see the description of testWhileIdle attribute in detail      timeBetweenEvictionRunsMillis: 60000
      #The minimum time to keep the connection idle without being evict      minEvictableIdleTimeMillis: 300000
      #Sql used to detect whether the connection is valid, requires a query statement, and select 'x' is commonly used.      #If validationQuery is null, testOnBorrow, testOnReturn, and testWhileIdle will not work.      validationQuery: SELECT 'X'
      #It is recommended to configure it to true, which does not affect performance and ensures security.      #Check when applying for a connection. If the idle time is greater than timeBetweenEvictionRunsMillis, execute validationQuery to check whether the connection is valid.      testWhileIdle: true
      # When applying for a connection, perform validationQuery to check whether the connection is valid. This configuration will reduce performance.      testOnBorrow: false
      # When returning the connection, perform validationQuery to check whether the connection is valid. Doing this configuration will reduce performance.      testOnReturn: false
      #Whether prepareStatement is cached, that is, PSCache.  PSCache has greatly improved the performance of databases that support cursors, such as oracle.  It is recommended to close under mysql.      poolPreparedStatements: false
      #To enable PSCache, it must be configured with greater than 0. When greater than 0, poolPreparedStatements will be automatically triggered to modify to true.  In Druid, there is no problem that PSCache takes up too much memory under Oracle. You can configure this value larger, such as 100      maxPoolPreparedStatementPerConnectionSize: -1

This is all about this article about SpringBoot Integration Druid. For more related SpringBoot Integration Druid content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!