IDEA 生成 JPA實體類


文章目錄
1、創建工程
2、連接數據源
3、生成`JPA`實體類
4、生成實體類結果
1、創建工程
使用Maven來構建工程,為了簡化創建步驟

創建一個新工程不包含任何Maven模板,[按需添加]

命名 GroupId、ArifactId

默認即可,點擊Finish

主界面右下角選擇Auto-Import

2、連接數據源
如下圖,打開Database

在Database界面,點擊+按鈕打開數據源界面

建立數據源,設置name、host、database、user、password,測試連接Test Connection

連接成功

3、生成JPA實體類
打開Project Strueture,選中Modules,點擊+添加JPA模塊


打開 Persistence,右擊依次選擇Generate Persistence Mapping、By Database Schema打開Import Database Schema窗口

按下圖紅框,箭頭指示操作,依次點擊OK 、yes按鈕

4、生成實體類結果
在java/com.testjpa包下面生成兩個文件,分別是:

UserEntity實體類內容
package com.testjpa;

import javax.persistence.*;

@Entity
@Table(name="user", schema="mypro", catalog="")
public class UserEntity {
private Integer id;
private String name;
private String profession;

@Id
@Column(name="id", nullable=false)
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id=id;
}

@Basic
@Column(name="name", nullable=false, length=20)
public String getName() {
return name;
}

public void setName(String name) {
this.name=name;
}

@Basic
@Column(name="profession", nullable=true, length=20)
public String getProfession() {
return profession;
}

public void setProfession(String profession) {
this.profession=profession;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

UserEntity that=(UserEntity) o;

if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (profession != null ? !profession.equals(that.profession) : that.profession != null) return false;

return true;
}

@Override
public int hashCode() {
int result=id != null ? id.hashCode() : 0;
result=31 * result + (name != null ? name.hashCode() : 0);
result=31 * result + (profession != null ? profession.hashCode() : 0);
return result;
}
}

 


UserEntity.xml文件內容
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
version="2.0">

<entity class="com.testjpa.UserEntity">
<table name="user" schema="mypro" catalog=""/>
<attributes>
<id name="id">
<column name="id" precision="11"/>
</id>
<basic name="name">
<column name="name" length="20"/>
</basic>
<basic name="profession">
<column name="profession" nullable="false" length="20"/>
</basic>
</attributes>
</entity>
</entity-mappings>

---------------------
作者:dadeity
來源:CSDN
原文:https://blog.csdn.net/github_38336924/article/details/82791422


免責聲明!

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



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