您的位置:首页 > 其它

IntelliJ IDEA下自动生成Hibernate映射文件以及实体类

2019-04-16 17:31 316 查看

1、构建项目并添加项目结构配置以及配置初始参数

1.1、如图将基本的架子搭建好




1.2、点击File,弹出的菜单中点击Project Structure;

1.3、点击左侧的Modules,再点击“+”号,再在弹出的菜单中选择Hibernate;

1.4、在这时,项目中多出了一个Hibernate,点击Hibernate,再点击“+”号,选择hibernate.hbm.xml;

1.5、弹出的窗口中选择Hibernate的版本,然后点击OK;

1.6、点击OK后在原来1.4步骤的窗口中的Apply按妞应用到项目;
1.7、这时项目架子中多出了一个名为hibernate.hbm.xml的配置文件;

1.8、在hibernate.hbm.xml中配置如下配置;
  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!--数据库连接url配置-->
  8. <property name="connection.url">jdbc:mysql://localhost:3306/SSHBlog?useUnicode=true&characterEncoding=utf8&useSSL=true&zeroDateTimeBehavior=convertToNull</property>
  9. <!--数据库驱动配置-->
  10. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  11. <!--数据库用户名配置-->
  12. <property name="connection.username">root</property>
  13. <!--数据库密码配置-->
  14. <property name="connection.password"></property>
  15. <!-- DB schema will be updated if needed -->
  16. <!-- <property name="hbm2ddl.auto">update</property> -->
  17. </session-factory>
  18. </hibernate-configuration>
[/code]
1.9、第一步配置完毕。

2、配置数据库

2.1、点击左下角按钮,使窗口样式如图所示;

2.2、选择数据库;

2.4、配置数据库后测试连接是否成功,若成功后点击确定;

2.5、数据库如下;

3、生成Hibernate的实体类以及配置文件

3.1、点击窗口中的Persistence;

3.2、在Persistence中右键项目,然后点击Generate Persistence Mapping,选择By Database Schema;

3.3、选择数据源,配置实体类包,选择要生成的实体类(其中日期类型的只能手动修改为java.util.Date),然后点击OK;


3.4、等待一段时间之后,发现项目中的实体类以及配置文件已经自动生成。


3.5、生成的实体类以及配置文件如下所示; 实体类:Contacts.java
  1. package com.sshblog.entity;
  2. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  3. import javax.persistence.*;
  4. import java.util.Date;
  5. @Entity
  6. @Table(name = "contacts")
  7. @JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler","operations","roles","menus"})
  8. public class Contacts {
  9. private int id;
  10. private String name;
  11. private String address;
  12. private String gender;
  13. private Date dob;
  14. private String email;
  15. private Long mobile;
  16. @Id
  17. @Column(name = "id")
  18. public int getId() {
  19. return id;
  20. }
  21. public void setId(int id)< 20000 /span> {
  22. this.id = id;
  23. }
  24. @Basic
  25. @Column(name = "name")
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. @Basic
  33. @Column(name = "address")
  34. public String getAddress() {
  35. return address;
  36. }
  37. public void setAddress(String address) {
  38. this.address = address;
  39. }
  40. @Basic
  41. @Column(name = "gender")
  42. public String getGender() {
  43. return gender;
  44. }
  45. public void setGender(String gender) {
  46. this.gender = gender;
  47. }
  48. @Basic
  49. @Column(name = "dob")
  50. public Date getDob() {
  51. return dob;
  52. }
  53. public void setDob(Date dob) {
  54. this.dob = dob;
  55. }
  56. @Basic
  57. @Column(name = "email")
  58. public String getEmail() {
  59. return email;
  60. }
  61. public void setEmail(String email) {
  62. this.email = email;
  63. }
  64. @Basic
  65. @Column(name = "mobile")
  66. public Long getMobile() {
  67. return mobile;
  68. }
  69. public void setMobile(Long mobile) {
  70. this.mobile = mobile;
  71. }
  72. @Override
  73. public boolean equals(Object o) {
  74. if (this == o) return true;
  75. if (o == null || getClass() != o.getClass()) return false;
  76. Contacts contacts = (Contacts) o;
  77. if (id != contacts.id) return false;
  78. if (name != null ? !name.equals(contacts.name) : contacts.name != null) return false;
  79. if (address != null ? !address.equals(contacts.address) : contacts.address != null) return false;
  80. if (gender != null ? !gender.equals(contacts.gender) : contacts.gender != null) return false;
  81. if (dob != null ? !dob.equals(contacts.dob) : contacts.dob != null) return false;
  82. if (email != null ? !email.equals(contacts.email) : contacts.email != null) return false;
  83. if (mobile != null ? !mobile.equals(contacts.mobile) : contacts.mobile != null) return false;
  84. return true;
  85. }
  86. @Override
  87. public int hashCode() {
  88. int result = id;
  89. result = 31 * result + (name != null ? name.hashCode() : 0);
  90. result = 31 * result + (address != null ? address.hashCode() : 0);
  91. result = 31 * result + (gender != null ? gender.hashCode() : 0);
  92. result = 31 * result + (dob != null ? dob.hashCode() : 0);
  93. result = 31 * result + (email != null ? email.hashCode() : 0);
  94. result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
  95. return result;
  96. }
  97. }
[/code] 配置文件:Contacts.hbm.xml
  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="com.sshblog.entity.Contacts" table="contacts" schema="SSHBlog">
  7. <id name="id" column="id"/>
  8. <property name="name" column="name"/>
  9. <property name="address" column="address"/>
  10. <property name="gender" column="gender"/>
  11. <property name="dob" column="dob"/>
  12. <property name="email" column="email"/>
  13. <property name="mobile" column="mobile"/>
  14. </class>
  15. </hibernate-mapping>
[/code]

4、使用IntelliJ IDEA生成实体类的好处

使用IntelliJ IDEA的Hibernate生成实体类的好处是方便编码,提升编码效率; 相比较Eclipse而言,IntelliJ IDEA自带Hibernate生成的机制,而Eclipse则需要下载插件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: