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() {

     //1. 指定 url 數據庫用戶名 密碼 String userName
= "postgres"; String password = "citrix"; String url = "jdbc:postgresql://localhost:5432/report"; //2. 創建連接 try (Connection conn = DriverManager.getConnection(url, userName, password)) { //3. 將連接和具體的數據庫類型綁定 DSLContext create = DSL.using(conn, SQLDialect.POSTGRES);
  
       //4. 執行查詢 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.問題消失


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM