springboot整合druid的完整流程記錄

pom.xml
druid版本問(wèn)題
druid 的依賴(lài)版本,盡量選擇 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>
通過(guò)源碼分析,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)目的依賴(lài)
<?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開(kāi)發(fā)的場(chǎng)景啟動(dòng)器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 數(shù)據(jù)庫(kù)相關(guān)配置啟動(dòng)器 jdbctemplate 事務(wù)相關(guān)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid啟動(dòng)器的依賴(lài) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- 驅(qū)動(dòng)類(lèi)-->
<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
# 銷(xiāo)毀線程時(shí)檢測(cè)當(dāng)前連接的最后活動(dòng)時(shí)間和當(dāng)前時(shí)間差大于該值時(shí),關(guān)閉當(dāng)前連接(配置連接在池中的最小生存時(shí)間)
min-evictable-idle-time-millis: 30000
# 用來(lái)檢測(cè)數(shù)據(jù)庫(kù)連接是否有效的sql 必須是一個(gè)查詢(xún)語(yǔ)句(oracle中為 select 1 from dual)
validation-query: select 1
# 申請(qǐng)連接時(shí)會(huì)執(zhí)行validationQuery檢測(cè)連接是否有效,開(kāi)啟會(huì)降低性能,默認(rèn)為true
test-on-borrow: false
# 歸還連接時(shí)會(huì)執(zhí)行validationQuery檢測(cè)連接是否有效,開(kāi)啟會(huì)降低性能,默認(rèn)為true
test-on-return: false
# 是否緩存preparedStatement, 也就是PSCache,PSCache對(duì)支持游標(biāo)的數(shù)據(jù)庫(kù)性能提升巨大,比如說(shuō)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)存過(guò)多的問(wèn)題,可以把這個(gè)數(shù)值配置大一些,比如說(shuō)100
max-pool-prepared-statement-per-connection-size: -1
# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
use-global-data-source-stat: true
實(shí)體類(lèi) User
package com.atguigu.pojo;
import lombok.Data;
@Data
public class User {
private int empId;
private String empName;
private double empSalary;
}
實(shí)體類(lèi) 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)文章
spring為類(lèi)的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于spring為類(lèi)的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法,有需要的朋友們可以參考下。2019-10-10
詳解SpringMVC和MyBatis框架開(kāi)發(fā)環(huán)境搭建和簡(jiǎn)單實(shí)用
這篇文章主要介紹了詳解SpringMVC和MyBatis框架開(kāi)發(fā)環(huán)境搭建和簡(jiǎn)單實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問(wèn)題
這篇文章主要介紹了詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問(wèn)題,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

