IntelliJ IDEA下自動生成Hibernate映射文件以及實體類


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.  
     
  16.  
    <!-- DB schema will be updated if needed -->
  17.  
    <!-- <property name="hbm2ddl.auto">update</property> -->
  18.  
    </session-factory>
  19.  
    </hibernate-configuration>

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.  
     
  3.  
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  4.  
     
  5.  
    import javax.persistence.*;
  6.  
    import java.util.Date;
  7.  
     
  8.  
    @Entity
  9.  
    @Table(name = "contacts")
  10.  
    @JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler","operations","roles","menus"})
  11.  
    public class Contacts {
  12.  
    private int id;
  13.  
    private String name;
  14.  
    private String address;
  15.  
    private String gender;
  16.  
    private Date dob;
  17.  
    private String email;
  18.  
    private Long mobile;
  19.  
     
  20.  
    @Id
  21.  
    @Column(name = "id")
  22.  
    public int getId() {
  23.  
    return id;
  24.  
    }
  25.  
     
  26.  
    public void setId(int id) {
  27.  
    this.id = id;
  28.  
    }
  29.  
     
  30.  
    @Basic
  31.  
    @Column(name = "name")
  32.  
    public String getName() {
  33.  
    return name;
  34.  
    }
  35.  
     
  36.  
    public void setName(String name) {
  37.  
    this.name = name;
  38.  
    }
  39.  
     
  40.  
    @Basic
  41.  
    @Column(name = "address")
  42.  
    public String getAddress() {
  43.  
    return address;
  44.  
    }
  45.  
     
  46.  
    public void setAddress(String address) {
  47.  
    this.address = address;
  48.  
    }
  49.  
     
  50.  
    @Basic
  51.  
    @Column(name = "gender")
  52.  
    public String getGender() {
  53.  
    return gender;
  54.  
    }
  55.  
     
  56.  
    public void setGender(String gender) {
  57.  
    this.gender = gender;
  58.  
    }
  59.  
     
  60.  
    @Basic
  61.  
    @Column(name = "dob")
  62.  
    public Date getDob() {
  63.  
    return dob;
  64.  
    }
  65.  
     
  66.  
    public void setDob(Date dob) {
  67.  
    this.dob = dob;
  68.  
    }
  69.  
     
  70.  
    @Basic
  71.  
    @Column(name = "email")
  72.  
    public String getEmail() {
  73.  
    return email;
  74.  
    }
  75.  
     
  76.  
    public void setEmail(String email) {
  77.  
    this.email = email;
  78.  
    }
  79.  
     
  80.  
    @Basic
  81.  
    @Column(name = "mobile")
  82.  
    public Long getMobile() {
  83.  
    return mobile;
  84.  
    }
  85.  
     
  86.  
    public void setMobile(Long mobile) {
  87.  
    this.mobile = mobile;
  88.  
    }
  89.  
     
  90.  
    @Override
  91.  
    public boolean equals(Object o) {
  92.  
    if (this == o) return true;
  93.  
    if (o == null || getClass() != o.getClass()) return false;
  94.  
     
  95.  
    Contacts contacts = (Contacts) o;
  96.  
     
  97.  
    if (id != contacts.id) return false;
  98.  
    if (name != null ? !name.equals(contacts.name) : contacts.name != null) return false;
  99.  
    if (address != null ? !address.equals(contacts.address) : contacts.address != null) return false;
  100.  
    if (gender != null ? !gender.equals(contacts.gender) : contacts.gender != null) return false;
  101.  
    if (dob != null ? !dob.equals(contacts.dob) : contacts.dob != null) return false;
  102.  
    if (email != null ? !email.equals(contacts.email) : contacts.email != null) return false;
  103.  
    if (mobile != null ? !mobile.equals(contacts.mobile) : contacts.mobile != null) return false;
  104.  
     
  105.  
    return true;
  106.  
    }
  107.  
     
  108.  
    @Override
  109.  
    public int hashCode() {
  110.  
    int result = id;
  111.  
    result = 31 * result + (name != null ? name.hashCode() : 0);
  112.  
    result = 31 * result + (address != null ? address.hashCode() : 0);
  113.  
    result = 31 * result + (gender != null ? gender.hashCode() : 0);
  114.  
    result = 31 * result + (dob != null ? dob.hashCode() : 0);
  115.  
    result = 31 * result + (email != null ? email.hashCode() : 0);
  116.  
    result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
  117.  
    return result;
  118.  
    }
  119.  
    }
配置文件: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.  
     
  7.  
    <class name="com.sshblog.entity.Contacts" table="contacts" schema="SSHBlog">
  8.  
    <id name="id" column="id"/>
  9.  
    <property name="name" column="name"/>
  10.  
    <property name="address" column="address"/>
  11.  
    <property name="gender" column="gender"/>
  12.  
    <property name="dob" column="dob"/>
  13.  
    <property name="email" column="email"/>
  14.  
    <property name="mobile" column="mobile"/>
  15.  
    </class>
  16.  
    </hibernate-mapping>
 

4、使用IntelliJ IDEA生成實體類的好處

使用IntelliJ IDEA的Hibernate生成實體類的好處是方便編碼,提升編碼效率;
相比較Eclipse而言,IntelliJ IDEA自帶Hibernate生成的機制,而Eclipse則需要下載插件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM