【问题标题】:Jooq code gen with multiple xml schema files具有多个 xml 模式文件的 Jooq 代码生成
【发布时间】:2018-01-06 13:43:33
【问题描述】:

我在 maven 中使用 jooq codegen 插件从 xml 模式文件生成代码。

<configuration>
    <generator>
        <database>
            <name>org.jooq.util.xml.XMLDatabase</name>
            <properties>

                <!-- Use any of the SQLDialect values here -->
                <property>
                    <key>dialect</key>
                    <value>MYSQL</value>
                </property>

                <!-- Specify the location of your database file -->
                <property>
                    <key>xml-file</key>
                    <value>${project.basedir}/src/main/resources/schema.xml</value>
                </property>
            </properties>
        </database>
        <generate>
            <daos>true</daos>
            <pojos>true</pojos>
            <records>true</records>
            <relations>true</relations>
            <globalObjectReferences>false</globalObjectReferences>
        </generate>
        <target>
            <!-- The destination package of your generated classes (within the 
                destination directory) -->
            <packageName>com.generated.classes</packageName>

            <!-- The destination directory of your generated classes. Using 
                Maven directory layout here -->
            <directory>${project.basedir}/src/generated/classes</directory>
        </target>
    </generator>
</configuration>

是否有从两个不同的架构文件生成代码的解决方案。示例:schema-other.xml。

【问题讨论】:

    标签: java jooq


    【解决方案1】:

    XMLDatabase 元数据源尚不支持此功能。待处理的功能请求是:https://github.com/jOOQ/jOOQ/issues/6260

    不过有一些解决方法:

    使用单独的配置

    如果两个架构/文件没有链接,您可以运行两个独立的代码生成运行。如果您使用的是 Maven,您可以这样做 (see also this question):

    <plugin>
      <groupId>org.jooq</groupId>
      <artifactId>jooq-codegen-maven</artifactId>
      <version>3.9.4</version>
    
      <executions>
        <execution>
          <id>first-generation</id>
          <phase>generate-sources</phase>
          <goals><goal>generate</goal></goals>
          <configuration>
            <generator>
              <database>
                <name>org.jooq.util.xml.XMLDatabase</name>
                ...
                <properties>
                  <property>
                    <key>xml-file</key>
                    <value>file1.xml</value>
                  </property>
                </properties>
              </database>
              ...
              <target>
                <packageName>com.generated.classes.schema1</packageName>
                <directory>${project.basedir}/src/generated/classes</directory>
              </target>
            </generator>
          </configuration>
        </execution>
    
        <execution>
          <id>second-generation</id>
          <phase>generate-sources</phase>
          <goals><goal>generate</goal></goals>
          <configuration>
            <!-- jOOQ configuration here -->
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    如果您使用独立代码生成,只需配置两个单独的运行。

    合并 XML 文件

    您当然可以手动将两个 XML 文件合并为一个,例如通过使用 XSLT 进行自动合并或手动合并。

    【讨论】:

    • 我用两个模式文件试过这个,但是如果它们的包名相同,第二次执行会删除第一次执行创建的类。是否有一个属性可以覆盖它?
    • @Pradeep:不,没有。输出包属于单代运行。在运行之间重用包的用例是什么?
    • 这个想法是将所有生成的类文件(来自两个模式文件)放在一个包下。
    • 嗯。我们将很快支持在XMLDatabase 中加载多个文件。同时,我能看到的最简单的解决方法是在运行代码生成器之前使用 XSLT 将两个文件合并为一个...
    猜你喜欢
    • 2019-02-16
    • 2020-01-07
    • 1970-01-01
    • 2021-09-16
    • 2021-03-28
    • 2021-12-17
    • 2014-09-09
    • 2017-12-03
    • 2017-08-13
    相关资源
    最近更新 更多