最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot整合druid的完整流程記錄

 更新時(shí)間:2026年02月10日 09:02:43   作者:改了一個(gè)昵稱  
Java程序很大一部分要操作數(shù)據(jù)庫,為了提高性能操作數(shù)據(jù)庫的時(shí)候,又不得不使用數(shù)據(jù)庫連接池,Druid是阿里巴巴開源平臺(tái)上一個(gè)數(shù)據(jù)庫連接池實(shí)現(xiàn),結(jié)合了DBCP等DB池的優(yōu)點(diǎn),同時(shí)加入了日志監(jiān)控,這篇文章主要介紹了springboot整合druid的相關(guān)資料,需要的朋友可以參考下

pom.xml

druid版本問題

druid 的依賴版本,盡量選擇 1.2.20 及 以上,不然會(huì)報(bào)錯(cuò)

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-3-starter</artifactId>
	<version>1.2.20</version>
	
	// 不要用 1.2.18 哦,會(huì)報(bào)錯(cuò)
	<version>1.2.18</version>
</dependency>

通過源碼分析,druid-spring-boot-3-starter 的 1.2.18 版本,

雖然,適配了 SpringBoot3,

但是,缺少自動(dòng)裝配的配置文件,

需要手動(dòng)在 resources目錄下創(chuàng)建

META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,

文件內(nèi)容如下:

com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure

項(xiàng)目的依賴

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
    </parent>

    <groupId>com.atguigu</groupId>
    <artifactId>boot-druid</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!--  web開發(fā)的場景啟動(dòng)器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 數(shù)據(jù)庫相關(guān)配置啟動(dòng)器 jdbctemplate 事務(wù)相關(guān)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- druid啟動(dòng)器的依賴  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-3-starter</artifactId>
            <version>1.2.20</version>
        </dependency>

        <!-- 驅(qū)動(dòng)類-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>

    </dependencies>

</project>

application.yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource # 使用druid連接池
    druid:
      url: jdbc:mysql:///mybatis-example
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
      
      # 初始化時(shí)建立物理連接的個(gè)數(shù)
      initial-size: 5
      
      # 連接池的最小空閑數(shù)量
      min-idle: 5
      
      # 連接池最大連接數(shù)量
      max-active: 20
      
      # 獲取連接時(shí)最大等待時(shí)間,單位毫秒
      max-wait: 60000
      
      # 申請(qǐng)連接的時(shí)候檢測(cè),如果空閑時(shí)間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測(cè)連接是否有效。
      test-while-idle: true
      
      # 既作為檢測(cè)的間隔時(shí)間又作為testWhileIdel執(zhí)行的依據(jù)
      time-between-eviction-runs-millis: 60000
      
      # 銷毀線程時(shí)檢測(cè)當(dāng)前連接的最后活動(dòng)時(shí)間和當(dāng)前時(shí)間差大于該值時(shí),關(guān)閉當(dāng)前連接(配置連接在池中的最小生存時(shí)間)
      min-evictable-idle-time-millis: 30000
      
      # 用來檢測(cè)數(shù)據(jù)庫連接是否有效的sql 必須是一個(gè)查詢語句(oracle中為 select 1 from dual)
      validation-query: select 1
      
      # 申請(qǐng)連接時(shí)會(huì)執(zhí)行validationQuery檢測(cè)連接是否有效,開啟會(huì)降低性能,默認(rèn)為true
      test-on-borrow: false
      
      # 歸還連接時(shí)會(huì)執(zhí)行validationQuery檢測(cè)連接是否有效,開啟會(huì)降低性能,默認(rèn)為true
      test-on-return: false
      
      # 是否緩存preparedStatement, 也就是PSCache,PSCache對(duì)支持游標(biāo)的數(shù)據(jù)庫性能提升巨大,比如說oracle,在mysql下建議關(guān)閉。
      pool-prepared-statements: false
      
      # 要啟用PSCache,必須配置大于0,當(dāng)大于0時(shí),poolPreparedStatements自動(dòng)觸發(fā)修改為true。在Druid中,不會(huì)存在Oracle下PSCache占用內(nèi)存過多的問題,可以把這個(gè)數(shù)值配置大一些,比如說100
      max-pool-prepared-statement-per-connection-size: -1
      
      # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
      use-global-data-source-stat: true
      

實(shí)體類 User

package com.atguigu.pojo;

import lombok.Data;

@Data
public class User {
    private int empId;
    private String empName;
    private double empSalary;
}

實(shí)體類 User 對(duì)應(yīng)的 controller

package com.atguigu.controller;

import com.atguigu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("findAll")
    public List<User> findAll() {
        String sql = "select * from t_emp";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        return list;
    }
}

spboot 的啟動(dòng)程序

package com.atguigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

總結(jié) 

到此這篇關(guān)于springboot整合druid的文章就介紹到這了,更多相關(guān)springboot整合druid內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • iOS多線程介紹

    iOS多線程介紹

    這篇文章主要介紹了iOS多線程的相關(guān)知識(shí),涉及到對(duì)進(jìn)程,線程等方面的知識(shí)講解,本文非常具有參考價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05
  • Maven2環(huán)境安裝與準(zhǔn)備工作詳解

    Maven2環(huán)境安裝與準(zhǔn)備工作詳解

    這篇文章主要為大家詳細(xì)介紹了Maven2環(huán)境安裝與準(zhǔn)備工作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • JDK 14的新特性:文本塊Text Blocks的使用

    JDK 14的新特性:文本塊Text Blocks的使用

    這篇文章主要介紹了JDK 14的新特性:文本塊Text Blocks的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java 中實(shí)現(xiàn)異步的多種方式

    Java 中實(shí)現(xiàn)異步的多種方式

    文章介紹了Java中實(shí)現(xiàn)異步處理的幾種常見方式,每種方式都有其特點(diǎn)和適用場景,通過選擇合適的異步處理方式,可以提高程序的性能和可維護(hù)性,感興趣的朋友一起看看吧
    2025-03-03
  • spring為類的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法

    spring為類的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于spring為類的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法,有需要的朋友們可以參考下。
    2019-10-10
  • 關(guān)于@Data和@Builder注解解析

    關(guān)于@Data和@Builder注解解析

    在使用Lombok庫時(shí),@Data和@Builder注解混用可能會(huì)導(dǎo)致編譯失敗,解決方法包括添加@NoArgsConstructor和@AllArgsConstructor注解,或者重寫無參構(gòu)造器并注解@Tolerate,這是因?yàn)锧Data自動(dòng)生成的構(gòu)造器與@Builder的構(gòu)造模式存在沖突
    2024-10-10
  • 一文詳解如何使用Java獲取PDF頁面信息

    一文詳解如何使用Java獲取PDF頁面信息

    了解 PDF 頁面屬性是我們?cè)谔幚砦臋n、內(nèi)容提取、打印設(shè)置或頁面重組等任務(wù)時(shí)不可或缺的一環(huán),下面我們就來看看如何使用Java語言獲取這些信息吧
    2025-07-07
  • 詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實(shí)用

    詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實(shí)用

    這篇文章主要介紹了詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問題

    詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問題

    這篇文章主要介紹了詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問題,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • java冒泡排序和選擇排序示例

    java冒泡排序和選擇排序示例

    這篇文章主要介紹了java冒泡排序和選擇排序示例,需要的朋友可以參考下
    2014-05-05

最新評(píng)論

正宁县| 城步| 丹东市| 紫阳县| 柘城县| 耒阳市| 甘肃省| 济南市| 德保县| 芮城县| 宜章县| 易门县| 永仁县| 徐州市| 乐山市| 余干县| 丘北县| 津市市| 合水县| 惠水县| 鄯善县| 木里| 贡嘎县| 昭觉县| 东方市| 七台河市| 博爱县| 龙州县| 东乡族自治县| 通城县| 海晏县| 剑阁县| 米易县| 禹城市| 惠州市| 元江| 湖南省| 利辛县| 祥云县| 兖州市| 土默特左旗|