当前位置: 首页 > 工具软件 > jOOQ > 使用案例 >

jooq自定义GeneratorStrategy报错ClassNotFoundException

阎祖鹤
2023-12-01

问题描述

提示:这里描述项目中遇到的问题:

gradle(kotlin项目)

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jooq.codegen.GenerationTool
import org.jooq.meta.jaxb.*

plugins {
    id("org.springframework.boot") version "2.7.1"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"

}

group = "com.nova"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.jooq:jooq-codegen:3.14.7")
        classpath("mysql:mysql-connector-java:8.0.29")
    }
}


dependencies {

    implementation("org.springframework.boot:spring-boot-starter-jooq")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    runtimeOnly("mysql:mysql-connector-java:8.0.29")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation("org.jooq:jooq:3.14.7")
    implementation("org.jooq:jooq-meta:3.14.7")
    implementation("org.jooq:jooq-codegen:3.14.7")

}

tasks.register("JOOQ_Code_Generate") {
    doLast{
        val config: org.jooq.meta.jaxb.Configuration = Configuration()
            .withJdbc(Jdbc()
                .withDriver("com.mysql.cj.jdbc.Driver")
                .withUrl("jdbc:mysql://127.0.0.1:3306/learn-jooq?characterEncoding=UTF-8&serverTimezone=GMT")
                .withUsername("root")
                .withPassword("root"))
            .withGenerator(Generator()
                    //kotlin代码生成器,默认是java
                .withName("org.jooq.codegen.KotlinGenerator")
                .withGenerate(Generate()
                    .withComments(true) // 注释 √
                    .withCommentsOnCatalogs(true)
                    .withRelations(true)
                    .withImmutablePojos(false) // if true, cannot use 'into()' method
                    .withInterfaces(true)
                    .withDaos(true))
                .withDatabase(Database()
                    .withName("org.jooq.meta.mysql.MySQLDatabase")
                    .withIncludes("s.*")
                    .withInputSchema("learn-jooq")
                )
                .withTarget(org.jooq.meta.jaxb.Target()
                    .withPackageName("com.nova.novademo.codegen")
                    .withDirectory("src/main/kotlin"))
                .withStrategy(Strategy() //自定义生成策略,报错
                    .withName("com.nova.novademo.CustomGeneratorStrategy")
//                    .withName("org.jooq.codegen.DefaultGeneratorStrategy")
                )
            )
        GenerationTool.generate(config)
    }
}


tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

maven(java项目)

<?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.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lzy</groupId>
    <artifactId>JOOQ</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JOOQ</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta</artifactId>
            <version>3.14.15</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen</artifactId>
            <version>3.14.15</version>
        </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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>


            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>


            <!-- jooq code generate plugin-->
            <plugin>

                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>${jooq.version}</version>
                
                <configuration>
                    <jdbc>
                        <driver>com.mysql.cj.jdbc.Driver</driver>
                        <url>jdbc:mysql://127.0.0.1:3306/learn-jooq?serverTimezone=GMT%2B8</url>
                        <user>root</user>
                        <password>root</password>
                    </jdbc>
                    <generator>
                        <strategy>
                            <name>com.lzy.jooq.CustomGeneratorStrategy</name>
                        </strategy>
                        <generate>
                            <pojos>true</pojos>
                            <daos>true</daos>
                        </generate>
                        <database>
                            <includes>s.*</includes>
                            <inputSchema>learn-jooq</inputSchema>
                        </database>
                        <target>
                            <packageName>com.lzy.jooq.codegen</packageName>
                            <directory>/src/main/java</directory>
                        </target>
                    </generator>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>



原因分析:

在指定GeneratorStrategy为自定义的CustomGeneratorStrategy后会报错ClassNotFoundException


解决方案:

可以先编译 或 run,只要先在target文件夹下生成对应的 .class文件就可。

 类似资料: