如何使用jooq而不是xml从表中创建JavaPOJO?
我想使用jooq程序从表中创建.java,而不是通过.xml 我已经尝试过xml,但它不是我想要的 首先,jooq可以做什么 第二,有人知道怎么做吗如何使用jooq而不是xml从表中创建JavaPOJO?,java,pojo,jooq,Java,Pojo,Jooq,我想使用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
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) {
}
}
}
}
}
是的,您可以通过编程方式配置jOOQ的代码生成器。当您查看的源代码时,您将看到您可以从控制台调用其重载的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配置完全相同的结构