SpringBoot3集成PostgreSQL的詳細過程

PostgreSQL是一個功能強大的開源數(shù)據(jù)庫系統(tǒng),具有可靠性、穩(wěn)定性、數(shù)據(jù)一致性等特點,且可以運行在所有主流操作系統(tǒng)上,包括Linux、Unix、Windows等。
一、簡介
PostgreSQL是一個功能強大的開源數(shù)據(jù)庫系統(tǒng),具有可靠性、穩(wěn)定性、數(shù)據(jù)一致性等特點,且可以運行在所有主流操作系統(tǒng)上,包括Linux、Unix、Windows等。
通過官方文檔可以找到大量描述如何安裝和使用PostgreSQL的信息。
環(huán)境搭建,基于Centos7部署的PostgreSQL-14版本,官方文檔中提供yum安裝的方式,配置的話可以參考源碼倉庫中的其他版本「見文尾」,這里不贅述。

# 1、RPM倉庫 sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # 2、安裝PostgreSQL sudo yum install -y postgresql14-server # 3、初始化選項 sudo /usr/pgsql-14/bin/postgresql-14-setup initdb sudo systemctl enable postgresql-14 sudo systemctl start postgresql-14 # 4、查看版本 psql --version psql (PostgreSQL) 14.11
二、工程搭建
1、工程結(jié)構(gòu)

2、依賴管理
Druid連接池使用的是1.2.18版本;使用mybatis-plus組件的3.5.3.1版本;PostgreSQL本地環(huán)境是14.11版本,這里依賴包使用42.6.2版本;
<!-- Postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<!-- Druid組件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>${druid-spring-boot.version}</version>
</dependency>
<!-- MybatisPlus組件 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>三、PostgreSQL配置
1、數(shù)據(jù)庫配置
有關(guān)于Druid連接池的可配置參數(shù)還有很多,可以參考源碼中的描述或者官方案例,此處只提供部分常見的參數(shù)配置;
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 數(shù)據(jù)庫
url: jdbc:postgresql://127.0.0.1:5432/pg-data-14
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
# 連接池-初始化大小
initial-size: 10
# 連接池-最大連接數(shù)
max-active: 100
# 最大等待時間
max-wait: 60000
# 連接池-最小空閑數(shù)
min-idle: 10
# 檢測空閑連接
test-while-idle: true
# 最小空閑時間
min-evictable-idle-time-millis: 3000002、逆向工程類
逆向工程新版本的API語法和之前有變化,但是整體的邏輯還是差不多。其它的SQL腳本和基礎(chǔ)案例,以及相關(guān)單元測試不再贅述,參考源碼倉庫即可。
public class GeneratorMybatisPlus {
private static final String jdbcUrl = "數(shù)據(jù)庫地址";
private static final String outDir = "存放路徑";
public static void main(String[] args) {
// 數(shù)據(jù)源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder
(jdbcUrl,"postgres","postgres")
.build();
// 代碼生成器
AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);
// 全局配置
GlobalConfig globalConfig = new GlobalConfig.Builder()
.outputDir(outDir).disableOpenDir().author("知了一笑") // .enableSwagger()
.build();
// 分包配置
PackageConfig packageConfig = new PackageConfig.Builder()
.parent("com.boot.pgsql.generator").controller("controller")
.service("dao").serviceImpl("dao.impl").mapper("mapper").entity("entity")
.build();
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig.Builder()
.addInclude("user_info","sys_user")
.addTablePrefix("")
.entityBuilder().enableLombok()
.naming(NamingStrategy.underline_to_camel)
.columnNaming(NamingStrategy.underline_to_camel)
.controllerBuilder().formatFileName("%sController")
.entityBuilder().formatFileName("%s")
.serviceBuilder().formatServiceFileName("%sDao").formatServiceImplFileName("%sDaoImpl")
.mapperBuilder().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper")
.build();
autoGenerator.global(globalConfig);
autoGenerator.packageInfo(packageConfig);
autoGenerator.strategy(strategyConfig);
// 執(zhí)行
autoGenerator.execute();
}
}四、參考源碼
文檔倉庫:https://gitee.com/cicadasmile/butte-java-note
源碼倉庫:https://gitee.com/cicadasmile/butte-spring-parent
PostgreSQL配置參考:https://gitee.com/cicadasmile/butte-java-note/blob/master/doc/database/postgresql/P01、PostgreSQL環(huán)境搭建.md
Mybatis三種逆向工程:
https://gitee.com/cicadasmile/butte-java-note/blob/master/doc/frame/tool/T01、Mybatis三種逆向工程.md
Gitee主頁: https://gitee.com/cicadasmile/butte-java-note
到此這篇關(guān)于SpringBoot3集成PostgreSQL的文章就介紹到這了,更多相關(guān)SpringBoot3集成PostgreSQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實現(xiàn)
- SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
- SpringBoot整合PostgreSQL的示例代碼
- SpringBoot項目配置postgresql數(shù)據(jù)庫完整步驟(配置多數(shù)據(jù)源)
- springboot+springJdbc+postgresql 實現(xiàn)多數(shù)據(jù)源的配置
- SpringBoot連接使用PostgreSql數(shù)據(jù)庫的方法
- Springboot中MyBatisplus使用IPage和Page分頁的實例代碼
- SpringBoot+MybatisPlus+代碼生成器整合示例
- springboot集成mybatisplus實例詳解
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
相關(guān)文章
如何將excel表格數(shù)據(jù)導入postgresql數(shù)據(jù)庫
這篇文章主要介紹了如何將excel表格數(shù)據(jù)導入postgresql數(shù)據(jù)庫,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
postgreSql分組統(tǒng)計數(shù)據(jù)的實現(xiàn)代碼
這篇文章給大家介紹postgreSql的監(jiān)控記錄表里多條不同時間的數(shù)據(jù),只取最新的數(shù)據(jù),并分組統(tǒng)計,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12
Postgresql數(shù)據(jù)庫密碼忘記的詳細解決方法
在使用PostgreSQL數(shù)據(jù)庫時,忘記數(shù)據(jù)庫密碼可能會影響到正常的開發(fā)和維護工作,這篇文章主要介紹了Postgresql數(shù)據(jù)庫密碼忘記的詳細解決方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-06-06

