很多人都在使用myEclipse,很多公司也都使用hibernate框架,老版本的hibernate中,由于没有annotation,我们需要写两个文件来维护表与对象的关系,写一个类,就要写一个hbm.xml文件,数据库表比较少就无所谓,但是多了就很麻烦,要一个一个的手写非常浪费时间,还有可能写错。新版的hibernate加入了annotation的形式来映射对象与数据库表之间的关系,虽然不需要写hbm.xml文件了,但是数据库表非常多的情况下,还是需要写很多的类。我们为何不找个工具自动帮我们生成这些文件呢?myEclipse就可以做到,下面我来讲解如何使用myEclipse来生成hibernate所需文件。

2.   打开myEclipse

 通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

3. 建立myEclipse工程

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

4. 选择web project

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

5.打开数据库透视图

  在myEclipse右上角

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

点击右键

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

添加连接数据库所需驱动

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

测试是否联通

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

联通后Finish就可以了

6. 为项目添加hibernate支持

我们先回到myEclipse透视图

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

选中项目然后添加hibernate支持

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

7.   选择一些选项

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

选中刚才在数据库透视图里面创建的连接

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

完成后

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

 

8.反向生成

我们在回到数据库透视图

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

打开数据连接

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

打开刚刚创建的连接,然后打开用户名下的表

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

这个就是she用户下的表,我们选中要反向生成的表,可以多选,然后点击右键

 

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

点击Hibernate Reverse Engineering…

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

选择你项目的src目录,填写一个反向生成后,类放哪个包下

Create a Hibernate mapping file (*.hbm.xml)这句话的意思是生成类和hbm.xml文件的,

如果想生成annotation文件请选择下面的Add hibernate mapping annotations to POJO那个选项

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

这一步你可以什么都不选,如果确定数据库表都有主键,并且知道生成方式,你可以选择Id Generator

如果是uuid生成,可以选择

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

这步不选也可以,下一步也可以选择。

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

 

Include referenced tables (A->B)

Include referenced tables (A<-B)

这两个一定要选择,否则myeclipse不能给你生成一对多、多对多等映射,如果只想生成单项映射,可以去掉一个,一般我们都选择,双项映射。

在回到myEclipse透视图,查看项目

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

 

我们看到项目已经生成了映射文件和类。

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

已经生成了关系映射

Annotation方式的

通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

对于数据库的表,一定要有主键,不然,hibernate会自动帮你生成一个主键,这样不好

 

 

相关文章:

  • 2021-08-15
  • 2021-05-02
  • 2022-12-23
  • 2021-11-11
  • 2021-05-17
  • 2021-09-09
  • 2021-06-26
猜你喜欢
  • 2021-08-01
  • 2022-12-23
  • 2021-10-19
  • 2021-10-11
  • 2022-01-07
相关资源
相似解决方案