Hibernate逆向工程全过程

时间:2023-03-10 02:38:10
Hibernate逆向工程全过程

前提你已经创建好了数据库,按如下操作进行:

1.添加hibernate.cfg.xml

  在src下-->new-->other--->hibernate-->选择“Hibernate Configuration File(cfg.xml)”-->next-->选中项目名-->next-->填上连接数据库的必要信息-->finish

Hibernate逆向工程全过程

按如下方式填写好相关信息:

Hibernate逆向工程全过程

生成了一个xx.hbm.xml文件,但是生成有点问题,我们需要修改下,把:

<property name="hibernate.connection.url">jdbc:mysql://&lt;host&gt;&lt;:port&gt;/&lt;database&gt;</property>

改成

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/数据库名</property>

2.创建Hibernate Console Configuration

还是按如上步骤,

Hibernate逆向工程全过程

不用写任何东西,默认即可。

其实我们在第一步hibernate.cfg.xml  把 create console configration勾选,就不用这个步骤了

3.

,添加:hibernate.reveng.xml

右击项目-->new-->other--->(hibernate)-->选择“Hibernate Reverse Engineering File(reveng.xml)”-->next.

会出现如下图:

我们选中相应的console configration,点击Refresh按钮,会显示该数据库对应的表格:

Hibernate逆向工程全过程

选中所有的表include,点击Finish,会发现在src多了个hibernate.reveng.xml。打开这个:

Hibernate逆向工程全过程

添加完以后,保存,关闭。

4,生成xxx.hbm.xml和bean.

启动Hibernate Code Generation Configuerations 工具,这个工具在哪里,找了好久,需要我们切换perspective。

open perspetive->other->hibernate  选择 了hibernate的perspective之后,可以看到布局变成了:

Hibernate逆向工程全过程

点击hibernate code generation configuration 工具,在工具栏运行后边:

Hibernate逆向工程全过程最后一个就是。

Hibernate逆向工程全过程

Main标签下的Output directory选择的是生成文件的位置,选择到src目录即可。Reverse engineer from JDBC Connection这个单选框很重要,选上就是了。

package不写的话默认就是src下面,写不写都可以。记住要勾选reverse enginerer from jdbc。

然后在切换到第二个选项卡exported:

Hibernate逆向工程全过程

执行run后便生成了相应的pojo文件:

一般自动生成的mapping文件(XXX.hbm.xml)都需要修改后才能使用。

参考了;

http://www.cnblogs.com/overstep/archive/2012/08/15/2639655.html]

http://www.cnblogs.com/whjblogs/p/3137385.html

http://blog.csdn.net/undoner/article/details/8958512