JOOQ快速上手(基於springboot 和 postgresql)

来源:https://www.cnblogs.com/darrenqiao/archive/2018/06/03/9127743.html
-Advertisement-
Play Games

JOOQ快速上手(基於springboot 和 postgresql) ...


是什麼

  • 全稱Java Object Oriented Querying,基於java開發出來的工具包,主要用於訪問關係型資料庫。

為什麼用

  • Hibernate對SQL的操作太抽象
  • JDBC使用太過繁瑣
  • JOOQ希望在上面兩者中找到一個平衡。

基本過程

  • 準備資料庫中的表以及數據
  • 使用JOOQ生成表結構代碼文件
  • 將代碼文件載入到項目中調用

怎麼用

  •  在postgresql中準備數據
    • database 為 report
    • schema 為 site_issue
    • table 為 issue_detail
 1 CREATE TABLE site_issue.issue_detail
 2 (
 3 site_id integer,
 4 issue_key text COLLATE pg_catalog."default",
 5 alert_type text COLLATE pg_catalog."default",
 6 "alert_resolution " text COLLATE pg_catalog."default",
 7 "duration_start " date,
 8 "duration_end " date,
 9 org_id integer
10 )
    • 插入表數據
INSERT INTO site_issue.issue_detail(
site_id, issue_key, alert_type, "alert_resolution ", "duration_start ", "duration_end ", org_id)
VALUES (12,"23","error","asd",now(),now(),2323);
  • https://start.spring.io/定製並下載Maven工程

  • 從spring tool suite 載入report工程
  • 配置pom.xml
<profiles>
    <profile>
        <id>postgresql</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jooq</groupId>
                    <artifactId>jooq-codegen-maven</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.postgresql</groupId>
                            <artifactId>postgresql</artifactId>
                            <version>42.2.2</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <jdbc>
                            <driver>org.postgresql.Driver</driver>
                            <url>jdbc:postgresql://localhost:5432/report</url>
                            <user>postgres</user>
                            <password>citrix</password>
                        </jdbc>
                        <generator>
                            <name>org.jooq.util.DefaultGenerator</name>
                            <database>
                                <name>org.jooq.util.postgres.PostgresDatabase</name>
                                <includes>.*</includes>
                                <excludes />
                                <inputSchema>site_issue</inputSchema>
                            </database>
                            <target>
                                <packageName>com.citrix.nanjing.report.jooq</packageName>
                                <directory>gensrc/main/java</directory>
                            </target>
                        </generator>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
  • 進入到代碼根目錄下執行 mvn clean install -P postgresql 執行完成之後你就可以看到gensrc/main/java 下麵有對應的表結構代碼 
  • 再配置pom.xml將代碼加入到工程中
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>gensrc/main/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • 右鍵項目選擇Maven–>update projects
  • 代碼中調用表結構數據
package priv.darrenqiao.nanjing.report.controller;

import java.sql.Connection;
import java.sql.DriverManager;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.darrenqiao.nanjing.report.jooq.tables.*;

@RestController
@RequestMapping("/screport/")
public class ReportController {

    @RequestMapping("test")
    public String testdada() {
        String userName = "postgres";
        String password = "citrix";
        String url = "jdbc:postgresql://localhost:5432/report";

        // Connection is the only JDBC resource that we need
        // PreparedStatement and ResultSet are handled by jOOQ, internally
        try (Connection conn = DriverManager.getConnection(url, userName, password)) {
            // ...
            DSLContext create = DSL.using(conn, SQLDialect.POSTGRES);
            Result<Record> result = create.select().from(IssueDetail.ISSUE_DETAIL).fetch();
            String re = null;
            for (Record r : result) {
                re = r.getValue(IssueDetail.ISSUE_DETAIL.ISSUE_KEY);
            }
            return re;
        } 

        // For the sake of this tutorial, let's keep exception handling simple
        catch (Exception e) {
            e.printStackTrace();
        }
        return "test";
    }
}
  • 啟動 訪問 http://localhost:8080/screport/test 就能看到 23

 

問題記錄

  • Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.  
    • 修改註解 為 @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
  • Unable to load the mojo 'resources'
    • 右鍵菜單,Maven–>Update Projects.問題消失

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Mit6.824 是我在學習一些分散式系統方面的知識的時候偶然看到的,然後就開始嘗試跟課。不得不說,國外的課程難度是真的大,一周的時間居然要學一門 Go 語言,然後還要讀論文,進而做MapReduce 實驗。 由於 MR(MapReduce) 框架需要建立在 DFS(Distributed... ...
  • 開始前我們必須先認識絕對路徑與相對路徑 絕對路徑是從盤符開始的路徑 ;例如:/etc/sysconfig/network (從根直接指到network) 相對路徑是從當前自己所在位置開始的路徑;例如我當前不在/(根)之下,而在etc這個文件夾里。那我要去sysconfig這個文件。只需要cd sys ...
  • i.MXRT沒有內部FLASH,需要外接FLASH存儲器以存儲image。眾所周知,FLASH從結構上分為NOR和NAND,i.MXRT啟動同時支持這兩種FLASH,NOR FLASH可以實現XIP,NAND FLASH不可以XIP,為了相容所有FLASH,在設計i.MXRT bootable im... ...
  • 作為一名合格的運維人員,要能很好地瞭解Linux伺服器,要能熟練查看Linux系統的運行狀態。以下是常用到的Linux系統狀態檢測命令。 1. ifconfig:用於獲取網卡配置與網路狀態等信息。通常我們直接輸入 ifconfig 並回車,就會看到想要的關於本機網路配置的信息。 2. uname:用 ...
  • 在ubuntu系統中安裝微信小程式開發工具之前,先要安裝wine與git 一、安裝wine 1.如果您的系統是64位,啟用32位架構(如果您還沒有) sudo dpkg --add-architecture i386 2.添加倉庫 wget -nc https://dl.winehq.org/win ...
  • 由於操作系統實驗的需要,做了這些內容。代碼基於Ubuntu操作系統環境下運行和測試。 ...
  • 基礎知識 橋接和僅主機模式的區別 橋接模式 在橋接模式下,相當於VM虛擬出了一個交換機,然後交換機也連接在物理的連接上,這時,虛擬主機都可以獲取到和物理主機在同一個網段上的地址。所以可以通過橋接模式,來連接同一個區域網內的虛擬主機。比如你可以連接你的小伙伴的虛擬的主機,如果他也是橋接在當前的區域網中 ...
  • Linux相關的知識:https://www.cnblogs.com/dunitian/p/4822808.html#linux 上次有人無意間看見我桌面,然後問微信怎麼裝的? 其實很簡單,有人問,那就說下: 其實這個是一個開源軟體,當然了是基於web版的微信。Ubuntu軟體市場也是有的,所以安全 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...