hibernate 反向工程

    xiaoxiao2026-05-18  1

    本文用到的开发环境:

    eclipse: eclipse-jee-neon-R-win32

    MySQL: mariadb-10.0.19-win32

    hibernate: hibernate-release-4.3.11.Final

    MySQL connector mysql-connector-java-5.1.39-bin.jar

    JBoss Tools JBoss Tools 4.4.0.Final


    hibernate 反向工程插件的安装方法,看这里


    步骤一: 打开 hibernate Perspective 如下图:


    步骤二: 按自己需要创建数据库,本文示例数据库名为forum,数据库表如下:


    步骤三: 生成hibernate.cfg.xml

    如下图,在项目右键 -> New -> Other…,然后查找 hibernate 插件,再然后选择 Hibernate Configuration File

    下一步如下图: hibernate.cfg.xml 的命名别打错, 注意把 hibernate.cfg.xml 放到项目的 src 文件夹

    下一步如下图: 数字1处,注意 hibernate jar 的版本号 数字2处,可以使用 Get values from Connection 来 生成本窗口的数据库连接信息 如下图填写必要的信息即可,下图空白的部分可忽略 Create a console configuration 处请打勾 console configuration 在下面会用到

    附: 上面的 Create a console configuration 勾选后,生成完 hibernate.cfg.xml 后, 在 Hibernate Configurations 视图,有刚刚生成的 Configuration,不用深究它是什么,知道它等一下有用就好。

    当然,如果需要手动生成 console configuration,也可以在这个视图进行新建,点击上图的 +号 新建 弹出如下图: Configuration file 处选择上面生成的 hibernate.cfg.xml 即可


    步骤四: 生成 hibernate.reveng.xml

    如下图,在项目右键 -> New -> Other…,然后查找 hibernate 插件,再然后选择 Reverse Engineering File

    如下图: 选择 hibernate.reveng.xml 要放到项目的哪个地方,直接放项目根目录也可以了,放到项目的src文件夹也可以

    如下图: 选择步骤三生成的 console configuration -> Refresh -> 选择表 -> Include

    然后就生成了 hibernate.reveng.xml


    步骤五: 生成实体类 以及 映射文件 如下图,找到图中图标:

    下一步如下图: 数字1处:选择上面生成的 console configuration 数字2处:一般都选择src文件夹 数字3处:输入模型类的包名 数字4处:选择步骤四生成的 hibernate.reveng.xml

    选择Exporters选项卡,如下图: 选择框中三项,然后 Apply -> Run 其中 Hibernate xml configuration 最好勾选,否则要自己写 mapping 标签,因为上面生成的 hibernate.cfg.xml 是缺少 mapping 标签的,如果缺少 mapping 标签的话,是查不到数据的,而且没有明显的报错

    生成 成功后如下图:


    转载请注明原文地址: https://ju.6miu.com/read-1309807.html
    最新回复(0)