美文网首页
Jooq生成数据库访问代码

Jooq生成数据库访问代码

作者: rainmanhhh | 来源:发表于2018-08-08 17:45 被阅读0次
    1. 新建module(maven或gradle)
    2. 引入依赖
      jdbc驱动:
      例如mysql-connector-java
      jooq依赖:
      org.jooq:jooq-meta:$jooqVersion
      org.jooq:jooq-codegen:$jooqVersion
      如果是java9以上,还需要:
      javax.xml:jaxb-api:2.1
      javax.xml:jaxb-impl:2.1
      javax.activation:javax.activation-api:1.2.0
    3. gradle:新建JavaExec类型的task
      build.gradle.kts
      val jooqCodeGen by tasks.creating(JavaExec::class) {
      classpath = sourceSets["main"].runtimeClasspath
      main = "org.jooq.codegen.GenerationTool"
      args = listOf("default.xml")
      }
    4. maven:使用exec插件
      pom.xml
        <build>
          <plugins>
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>exec-maven-plugin</artifactId>
              <version>1.5.0</version>
              <executions>
                <execution>
                  <goals><goal>java</goal></goals>
                </execution>
              </executions>
              <configuration>
                <mainClass>org.jooq.codegen.GenerationTool</mainClass>
                <arguments>
                  <argument>default.xml</argument>
                </arguments>
                <cleanupDaemonThreads>false</cleanupDaemonThreads>
              </configuration>
            </plugin>
          </plugins>
        </build>
      
    5. 输入文件default.xml示例
      <?xml version="1.0" encoding="UTF-8"?>
      <configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.11.0.xsd">
        <jdbc>
          <driver>com.mysql.jdbc.Driver</driver>
          <url>jdbc:mysql://localhost:3306</url>
          <user>root</user>
          <password>password</password>
        </jdbc>
        <generator>
          <name>org.jooq.codegen.JavaGenerator</name>
          <database>
            <name>org.jooq.meta.mysql.MySQLDatabase</name>
            <inputSchema>demo</inputSchema>
            <includes>
              table1
              |table2
            </includes>
            <excludes/>
          </database>
          <target>
            <packageName>my.demo.generated</packageName>
            <directory>src/main/java/</directory>
          </target>
      
          <strategy>
            <matchers>
              <tables>
                <table>
                  <expression>^(.*)$</expression>
                  <tableClass>
                    <transform>PASCAL</transform>
                    <expression>T_$1</expression>
                  </tableClass>
                  <recordClass>
                    <transform>PASCAL</transform>
                    <expression>R_$1</expression>
                  </recordClass>
                </table>
              </tables>
            </matchers>
          </strategy>
      
          <generate>
            <pojos>true</pojos>
            <daos>true</daos>
            <springAnnotations>true</springAnnotations>
            <fluentSetters>true</fluentSetters>
            <javaTimeTypes>true</javaTimeTypes>
          </generate>
        </generator>
      </configuration>
      
    6. 若有更进一步的自定义需求,例如从dao生成controller,建议继承JavaGenerator,并重写方法
      protected void generateDao(TableDefinition table, JavaWriter out)
      

    相关文章

      网友评论

          本文标题:Jooq生成数据库访问代码

          本文链接:https://www.haomeiwen.com/subject/jhmxbftx.html