有数据库逆向工程的框架吗?

发布于 2024-10-27 23:33:30 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

给妤﹃绝世温柔 2024-11-03 23:33:30

表、列名、索引等可以使用标准jdbc类获取,参见数据库元数据。以下是一些可能帮助您入门的代码:

static void dumpResultSet(ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columnCount = md.getColumnCount();
    PrintStream out = System.out;
    while (rs.next()) {
        out.print("{\n");
        for (int i=1; i<=columnCount; i++) {
            out.print("    ");
            out.print(md.getColumnLabel(i));
            out.print(": ");
            out.print(rs.getObject(i));

            if (i<columnCount-1) {
                out.print(", ");
            }
            out.print("\n");
        }
        out.print("}\n");
    }
}

Connection con = ...;
DatabaseMetaData md = con.getMetaData();

dumpResultSet(md.getTables(null, null, null, new String[]{"TABLE", "VIEW"}));
dumpResultSet(md.getColumns(null, null, "TABLE_NAME", null));
dumpResultSet(md.getExportedKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getImportedKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getPrimaryKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getIndexInfo(null, null, "TABLE_NAME", false, true));

Tables, column names, indexes and so on can be obtained using standard jdbc classes, see DatabaseMetaData. Here is some code that might get you started:

static void dumpResultSet(ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columnCount = md.getColumnCount();
    PrintStream out = System.out;
    while (rs.next()) {
        out.print("{\n");
        for (int i=1; i<=columnCount; i++) {
            out.print("    ");
            out.print(md.getColumnLabel(i));
            out.print(": ");
            out.print(rs.getObject(i));

            if (i<columnCount-1) {
                out.print(", ");
            }
            out.print("\n");
        }
        out.print("}\n");
    }
}

Connection con = ...;
DatabaseMetaData md = con.getMetaData();

dumpResultSet(md.getTables(null, null, null, new String[]{"TABLE", "VIEW"}));
dumpResultSet(md.getColumns(null, null, "TABLE_NAME", null));
dumpResultSet(md.getExportedKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getImportedKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getPrimaryKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getIndexInfo(null, null, "TABLE_NAME", false, true));
偏闹i 2024-11-03 23:33:30

对于逆向工程,您可以使用 Spring Roo / Jboss forge 或 Play 框架。 Spring Roo 最适合增量脚手架。您只需使用 Roo 执行 2 个命令即可生成代码。

谢谢

For reverse engineering you can use Spring Roo / Jboss forge or Play framework. Spring Roo is best for incremental scaffolding. You can generate code by executing just 2 commands with Roo.

Thanks

娇妻 2024-11-03 23:33:30

我会使用像 Codesmith 这样的东西。它与语言无关,可以通过数据库来创建您想要的模型。
http://www.codesmithtools.com/

在任何适用于 Android 的 ORM 出现之前,我用它来生成我的模型对于sqlite。效果非常好。

I would use something like codesmith. It is language agnostic and can go over a database to create the models you want.
http://www.codesmithtools.com/

Before any ORMs came out for android I used it to generate my models for sqlite. It worked very nicely.

早茶月光 2024-11-03 23:33:30

我专门为此制作了一个工具:DB Importer。它从数据库模式生成 JPA 类。代码生成可通过 Groovy 脚本进行高度配置。该脚本中使用的 Groovy 语法与 Java 类似。

I have made a tool just for that: DB Importer. It generates JPA classes from a database schema. The code generation is highly configurable with a Groovy script. The Groovy syntax used in this script is just like Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文