IntelliJIDEA下如何自動生成Hibernate映射文件以及實體類

這篇文章將為大家詳細講解有關IntelliJIDEA下如何自動生成Hibernate映射文件以及實體類,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

作為一家“創(chuàng)意+整合+營銷”的成都網站建設機構,我們在業(yè)內良好的客戶口碑。創(chuàng)新互聯(lián)建站提供從前期的網站品牌分析策劃、網站設計、成都網站設計、成都做網站、創(chuàng)意表現(xiàn)、網頁制作、系統(tǒng)開發(fā)以及后續(xù)網站營銷運營等一系列服務,幫助企業(yè)打造創(chuàng)新的互聯(lián)網品牌經營模式與有效的網絡營銷方法,創(chuàng)造更大的價值。

1、構建項目并添加項目結構配置以及配置初始參數(shù)

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中配置如下配置;

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD//EN"  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory>  <!--數(shù)據(jù)庫連接url配置-->  <property name="connection.url">jdbc:MySQL://localhost:3306/SSHBlog?useUnicode=true&characterEncoding=utf8&useSSL=true&zeroDateTimeBehavior=convertToNull</property>  <!--數(shù)據(jù)庫驅動配置-->  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  <!--數(shù)據(jù)庫用戶名配置-->  <property name="connection.username">root</property>  <!--數(shù)據(jù)庫密碼配置-->  <property name="connection.password"></property>   <!-- DB schema will be updated if needed -->  <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory></hibernate-configuration>

1.9、第一步配置完畢。

2、配置數(shù)據(jù)庫

2.1、點擊左下角按鈕,使窗口樣式如圖所示;

2.2、選擇數(shù)據(jù)庫;

2.4、配置數(shù)據(jù)庫后測試連接是否成功,若成功后點擊確定;

2.5、數(shù)據(jù)庫如下;

3、生成Hibernate的實體類以及配置文件

3.1、點擊窗口中的Persistence;

3.2、在Persistence中右鍵項目,然后點擊Generate Persistence Mapping,選擇By Database Schema;

3.3、選擇數(shù)據(jù)源,配置實體類包,選擇要生成的實體類(其中日期類型的只能手動修改為java.util.Date),然后點擊OK;

3.4、等待一段時間之后,發(fā)現(xiàn)項目中的實體類以及配置文件已經自動生成。

3.5、生成的實體類以及配置文件如下所示;實體類:Contacts.java

package com.sshblog.entity; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*;import java.util.Date; @Entity@Table(name = "contacts")@JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler","operations","roles","menus"})public class Contacts {  private int id;  private String name;  private String address;  private String gender;  private Date dob;  private String email;  private Long mobile;   @Id  @Column(name = "id")  public int getId() {    return id;  }   public void setId(int id) {    this.id = id;  }   @Basic  @Column(name = "name")  public String getName() {    return name;  }   public void setName(String name) {    this.name = name;  }   @Basic  @Column(name = "address")  public String getAddress() {    return address;  }   public void setAddress(String address) {    this.address = address;  }   @Basic  @Column(name = "gender")  public String getGender() {    return gender;  }   public void setGender(String gender) {    this.gender = gender;  }   @Basic  @Column(name = "dob")  public Date getDob() {    return dob;  }   public void setDob(Date dob) {    this.dob = dob;  }   @Basic  @Column(name = "email")  public String getEmail() {    return email;  }   public void setEmail(String email) {    this.email = email;  }   @Basic  @Column(name = "mobile")  public Long getMobile() {    return mobile;  }   public void setMobile(Long mobile) {    this.mobile = mobile;  }   @Override  public boolean equals(Object o) {    if (this == o) return true;    if (o == null || getClass() != o.getClass()) return false;     Contacts contacts = (Contacts) o;     if (id != contacts.id) return false;    if (name != null ? !name.equals(contacts.name) : contacts.name != null) return false;    if (address != null ? !address.equals(contacts.address) : contacts.address != null) return false;    if (gender != null ? !gender.equals(contacts.gender) : contacts.gender != null) return false;    if (dob != null ? !dob.equals(contacts.dob) : contacts.dob != null) return false;    if (email != null ? !email.equals(contacts.email) : contacts.email != null) return false;    if (mobile != null ? !mobile.equals(contacts.mobile) : contacts.mobile != null) return false;     return true;  }   @Override  public int hashCode() {    int result = id;    result = 31 * result + (name != null ? name.hashCode() : 0);    result = 31 * result + (address != null ? address.hashCode() : 0);    result = 31 * result + (gender != null ? gender.hashCode() : 0);    result = 31 * result + (dob != null ? dob.hashCode() : 0);    result = 31 * result + (email != null ? email.hashCode() : 0);    result = 31 * result + (mobile != null ? mobile.hashCode() : 0);    return result;  }}

配置文件:Contacts.hbm.xml

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>   <class name="com.sshblog.entity.Contacts" table="contacts" schema="SSHBlog">    <id name="id" column="id"/>    <property name="name" column="name"/>    <property name="address" column="address"/>    <property name="gender" column="gender"/>    <property name="dob" column="dob"/>    <property name="email" column="email"/>    <property name="mobile" column="mobile"/>  </class></hibernate-mapping>

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

使用IntelliJ IDEA的Hibernate生成實體類的好處是方便編碼,提升編碼效率;

相比較Eclipse而言,IntelliJ IDEA自帶Hibernate生成的機制,而Eclipse則需要下載插件。

關于“IntelliJIDEA下如何自動生成Hibernate映射文件以及實體類”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

本文題目:IntelliJIDEA下如何自動生成Hibernate映射文件以及實體類
標題鏈接:http://muchs.cn/article22/geedjc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供響應式網站靜態(tài)網站、域名注冊標簽優(yōu)化、網站改版、搜索引擎優(yōu)化

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網站建設