jooq从入门到精通(一)

jooq从入门到精通

    • 简介
    • 环境介绍
    • 环境准备
    • 代码生成
    • 代码测试

简介

本篇文章作为jooq的入门,搭建一个jooq的基本的测试环境,为后续深入学习jooq作准备。

jooq是一个orm层的框架,生态中的地位和mybatis等同,但是不用书写过多的sql语句,其包括了强大的api以及数据层代码生成能力。

环境介绍

sping boot 2.5.6
postgresql 10.4

环境准备

首先需要创建一个spring boot的项目,添加依赖如下:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

添加postgresql依赖和jooq依赖以及jooq代码生成器相关的依赖:

<dependency>
	<groupId>org.postgresql</groupId>
	<artifactId>postgresql</artifactId>
	<version>42.2.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jooq</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen</artifactId>
    <version>3.11.5</version>
    <exclusions>
        <exclusion>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-meta</artifactId>
    <version>3.11.5</version>
</dependency>

注意: jooq-codegen:3.11.5又依赖了jooq-meta,但是其自动下载的版本却是3.14.5,会在运行代码生成器的时候,报错缺少一个类,所以上文中手动引用指定版本的jooq-meta。

代码生成

import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Database;
import org.jooq.meta.jaxb.Generator;
import org.jooq.meta.jaxb.Jdbc;
import org.jooq.meta.jaxb.Target;
import org.junit.jupiter.api.Test;
/**
 * @author daxue0929
 * @date 2023/3/11
 */
public class GenerateCode {
    @Test
    public void generate() throws Exception {
        Configuration configuration = new Configuration()
            .withJdbc(new Jdbc()
                .withDriver("org.postgresql.Driver")
                .withUrl("jdbc:postgresql://localhost:5432/app_config?useJDBCCompliantTimezoneShift=true&serverTimezone=UTC&useSSL=false")
                .withUser("postgres")
                .withPassword("123456"))
            .withGenerator(new Generator()
                .withDatabase(new Database()
                    .withName("org.jooq.meta.postgres.PostgresDatabase")
                    .withIncludes("config")
                    .withExcludes("")
                    .withIncludePrimaryKeys(true)
                    .withInputSchema("public"))
                .withTarget(new Target()
                    .withPackageName("com.daxue.configtest.config.repository.dao")
                    .withDirectory("src/main/java")
                    .withClean(false)
                ));
        GenerationTool.generate(configuration);
    }
}

如果运行成功将会在相应的包下生成代码。其中注意的是: 因为withDirectory()指定的是相对路径,所以一定要注意测试类运行的工作目录是哪里,一定要检查Working directory参数。

jooq从入门到精通(一)_第1张图片

代码测试

到这里我们已经生成了代码,但是想要运行生成的测试代码,但是不要忘了应为我们启动的是spring boot环境,所以还需要配置spring boot连接数据库的 application.yml 如下参考:

server:
  port: 8089
spring:
  application:
    name: config-test
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/app_config?useJDBCCompliantTimezoneShift=true&serverTimezone=UTC&useSSL=false
    username: postgres
    password: 123456

同样因为我们用的是spring boot想要建立测试类,一定要注意@SpringBootTes注解的测试类,它的包名一定要和启动类,也就是@SpringBootApplication的包名一致,这样才能自动扫描到bean。
在测试类中我们可以直接属性注入DSLContext类的单例bean。

package com.daxue.configtest;

import com.daxue.configtest.config.repository.dao.tables.records.ConfigRecord;
import org.jooq.DSLContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static com.daxue.configtest.config.repository.dao.Tables.CONFIG;

/**
 * @author daxue0929
 * @date 2023/3/12
 */
@SpringBootTest
public class ConfigTestApplicationTest {
    @Autowired
    public DSLContext context;
    
    @Test
    public void context() {
        ConfigRecord configRecord = context.selectFrom(CONFIG)
            .where(CONFIG.ID.eq("1"))
            .fetchOne();
        System.out.println(configRecord.getName());
    }
}

jooq入门篇环境搭建到此结束,程序测试结果符合预期,下一篇再介绍DSLContext相关api的用法。
插一句: 公司项目中数据存取层是对DSLContext相关的api的再次封装。


❤️ 博主笔力尚浅,文中记录有疏漏之处还请流言指正,不胜感激。❤️ 谢谢大家。❤️

你可能感兴趣的:(java,数据库,mybatis,java)