JOOQ 入門(二)--Srpingboot + Maven + JOOQ 配置


序言

開發工具IDEA,從新建Springboot項目開始,介紹Maven plugin 配置JOOQ自動生成代碼。

ps:只要是Maven項目就可以,用什么開發工具都無關。因為比較習慣使用IDEA,所以文章中的項目是在IDEA中創建的。

新建項目

第一步:

菜單順序:File->New->Project->Spring Initailizr 選擇完之后點擊右下角的Next按鈕開始下一步

如下圖:

Project SDK 是指項目使用的JDK版本,如果和本地安裝的不一樣可以點擊后面的New設置。

Choose Initializr Service URL 是指定從哪一個網址下載項目。

這里有一點補充的就是:我們看到默認選中的Default是指向https://start.spring.io

意思就是當我們創建完之后,會從上面這個網址去下載配置好的項目。(如果不用IDEA的話,我們也可以直接訪問這個網址去創建Springboot的項目)

第二步:

這一步開始先設置Group和Artifact(項目名) ,這兩個設置會直接確定項目的包名。

因為最終是一個web項目,所以打包方式選擇War(至於jar包和war包有的區別不知道的可以自己去百度)。

點擊Next

然后選擇創建項目需要用到的技術,選擇完成之后系統會在pom.xml自動幫我們導入相應的maven依賴。

Developer Tools:

Web:

SQL:

這里選擇了mysql、flyway、jooq三個

用mysql數據庫要選這個如果使用其他的數據庫選擇對應的就可以,會自動引入數據庫連接java的依賴。

Flyway則是數據庫版本管理器,后續會用到,所以這里先引入,如果不想引入也可以先不選。

選擇完成之后點擊Next。

等待創建完成之后的目錄結構,如下圖:

可以看到是一個標准的maven項目結構,main下面包含java和resources

其中java是存放java代碼

resources存放資源文件,例如配置文件、前端代碼等等。

resources文件夾下面的db.migration存放Flyway要用的數據庫腳本,暫時不用管;static和templates這兩個文件夾可以刪掉,因為在這次的例子里面用不上。

接下來是自動生成的pom.xml ,看看Springboot為我們自動加了哪些依賴。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bkn</groupId>
    <artifactId>breakingnews</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>breakingnews</name>
    <description>a news web application</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

     <!-- jooq核心依賴 -->  <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jooq</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

項目創建完成之后,開始看看在Maven項目中如何配置Jooq自動生成代碼。

配置plugin

 開始配置插件之前需要先引入兩個需要的依賴

 1 <!--jooq 需要的meta和生成代碼的codegen包
 2             這里注意查看spring-boot-starter-jooq 中的jooq是什么版本-->
 3         <dependency>
 4             <groupId>org.jooq</groupId>
 5             <artifactId>jooq-meta</artifactId>
 6             <version>3.12.4</version>
 7         </dependency>
 8         <dependency>
 9             <groupId>org.jooq</groupId>
10             <artifactId>jooq-codegen</artifactId>
11             <version>3.12.4</version>
12         </dependency>

接下來在就是配置插件,在pom.xml中添加如下配置,注意找到對應的標簽,

需要把下面的jooq那一個plugin中的內容復制到你自己項目的plugins下面

 1 <build>
 2         <plugins>
 3             <plugin>
 4                 <groupId>org.springframework.boot</groupId>
 5                 <artifactId>spring-boot-maven-plugin</artifactId>
 6             </plugin>
 7             <!-- jooq code generate plugin-->
 8             <plugin>
 9                 <!-- Use org.jooq for the Open Source Edition
10                 org.jooq.pro-java-8 for commercial editions with Java 8 support,
11                 org.jooq.trial for the free trial edition
12                 Note: Only the Open Source Edition is hosted on Maven Central.
13                 Import the others manually from your distribution -->
14                 <groupId>org.jooq</groupId>
15                 <artifactId>jooq-codegen-maven</artifactId>
16                 <version>${org.jooq.version}</version>
17                 <!-- The jOOQ code generation plugin is also executed in the generate-sources phase, prior to compilation -->
18                 <executions>
19                     <execution>
20                         <phase>generate-sources</phase>
21                         <goals>
22                             <goal>generate</goal>
23                         </goals>
24                     </execution>
25                 </executions>
26                 <!-- This is a minimal working configuration. See the manual's section about the code generator for more details -->
27                 <configuration>
28                     <!-- 這里使用配置文件 -->
29                     <configurationFile>src/main/resources/jooqConfig.xml</configurationFile>
30                 </configuration>
31             </plugin>
32         </plugins>
33     </build>

關於插件中的生成代碼配置,也就是<configeration>標簽里面的內容,官網使用的是直接在該標簽下配置。(官方怎么配置去官網查一下)

我這里使用的是使用配置文件的方式,兩種方式都可以,看個人習慣。

這里我們需要新建一個jooqConfig.xml配置文件,放在resources文件夾下面,用來存放jooq生成代碼的相關配置。

jooqConfig.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.12.0.xsd">
    <!-- Configure the database connection here -->
    <jdbc>
        <driver>com.mysql.cj.jdbc.Driver</driver>
        <url>jdbc:mysql://localhost:3306/bknews?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8</url>
        <user>root</user>
        <password>123456</password>
    </jdbc>

    <generator>
        <!-- The default code generator. You can override this one, to generate your own code style.
             Supported generators:
             - org.jooq.codegen.JavaGenerator
             - org.jooq.codegen.ScalaGenerator
             Defaults to org.jooq.codegen.JavaGenerator -->
        <name>org.jooq.codegen.JavaGenerator</name>

        <database>
            <!-- The database type. The format here is:
                 org.jooq.meta.[database].[database]Database -->
            <name>org.jooq.meta.mysql.MySQLDatabase</name>

            <!-- The database schema (or in the absence of schema support, in your RDBMS this
                 can be the owner, user, database name) to be generated -->
            <!-- 數據庫名 -->
            <inputSchema>bknews</inputSchema>

            <!-- All elements that are generated from your schema
                 (A Java regular expression. Use the pipe to separate several expressions)
                 Watch out for case-sensitivity. Depending on your database, this might be important! -->
            <!-- 包含哪些表 -->
            <includes>.*</includes>

            <!-- All elements that are excluded from your schema
                 (A Java regular expression. Use the pipe to separate several expressions).
                 Excludes match before includes, i.e. excludes have a higher priority -->
            <!-- 排除哪些表,這里支持正則表達式 ,多個條件可以用 | 連接符連接-->
            <!-- 例如:TEST | OTHERS 生成代碼時就不會把叫做TEST和OTHERS的表包括進去了-->
            <excludes></excludes>
        </database>

        <target>
            <!-- The destination package of your generated classes (within the destination directory) -->
            <!-- 生成的代碼存放的包名 -->
            <packageName>com.bkn.breakingnews.model</packageName>

            <!-- The destination directory of your generated classes. Using Maven directory layout here -->
            <!-- 存放的路徑 -->
            <directory>src/main/java/</directory>
        </target>
    </generator>
</configuration>

 

配置文件中需要注意有兩點:

  1. 數據庫URL中不帶時區參數可能會報錯,出現serverTimeZone的錯誤時,百度搜索一下就知道原因了。

  2. 生成代碼的路徑如果在自己設置的位置的話自己多試幾次就知道jooq是怎么存放的了。

執行plugin

配置完成之后就是執行生成代碼操作,打開Mave的操作欄,IDEA如果沒有自己重新設置過布局的話,Maven是在右邊。

找到Plugins->jooq-codegen->jooq-codegen:generate 然后點擊右鍵,選擇Run Maven Build

執行之后可以看到控制台的Maven執行日志,

執行完成之后,查看項目目錄。

 

OK,代碼生成成功之后來測試一下。

在test文件夾中新建類JooqTest,類中包含一個test()的方法,然后右鍵選擇Run test 或者Debug test

package com.bkn.breakingnews;

import com.bkn.breakingnews.model.tables.BkUser;
import org.jooq.DSLContext;
import org.jooq.Result;
import org.jooq.impl.DSL;
import org.junit.jupiter.api.Test;

public class JooqTest {

    @Test
    void test(){
        DSLContext create = DSL.using("jdbc:mysql://localhost:3306/bknews?serverTimezone=UTC",
                "root",
                "123456");
        Result result = create.selectFrom(BkUser.BK_USER) //BkUser是生成的表
                                .orderBy(1)
                                .fetch();
        System.out.println(result);
    }
}

執行結果:

 

總結

  a.配置過程中生成路徑和數據庫的URL出現問題時日志中錯誤信息還算比較完善,耐心看日志解決問題。

  b.實際的開發中jooq常用的語法以及更加詳細的配置會在后續的文章中來寫。

 


免責聲明!

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



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