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中配置如下配置;
<?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> <!--資料庫連線url配置--> <property name="connection.url">jdbc:mysql://localhost:3306/SSHBlog?useUnicode=true&characterEncoding=utf8&useSSL=true&zeroDateTimeBehavior=convertToNull</property> <!--資料庫驅動配置--> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!--資料庫使用者名稱配置--> <property name="connection.username">root</property> <!--資料庫密碼配置--> <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、配置資料庫
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
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則需要下載外掛。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。