• 用Hibernate Tools生成Hibernate Mapping映射文件


    用Hibernate Tools生成Hibernate Mapping映射文件

    Eclipse中要集成安装Hibernate Tools组件

    如果没有,请查看:Eclipse juno 中安装 JBoss Tools,集成Hibernate

    一.确定环境:

    1.Maven3.0.5

    2.Eclipse Juno,集成Jboss/Hibernate Tools

    3.Oracle10g

    4.Hibernate3.6.5.Final

    5.JDK1.7.0_11

    二.打开Hibernate Perspective视图层

    Eclipse中打开“Hibernate Perspective“。

      选择“Windows” >> “Open Perspective” >> “Others…” , 选择 “Hibernate“。

    三.新建Hibernate Configuration 配置

    1.在Hibernate Perspective中, 右键,选 “Add Configuration…”,弹出对话框

    2.在 “Project”区域, 单击 “Browse..” 选择你自己的项目。
    3.在 “Database Connection” 区域, 单击 “New..”创建你自己的数据库设定。


    4.弹出Connection Profile中选Oracle ,并填写“Name”后,Next

    5.在弹出对话框中,选Driver,没有的话要新加,此处新加Driver。

    6.弹出对话框中选一个driver,如果出现“Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar.”

    就要在“JAR List”选项卡中删除原有的驱动文件,重新加一下就好了。

    删除驱动文件重新添加,此处添加的是Maven仓库中的jar包

    完成后点击OK

    填写完数据库的相关连接后,可以点Test Connection测试是否可以连接上。

    连接上如图

    7.Configuration file配置

    店家“Setup”弹出“Setup configuration file”对话框,如果不存在.dfg.xml文件,可以新建

    上图点击“Create New...”后,弹出新建cfg.xml文件对话框

    创建在项目目录下的main/java/resources下,

    我的是在“maven-hibernate-demo/src/main/resources”

    可以点击next配置更具体的设置,此处我们直接Finsh

    返回前一界面后,OK。记住,此处的“Hibernate Version”选项一定要选对版本,否则反向工程不会成功。

    查看Hibernate Configurations视图

    四.查看src/main/resources目录下的刚建好的hibernate.cfg.xml文件

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
            <property name="hibernate.connection.password">password</property>
            <property name="hibernate.connection.url">jdbc:oracle:thin:@yourdatabaseip:gzip</property>
            <property name="hibernate.connection.username">username</property>
            <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        </session-factory>
    </hibernate-configuration>
    复制代码

    五.生成Hibernate代码

    在“Hibernate Perspective”视图中, 点击 “Hibernate Code Generation Configuration”

    在生成代码框中,新建configuration,并填写Main选项卡和Exporters选项卡,

    Main中需要填写输出路径等,填完的如下

    console configuration选择前边刚配置完的Hibernate Configuration,前边的名字是“hibernate”

    Output directory选择输出路径

    Reverse enginner from JDBC Connection勾选

    Package填写包名

    reveng.xml填写创建hibernate.reveng.xml的位置,没有则新建。

    在Exporters选择相应要生成的代码

    可以生成Model , mapping file (hbm) , DAO, annotation code 等等。

    点Run生成代码

    生成的代码结构如下

  • 相关阅读:
    Radmin01-如何使用Radmin进行云服务器的远程连接与文件传输?
    vmware-workstation迁移虚拟机 15pro到12版本
    爬格子呀5-10
    爬格子呀--IEEE极限编程大赛留念
    爬格子呀5-8
    爬格子呀5-7
    爬格子呀5-6
    爬格子呀5-5
    爬格子呀5-4
    爬格子呀5-3
  • 原文地址:https://www.cnblogs.com/Jeely/p/11225527.html
Copyright © 2020-2023  润新知