SpringBoot3集成PostgreSQL的詳細(xì)過(guò)程
PostgreSQL是一個(gè)功能強(qiáng)大的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng),具有可靠性、穩(wěn)定性、數(shù)據(jù)一致性等特點(diǎn),且可以運(yùn)行在所有主流操作系統(tǒng)上,包括Linux、Unix、Windows等。
一、簡(jiǎn)介
PostgreSQL是一個(gè)功能強(qiáng)大的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng),具有可靠性、穩(wěn)定性、數(shù)據(jù)一致性等特點(diǎn),且可以運(yùn)行在所有主流操作系統(tǒng)上,包括Linux、Unix、Windows等。
通過(guò)官方文檔可以找到大量描述如何安裝和使用PostgreSQL的信息。
環(huán)境搭建,基于Centos7
部署的PostgreSQL-14
版本,官方文檔中提供yum
安裝的方式,配置的話可以參考源碼倉(cāng)庫(kù)中的其他版本「見(jiàn)文尾」,這里不贅述。
# 1、RPM倉(cāng)庫(kù) 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、初始化選項(xiàng) 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ù)庫(kù)配置
有關(guān)于Druid
連接池的可配置參數(shù)還有很多,可以參考源碼中的描述或者官方案例,此處只提供部分常見(jiàn)的參數(shù)配置;
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: # 數(shù)據(jù)庫(kù) 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 # 最大等待時(shí)間 max-wait: 60000 # 連接池-最小空閑數(shù) min-idle: 10 # 檢測(cè)空閑連接 test-while-idle: true # 最小空閑時(shí)間 min-evictable-idle-time-millis: 300000
2、逆向工程類
逆向工程新版本的API語(yǔ)法和之前有變化,但是整體的邏輯還是差不多。其它的SQL腳本和基礎(chǔ)案例,以及相關(guān)單元測(cè)試不再贅述,參考源碼倉(cāng)庫(kù)即可。
public class GeneratorMybatisPlus { private static final String jdbcUrl = "數(shù)據(jù)庫(kù)地址"; 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(); } }
四、參考源碼
文檔倉(cāng)庫(kù):https://gitee.com/cicadasmile/butte-java-note
源碼倉(cāng)庫(kù):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主頁(yè): https://gitee.com/cicadasmile/butte-java-note
到此這篇關(guān)于SpringBoot3集成PostgreSQL的文章就介紹到這了,更多相關(guān)SpringBoot3集成PostgreSQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn)
- SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
- SpringBoot整合PostgreSQL的示例代碼
- SpringBoot項(xiàng)目配置postgresql數(shù)據(jù)庫(kù)完整步驟(配置多數(shù)據(jù)源)
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- SpringBoot連接使用PostgreSql數(shù)據(jù)庫(kù)的方法
- Springboot中MyBatisplus使用IPage和Page分頁(yè)的實(shí)例代碼
- SpringBoot+MybatisPlus+代碼生成器整合示例
- springboot集成mybatisplus實(shí)例詳解
- SpringBoot連接PostgreSQL+MybatisPlus入門案例(代碼詳解)
相關(guān)文章
postgresql 實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出
這篇文章主要介紹了postgresql 實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12Postgresql創(chuàng)建新增、刪除與修改觸發(fā)器的方法
這篇文章主要介紹了Postgresql創(chuàng)建新增、刪除與修改觸發(fā)器的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12玩轉(zhuǎn)PostgreSQL之30個(gè)實(shí)用SQL語(yǔ)句
本文主要整理總結(jié)了30個(gè)實(shí)用SQL,方便大家可以高效利用PostgreSQL,需要的朋友可以參考下2022-11-11啟動(dòng)PostgreSQL服務(wù)器 并用pgAdmin連接操作
這篇文章主要介紹了啟動(dòng)PostgreSQL服務(wù)器 并用pgAdmin連接操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01PostgreSQL有效地處理數(shù)據(jù)的加密和解密的常見(jiàn)方法
在信息化建設(shè)和等保建設(shè)中,都要求實(shí)現(xiàn)對(duì)用戶數(shù)據(jù)的隱私保護(hù),也就是我們常說(shuō)的脫敏,那么在?PostgreSQL?數(shù)據(jù)庫(kù)中有沒(méi)有這樣的方法或者策略可以實(shí)現(xiàn)呢,本文小編將給大家介紹一下PostgreSQL有效地處理數(shù)據(jù)的加密和解密的常見(jiàn)方法,需要的朋友可以參考下2025-03-03PostgreSQL如何殺死被鎖死的進(jìn)程問(wèn)題
文章總結(jié):文章主要介紹了如何使用PostgreSQL提供的pg_cancel_backend()和pg_terminate_backend()函數(shù)來(lái)解決數(shù)據(jù)庫(kù)表被鎖住的問(wèn)題,以及如何查詢哪些表、哪些進(jìn)程被鎖住了2024-12-12詳解PostgreSQL中實(shí)現(xiàn)數(shù)據(jù)透視表的三種方法
數(shù)據(jù)透視表(Pivot Table)是進(jìn)行數(shù)據(jù)匯總、分析、瀏覽和展示的強(qiáng)大工具,可以幫助我們了解數(shù)據(jù)中的對(duì)比情況、模式和趨勢(shì),是數(shù)據(jù)分析師和運(yùn)營(yíng)人員必備技能之一,本給大家介紹PostgreSQL中實(shí)現(xiàn)數(shù)據(jù)透視表的三種方法,需要的朋友可以參考下2024-04-04postgresql修改完端口后直接psql連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決
這篇文章主要介紹了postgresql修改完端口后直接psql連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01postgresql 實(shí)現(xiàn)多表關(guān)聯(lián)刪除
這篇文章主要介紹了postgresql 實(shí)現(xiàn)多表關(guān)聯(lián)刪除操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01