【问题标题】:How to create java pojo from table using jooq not by xml?如何使用jooq而不是xml从表中创建java pojo?
【发布时间】:2014-02-02 04:12:21
【问题描述】:

我想以编程方式使用 jooq 从表中创建 .java,而不是通过 .xml。

xml我已经试过了,但不是我想要的。

首先jooq可以做到吗?

其次,有人知道怎么做吗?

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.jooq.util.DefaultGenerator;
import org.jooq.util.JavaGenerator;
import org.jooq.util.mysql.MySQLDatabase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
    public static void main(String[] args) {
        Connection conn = null;

        String userName = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/library";

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);

            DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
            Result<Record> result = create.select().from("AUTHOR").fetch();

            //------------ here I want to create AUTHOR.java from table AUTHOR by connecting to database  
            DefaultGenerator  g = new DefaultGenerator();
            MySQLDatabase database = new MySQLDatabase();
            database.getSchema(conn.getSchema());
            JavaGenerator javaGenerator = new JavaGenerator();
            javaGenerator.generate(database);

            //------------

            for (Record r : result) {
                Long id = r.getValue(AUTHOR.ID);
                String firstName = r.getValue(AUTHOR.FIRST_NAME);
                String lastName = r.getValue(AUTHOR.LAST_NAME);

                System.out.println("ID: " + id + " first name: " + firstName + " last name: " + lastName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ignore) {
                }
            }
        }
    }
}

【问题讨论】:

    标签: java pojo jooq


    【解决方案1】:

    是的,您可以通过编程方式配置 jOOQ 的代码生成器。当您查看GenerationTool 的源代码时,您会发现您可以从控制台(as documented in the manual)调用其重载的main() 方法,或者通过传递一个org.jooq.util.jaxb.Configuration 对象。一个例子:

    import org.jooq.util.jaxb.*;
    
    // [...]
    
    Configuration configuration = new Configuration()
        .withJdbc(new Jdbc()
            .withDriver("com.mysql.jdbc.Driver")
            .withUrl("jdbc:mysql://localhost:3306/library")
            .withUser("root")
            .withPassword("root"))
        .withGenerator(new Generator()
            .withName("org.jooq.util.DefaultGenerator")
            .withDatabase(new Database()
                .withName("org.jooq.util.mysql.MySQLDatabase")
                .withIncludes(".*")
                .withExcludes("")
                .withInputSchema("library"))
            .withTarget(new Target()
                .withPackageName("org.jooq.util.maven.example")
                .withDirectory("target/generated-sources/jooq")));
    
    GenerationTool.main(configuration);
    

    以上配置 POJO 是由 XJC 生成的,因此您可以使用与 XML 配置完全相同的结构。

    This is now also documented in the manual.

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2019-06-05
      相关资源
      最近更新 更多