美文网首页springboot
【框架集成5】druid数据连接池集成

【框架集成5】druid数据连接池集成

作者: yedp | 来源:发表于2019-11-22 09:02 被阅读0次

pom.xml添加依赖

  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.20</version>
  </dependency>

添加aplication.yml

spring:
  datasource:
     druid:
           initial-size: 8
           min-idle: 1
           max-active: 20
           max-wait: 60000
           time-between-eviction-runsMillis: 60000
           min-evictable-idle-timeMillis: 300000
           validation-query: select 'x' FROM DUAL
           test-while-idle: true
           test-on-borrow: false
           test-on-return: false
           pool-prepared-statements: true
           max-open-prepared-statements: 20
           max-pool-prepared-statement-per-connection-size: 20
           filters: stat
           connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
           use-global-data-source-stat: true

测试

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@SpringBootTest()
class EzUserApplicationTests {
    @Resource
    DataSource dataSource;
    @Test
    public void contextLoads() throws SQLException {
        Connection connection = dataSource.getConnection();
        PreparedStatement prepareStatement = connection
                .prepareStatement("select * from user where id='1'");
        ResultSet resultSet = prepareStatement.executeQuery();
        while (resultSet.next()) {
            String cityName = resultSet.getString("user_name");
            System.out.println(cityName);
        }
    }

}

相关文章

网友评论

    本文标题:【框架集成5】druid数据连接池集成

    本文链接:https://www.haomeiwen.com/subject/bykfwctx.html