談起 Hibernate 應該得知道 Gavin King 大叔,他構建了 Hibernate ,並將其捐獻給了開源社區。數據庫
Hibernate 對象關係映射解決方案,爲面向對象的領域模型到傳統的關係型數據庫的映射,提供了一個使用方便的框架。編程
Hibernate 的設計目標是將軟件開發人員從大量相同的數據持久層相關編程工做中解放出來。app
Hibernate 也是目前Java開發中最爲火熱的數據庫持久層框架,現已歸JBOSS全部。框架
好了言歸正傳,IDEA 對開發者真的是貼心的小棉襖,本篇博客記錄下 IDEA 中鏈接數據庫反轉生成 Hibernate 實體和配置文件。ide
OK,數據源已添加好。ui
而後選擇數據源,選擇包,添加生成 Bean 的後綴,選擇表,選擇生成 xml 配置文件仍是註解。this
OK,結束,是否是比 Eclipse/MyEclipse 上面安裝各類插件效率要高的多。spa
若是你選擇生成帶 JPA 註解類,映射文件是能夠省略的,相反若是你生成映射文件,JPA 註解也能夠省略。hibernate
看你喜歡哪一種方式,有機會說說 JPA 註解類和映射文件在項目實戰中的優劣。插件
生成的JPA 註解類:
@Entity @Table(name = "user", schema = "db_test", catalog = "") public class UserPO { private String uuid; private String name; private String passwd; private String sex; private Timestamp birthday; private String phone; private String photo; private String email; private String yxbz; private String sorts; @Id @Column(name = "UUID") public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } @Basic @Column(name = "NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic @Column(name = "PASSWD") public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } @Basic @Column(name = "SEX") public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Basic @Column(name = "BIRTHDAY") public Timestamp getBirthday() { return birthday; } public void setBirthday(Timestamp birthday) { this.birthday = birthday; } @Basic @Column(name = "PHONE") public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Basic @Column(name = "PHOTO") public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } @Basic @Column(name = "EMAIL") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Basic @Column(name = "YXBZ") public String getYxbz() { return yxbz; } public void setYxbz(String yxbz) { this.yxbz = yxbz; } @Basic @Column(name = "SORTS") public String getSorts() { return sorts; } public void setSorts(String sorts) { this.sorts = sorts; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserPO userPO = (UserPO) o; if (uuid != null ? !uuid.equals(userPO.uuid) : userPO.uuid != null) return false; if (name != null ? !name.equals(userPO.name) : userPO.name != null) return false; if (passwd != null ? !passwd.equals(userPO.passwd) : userPO.passwd != null) return false; if (sex != null ? !sex.equals(userPO.sex) : userPO.sex != null) return false; if (birthday != null ? !birthday.equals(userPO.birthday) : userPO.birthday != null) return false; if (phone != null ? !phone.equals(userPO.phone) : userPO.phone != null) return false; if (photo != null ? !photo.equals(userPO.photo) : userPO.photo != null) return false; if (email != null ? !email.equals(userPO.email) : userPO.email != null) return false; if (yxbz != null ? !yxbz.equals(userPO.yxbz) : userPO.yxbz != null) return false; if (sorts != null ? !sorts.equals(userPO.sorts) : userPO.sorts != null) return false; return true; } @Override public int hashCode() { int result = uuid != null ? uuid.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (passwd != null ? passwd.hashCode() : 0); result = 31 * result + (sex != null ? sex.hashCode() : 0); result = 31 * result + (birthday != null ? birthday.hashCode() : 0); result = 31 * result + (phone != null ? phone.hashCode() : 0); result = 31 * result + (photo != null ? photo.hashCode() : 0); result = 31 * result + (email != null ? email.hashCode() : 0); result = 31 * result + (yxbz != null ? yxbz.hashCode() : 0); result = 31 * result + (sorts != null ? sorts.hashCode() : 0); return result; } }
生成的映射文件:
<?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.rombo.hiberdemo.po.OrgaUserPO" table="orga_user" schema="db_test"> <id name="uuid" column="UUID"/> <property name="orgaid" column="ORGAID"/> <property name="userid" column="USERID"/> <property name="mtype" column="MTYPE"/> </class> </hibernate-mapping>