IntelliJ IDEA下自動(dòng)生成Hibernate映射文件以及實(shí)體類
1、構(gòu)建項(xiàng)目并添加項(xiàng)目結(jié)構(gòu)配置以及配置初始參數(shù)
1.1、如圖將基本的架子搭建好

1.2、點(diǎn)擊File,彈出的菜單中點(diǎn)擊Project Structure;
1.3、點(diǎn)擊左側(cè)的Modules,再點(diǎn)擊“+”號(hào),再在彈出的菜單中選擇Hibernate;

1.4、在這時(shí),項(xiàng)目中多出了一個(gè)Hibernate,點(diǎn)擊Hibernate,再點(diǎn)擊“+”號(hào),選擇hibernate.hbm.xml;

1.5、彈出的窗口中選擇Hibernate的版本,然后點(diǎn)擊OK;

1.6、點(diǎn)擊OK后在原來1.4步驟的窗口中的Apply按妞應(yīng)用到項(xiàng)目;
1.7、這時(shí)項(xiàng)目架子中多出了一個(gè)名為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ù)庫驅(qū)動(dòng)配置--> <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、點(diǎn)擊左下角按鈕,使窗口樣式如圖所示;

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

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

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

3、生成Hibernate的實(shí)體類以及配置文件
3.1、點(diǎn)擊窗口中的Persistence;

3.2、在Persistence中右鍵項(xiàng)目,然后點(diǎn)擊Generate Persistence Mapping,選擇By Database Schema;

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

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

3.5、生成的實(shí)體類以及配置文件如下所示;實(shí)體類: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生成實(shí)體類的好處
使用IntelliJ IDEA的Hibernate生成實(shí)體類的好處是方便編碼,提升編碼效率;
相比較Eclipse而言,IntelliJ IDEA自帶Hibernate生成的機(jī)制,而Eclipse則需要下載插件。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Spring AOP做接口權(quán)限校驗(yàn)和日志記錄
本文介紹了面向切面編程(AOP)的基本概念、應(yīng)用場景及其在Spring中的實(shí)現(xiàn)原理,通過AOP,可以方便地在不修改原有代碼的情況下,實(shí)現(xiàn)日志記錄、權(quán)限校驗(yàn)等功能,以學(xué)生身份證號(hào)查詢接口為例,展示了如何定義權(quán)限注解、切面類以及權(quán)限驗(yàn)證服務(wù),感興趣的朋友一起看看吧2025-01-01
SpringBoot自定義bean綁定實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot自定義bean綁定,最常見的配置綁定的場景,是在自定義的bean中通過@Value注解將某個(gè)屬性和對(duì)應(yīng)的配置綁定2022-10-10
使用SpringBoot整合ssm項(xiàng)目的實(shí)例詳解
Spring Boot 現(xiàn)在已經(jīng)成為 Java 開發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬象的,可以跟各種技術(shù)集成。這篇文章主要介紹了使用SpringBoot整合ssm項(xiàng)目,需要的朋友可以參考下2018-11-11
Java語言----三種循環(huán)語句的區(qū)別介紹
下面小編就為大家?guī)硪黄狫ava語言----三種循環(huán)語句的區(qū)別介紹。小編舉得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
Java使用多線程處理未知任務(wù)數(shù)的方案介紹
這篇文章主要為大家詳細(xì)介紹了Java如何使用多線程實(shí)現(xiàn)處理未知任務(wù)數(shù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
Java中常見延時(shí)隊(duì)列的實(shí)現(xiàn)方案小結(jié)(建議收藏)
延時(shí)隊(duì)列它要具有隊(duì)列的特性,再給它附加一個(gè)延遲消費(fèi)隊(duì)列消息的功能,也就是說可以指定隊(duì)列中的消息在哪個(gè)時(shí)間點(diǎn)被消費(fèi),這篇文章主要介紹了Java中常見延時(shí)隊(duì)列的實(shí)現(xiàn)方案總結(jié),需要的朋友可以參考下2024-04-04
Spring Cloud Eureka 服務(wù)上下線監(jiān)控的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Eureka 服務(wù)上下線監(jiān)控的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Spring?Boot整合郵箱發(fā)送郵件實(shí)例
大家好,本篇文章主要講的是Spring?Boot整合郵箱發(fā)送郵件實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02

