zoukankan      html  css  js  c++  java
  • SpringBoot_整合连接池

    jdbc连接池是spring配置中的重要一环,在SpringBoot中该如何处理呢?

    答案是不需要处理,我们只要找到SpringBoot提供的启动器即可:

    在pom.xml中引入jdbc的启动器:

    <!--jdbc的启动器,默认使用HikariCP连接池-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    SpringBoot已经自动帮我们引入了一个HikariCP连接池,因此,我们只需要指定连接池参数即可:

    # 连接四大参数
    spring.datasource.url=jdbc:mysql://localhost:3306/heima
    spring.datasource.username=root
    spring.datasource.password=root
    # 可省略,SpringBoot自动推断
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    
    spring.datasource.hikari.idle-timeout=60000
    spring.datasource.hikari.maximum-pool-size=30
    spring.datasource.hikari.minimum-idle=10

    当然,如果你更喜欢Druid连接池,也可以使用Druid官方提供的启动器:

    <!-- Druid连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.6</version>
    </dependency>

    而连接信息的配置与上面是类似的,只不过在连接池特有属性上,方式略有不同:

    #初始化连接数
    spring.datasource.druid.initial-size=1
    #最小空闲连接
    spring.datasource.druid.min-idle=1
    #最大活动连接
    spring.datasource.druid.max-active=20
    #获取连接时测试是否可用
    spring.datasource.druid.test-on-borrow=true
    #监控页面启动
    spring.datasource.druid.stat-view-servlet.allow=true
    学习中,博客都是自己学习用的笔记,持续更新改正。。。
  • 相关阅读:
    ubuntu下python的错误
    Zookeeper(二) zookeeper集群搭建 与使用
    Zookeeper(一) zookeeper基础使用
    MapReduce(五) mapreduce的shuffle机制 与 Yarn
    MapReduce(四) 典型编程场景(二)
    Mysql(一) 基本操作
    MapReduce(三) 典型场景(一)
    MapReduce(二)常用三大组件
    MapReduce(一) mapreduce基础入门
    Hive(六)hive执行过程实例分析与hive优化策略
  • 原文地址:https://www.cnblogs.com/Tunan-Ki/p/11774304.html
Copyright © 2011-2022 走看看