简体   繁体   English

jOOQ 自定义 Pojo 和 DAO 生成

[英]jOOQ Custom Pojo & DAO Generation

Problem问题

I'm having some issues configuring mapping to custom Pojos during code generation.我在代码生成期间配置映射到自定义 Pojos 时遇到一些问题。

Question问题

I have implemented RecordMapperProvider but wondering how I register it to be used during the code generation phase, or even if that is possible?我已经实现RecordMapperProvider但想知道如何注册它以便在代码生成阶段使用它,或者即使这可能吗?

More Context更多上下文

I love the fact that Pojos & DAOs are generated but I want to define the Pojo myself without too much configuration code.我喜欢生成 Pojos 和 DAO 的事实,但我想自己定义 Pojo 而无需太多配置代码。 I am using ModelMapper to map from Type to Target:我正在使用ModelMapper从类型到目标 map:

@Override
public <R extends Record, E> RecordMapper<R, E> provide(RecordType<R> recordType,
                                                        Class<? extends E> type) {

    if (mapping.containsKey(type)) {
        return record -> modelMapper.map(mapping.get(type), type);
    }

    return new DefaultRecordMapper<>(recordType, type);
}

If it helps, I am configuring jOOQ using a DefaultConfiguration object (which is a bean):如果有帮助,我正在使用 DefaultConfiguration object(这是一个 bean)配置 jOOQ:

@Bean
public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
    jooqConfiguration.setConnectionProvider(dataSourceConnectionProvider());
    jooqConfiguration.setExecuteListenerProvider(new DefaultExecuteListenerProvider(
            jooqToSpringExceptionTranslator()));
    jooqConfiguration.setSQLDialect(
            SQLDialect.valueOf(env.getRequiredProperty("jooq.sql.dialect")));
    jooqConfiguration.setRecordMapperProvider(new JooqRecordMapperFactory(modelMapper()));

    return jooqConfiguration;
}

And then for Code Generation I am configuring it in gradle:然后对于代码生成,我在 gradle 中配置它:

jooq {
version = '3.10.5'
edition = 'OSS'

myDb(sourceSets.getByName("main")) {
    jdbc {
        driver = dbDriver
        url = dbUrl
        user = dbUsername
    }
    generator {
        name = 'org.jooq.util.JavaGenerator'
        strategy {
            name = 'org.jooq.util.DefaultGeneratorStrategy'
        }
        database {
            name = 'org.jooq.util.postgres.PostgresDatabase'
            inputSchema = dbSchema
        }
        generate {
            relations = true
            deprecated = false
            records = true
            immutablePojos = true
            fluentSetters = true
            daos = true
        }
        target {
            packageName = 'com.textiq.quinn.common.dao.model.generated'
        }
    }
}
}

I am sure there is a disconnect here between both configurations but I can't glean from the documentation how I synch these.我确信这两种配置之间存在脱节,但我无法从文档中了解我是如何同步这些配置的。 Ideally I want jOOQ to generate Pojos (based on the mapping that ModelMapper provides in my implementation of RecordMapperProvider ) and also have jOOQ provide the DAO's for these Pojos.理想情况下,我希望 jOOQ 生成 Pojo(基于ModelMapper在我的RecordMapperProvider实现中提供的映射),并让 jOOQ 为这些 Pojo 提供 DAO。 Is this possible?这可能吗? The documentation states:文件指出:

If you're using jOOQ's code generator, you can configure it to generate POJOs for you, but you're not required to use those generated POJOs.如果您正在使用 jOOQ 的代码生成器,您可以将其配置为为您生成 POJO,但您不需要使用那些生成的 POJO。 You can use your own.你可以使用你自己的。 See the manual's section about POJOs with custom RecordMappers to see how to modify jOOQ's standard POJO mapping behaviour.请参阅手册中关于带有自定义 RecordMappers 的 POJO 的部分,以了解如何修改 jOOQ 的标准 POJO 映射行为。

Source: https://www.jooq.org/doc/3.9/manual/sql-execution/fetching/pojos/来源: https://www.jooq.org/doc/3.9/manual/sql-execution/fetching/pojos/

Which to me indicates the possibility of this but only leads me to implementing RecordMapperProvider and nothing after that.对我来说,这表明了这种可能性,但只会让我实现RecordMapperProvider ,此后什么也没有。

I have implemented RecordMapperProvider but wondering how I register it to be used during the code generation phase, or even if that is possible? 我已经实现了RecordMapperProvider,但想知道如何注册它以在代码生成阶段使用,或者即使可以实现?

No, it's not possible, out of the box. 不,不可能,开箱即用。

I love the fact that Pojos & DAOs are generated but I want to define the Pojo myself without too much configuration code 我喜欢生成Pojos和DAO的事实,但是我想自己定义Pojo,而无需过多的配置代码

Then, I suggest turning off the generation of POJOs and DAOs and roll your own. 然后,我建议关闭POJO和DAO的生成,并自己滚动。 Either, create manual implementations of DAOs, or extend the JavaGenerator to do so. 创建DAO的手动实现,或扩展JavaGenerator来实现。

I'm a few years late to the party, but I actually found a very simple way to do this.我迟到了几年,但我实际上找到了一种非常简单的方法来做到这一点。 I'll admit that it's a little brittle, but you can refine it further to suit your needs.我承认它有点脆弱,但您可以进一步完善它以满足您的需要。

  1. Create a new module in your gradle project, for example called jooq-generator在您的 gradle 项目中创建一个新模块,例如名为jooq-generator
  2. Add jooq-codegen as a compileOnly dependency to the module将 jooq-codegen 作为 compileOnly 依赖项添加到模块
  3. Create a new class in the module:在模块中新建class:
public class Generator extends JavaGenerator {
    @Override
    public boolean generatePojos() {
        return false;
    }
}
  1. Create a new class in the module:在模块中新建class:

public class MyGeneratorStrategy extends DefaultGeneratorStrategy {
   @Override
   public String getJavaPackageName(Definition definition, Mode mode) {
       if (mode != Mode.POJO) {
           return super.getJavaPackageName(definition, mode);
       }
       return "com.example.my.model.package.prefix";
   }

}
  1. Add the module as a dependency to the jooqGenerator jooqGenerator project(":jooq-generator")将模块作为依赖项添加到 jooqGenerator jooqGenerator project(":jooq-generator")
  2. Add your new classes to the jooq config将新类添加到 jooq 配置
jooq {
    configurations {
        main {
            generationTool {
                generator {
                    name = 'com.example.my.package.name.Generator'
                    strategy {
                        name = 'com.example.my.package.name.MyGeneratorStrategy'
                    }
                }
            }
        }
    }
}
  1. The daos will now be generated using that package prefix for the POJOs instead of the generated package name.现在将使用 POJO 的 package 前缀而不是生成的 package 名称生成 daos。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM