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: 300000
2、逆向工程類
逆向工程新版本的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)文章
postgresql 實現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出
這篇文章主要介紹了postgresql 實現(xiàn)數(shù)據(jù)的導(dǎo)入導(dǎo)出,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12Postgresql創(chuàng)建新增、刪除與修改觸發(fā)器的方法
這篇文章主要介紹了Postgresql創(chuàng)建新增、刪除與修改觸發(fā)器的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12玩轉(zhuǎn)PostgreSQL之30個實用SQL語句
本文主要整理總結(jié)了30個實用SQL,方便大家可以高效利用PostgreSQL,需要的朋友可以參考下2022-11-11啟動PostgreSQL服務(wù)器 并用pgAdmin連接操作
這篇文章主要介紹了啟動PostgreSQL服務(wù)器 并用pgAdmin連接操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL有效地處理數(shù)據(jù)的加密和解密的常見方法
在信息化建設(shè)和等保建設(shè)中,都要求實現(xiàn)對用戶數(shù)據(jù)的隱私保護,也就是我們常說的脫敏,那么在?PostgreSQL?數(shù)據(jù)庫中有沒有這樣的方法或者策略可以實現(xiàn)呢,本文小編將給大家介紹一下PostgreSQL有效地處理數(shù)據(jù)的加密和解密的常見方法,需要的朋友可以參考下2025-03-03詳解PostgreSQL中實現(xiàn)數(shù)據(jù)透視表的三種方法
數(shù)據(jù)透視表(Pivot Table)是進行數(shù)據(jù)匯總、分析、瀏覽和展示的強大工具,可以幫助我們了解數(shù)據(jù)中的對比情況、模式和趨勢,是數(shù)據(jù)分析師和運營人員必備技能之一,本給大家介紹PostgreSQL中實現(xiàn)數(shù)據(jù)透視表的三種方法,需要的朋友可以參考下2024-04-04postgresql修改完端口后直接psql連接數(shù)據(jù)庫報錯的解決
這篇文章主要介紹了postgresql修改完端口后直接psql連接數(shù)據(jù)庫報錯的解決,具有很好的參考價值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01postgresql 實現(xiàn)多表關(guān)聯(lián)刪除
這篇文章主要介紹了postgresql 實現(xiàn)多表關(guān)聯(lián)刪除操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01