使用Hibernate Tools从数据库表生成pojo和hbm
2012-11-07 16:36
369 查看
http://www.iteye.com/topic/787498
hibernate使用从数据库表生成pojo和hbm
1,安装HibernateTools插件
去https://www.jboss.org/tools/download/stable/下载HibernateTools的eclipse插件,只要该插件即可,没必要把所有的插件一起打包下载。
安装成功后选择File->New->Other... 可以看到已经包含了Hibernate的插件支持
![](http://dl.iteye.com/upload/attachment/332872/a9a71776-1d6a-30d0-bd00-cf0754cc7918.png)
2,新建hibernate.cfg.xml文件
选择File->New->Other...->Hibernate->Hibernate Configuration File(cfg.xml)->Next->【选择当前PROJECT】->Next, 设置到此进入数据库配置界面,根据您的项目进行合适的配置,我的如下
![](http://dl.iteye.com/upload/attachment/332874/e44c75b9-0c2b-3e3d-8583-dde090f02f5d.png)
记得将Create a console configuration勾上,免得过会还要单独去创建。
3,新建Hibernate console configuration
接上一步,点击Next后进入到Hibernate console configuration的配置界面,在Classpath中导入当前数据库对应的驱动,我的如下
![](http://dl.iteye.com/upload/attachment/332876/af48cbc5-4867-31c8-8626-3d17cf44076e.png)
4,新建Hibernate Reverse Engineering文件
选择File->New->Other...->Hibernate->Hibernate Reverse Engineering File(reveng.xml)->Next->【选择当前PROJECT】->Next->【在Console configuration中选择刚才新建的Hibernate console configuration】,设置完后选择你需要处理的表(注:如果你未在左侧看到数据库中含有的表,可点击Refresh进行刷新),我的如下:
![](http://dl.iteye.com/upload/attachment/332878/19678b7f-0b32-3568-8985-add6737d22a2.png)
5,生成pojo及hbm文件
点击工具栏中的Hibernate小图标下拉框,选择Hibernate Code Generation...得到如下界面
![](http://dl.iteye.com/upload/attachment/332880/39655c23-8c1e-30df-bd4d-2d82c3d8d1ce.png)
双击左侧的Hibernate Code Generation或者“右击->New”来打开一个新的Configuration,选择刚才新建的Console configuration,在Output directory中填入你的java源文件所在根目录,Package填入你希望你的pojo存放的包路径,reveng.xml填入你刚才新建的hibernate.reveng.xml,我的配置如下:
![](http://dl.iteye.com/upload/attachment/332882/58eaf803-5d8b-3805-be6b-9f0e6cf9072b.png)
选择Exporters这个tab,将Generate domain code(.java)和Generate mappings(hbm.xml)勾上
![](http://dl.iteye.com/upload/attachment/332884/f829a510-4d68-3a2b-ba13-a1a6a481a894.png)
6,完成
点击Run,成功生成所需文件,有些地方需要我们手动更改一下,比如主键生成方式等。
![](http://dl.iteye.com/upload/attachment/332886/94aeca85-8a28-3e33-ac3a-1393ee582809.png)
7,有同事说还可以用CodeBuilder来做以上同样的工作,更简单,没试过,下次可以试试
hibernate使用从数据库表生成pojo和hbm
1,安装HibernateTools插件
去https://www.jboss.org/tools/download/stable/下载HibernateTools的eclipse插件,只要该插件即可,没必要把所有的插件一起打包下载。
安装成功后选择File->New->Other... 可以看到已经包含了Hibernate的插件支持
![](http://dl.iteye.com/upload/attachment/332872/a9a71776-1d6a-30d0-bd00-cf0754cc7918.png)
2,新建hibernate.cfg.xml文件
选择File->New->Other...->Hibernate->Hibernate Configuration File(cfg.xml)->Next->【选择当前PROJECT】->Next, 设置到此进入数据库配置界面,根据您的项目进行合适的配置,我的如下
![](http://dl.iteye.com/upload/attachment/332874/e44c75b9-0c2b-3e3d-8583-dde090f02f5d.png)
记得将Create a console configuration勾上,免得过会还要单独去创建。
3,新建Hibernate console configuration
接上一步,点击Next后进入到Hibernate console configuration的配置界面,在Classpath中导入当前数据库对应的驱动,我的如下
![](http://dl.iteye.com/upload/attachment/332876/af48cbc5-4867-31c8-8626-3d17cf44076e.png)
4,新建Hibernate Reverse Engineering文件
选择File->New->Other...->Hibernate->Hibernate Reverse Engineering File(reveng.xml)->Next->【选择当前PROJECT】->Next->【在Console configuration中选择刚才新建的Hibernate console configuration】,设置完后选择你需要处理的表(注:如果你未在左侧看到数据库中含有的表,可点击Refresh进行刷新),我的如下:
![](http://dl.iteye.com/upload/attachment/332878/19678b7f-0b32-3568-8985-add6737d22a2.png)
5,生成pojo及hbm文件
点击工具栏中的Hibernate小图标下拉框,选择Hibernate Code Generation...得到如下界面
![](http://dl.iteye.com/upload/attachment/332880/39655c23-8c1e-30df-bd4d-2d82c3d8d1ce.png)
双击左侧的Hibernate Code Generation或者“右击->New”来打开一个新的Configuration,选择刚才新建的Console configuration,在Output directory中填入你的java源文件所在根目录,Package填入你希望你的pojo存放的包路径,reveng.xml填入你刚才新建的hibernate.reveng.xml,我的配置如下:
![](http://dl.iteye.com/upload/attachment/332882/58eaf803-5d8b-3805-be6b-9f0e6cf9072b.png)
选择Exporters这个tab,将Generate domain code(.java)和Generate mappings(hbm.xml)勾上
![](http://dl.iteye.com/upload/attachment/332884/f829a510-4d68-3a2b-ba13-a1a6a481a894.png)
6,完成
点击Run,成功生成所需文件,有些地方需要我们手动更改一下,比如主键生成方式等。
![](http://dl.iteye.com/upload/attachment/332886/94aeca85-8a28-3e33-ac3a-1393ee582809.png)
7,有同事说还可以用CodeBuilder来做以上同样的工作,更简单,没试过,下次可以试试
相关文章推荐
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 在MyEclipse的web项目/java项目中,使用Hibernate-tools中的hbm2java和hbm2ddl工具,根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用hibernate tools插件生成POJO
- Intellij IDEA 根据数据库自动生成pojo和hbm
- 使用middlegen(由数据库表生成hbm文件)
- eclipse 中 使用Hibernate Tools 3.4 从数据库方向生成 hibernate 配置文件
- eclipse使用hibernate tools插件生成mysql数据表的pojo
- eclipse使用hibernate tools插件生成POJO
- hibernate tools 根据数据库中mysql表生成 .java .hbm.xml
- 使用hibernate tools插件生成POJO
- 使用hibernate tools插件生成POJO
- Netbeans 使用 Hibernate 逆向工程 生成hbm和pojo
- netbeans 的通过数据库生成hbm映射文件与pojo实体类稍有问题
- 施用hibernate tools插件反向数据库生成POJO
- Intellij IDEA 根据数据库自动生成pojo和hbm
- Maven项目中,使用mybatis,根据数据库自动生成pojo实体类、dao、mapper