Intellij IDEA 如何通過數(shù)據(jù)庫表生成帶注解的實體類(圖文詳細教程)
第一步:新建一個Maven項目。項目的名稱為JpaDemo。
我這里是通過idea插件對應(yīng)的spring項目生成器https://start.spring.io,直接生成項目。如圖:

下一步,修改成對應(yīng)項目的基本信息。如圖:

選擇相應(yīng)的依賴jar包。

選擇項目的位置

完成創(chuàng)建

溫馨提示,之前需要安裝好maven。

第二步:配置數(shù)據(jù)庫連接。
選擇Mysql。

配置數(shù)據(jù)庫基本信息

其實配置了這個數(shù)據(jù)庫連接之后,是可以直接通過腳本進行導(dǎo)出數(shù)據(jù)庫實體類了,但是這個導(dǎo)出的實體類比較簡陋,需要進行修改比較多,或是需要自己進行修改生成腳本語句。如:

通過generate POJOs.clj即可導(dǎo)出實體類。
需要選一下實體類放置的地方。


效果如下:

但是以上的實體類沒有帶注解。那么我們通過項目中用到hibernate,或是jpa需要加注解怎么辦,總不能一個個注解加上去吧。idea當(dāng)然不會這么干啦。
使用IntelliJ IDEA快編碼速度:我們程序員的工作不是寫程序,而是寫程序解決問題。那我們刪了之前生成的實體類。我們重新生成一份帶注解的實體類。
第三步:配置hibernate文件。
如果沒有配置該配置文件,idea則沒有顯示出生成實體類的工具選項。

配置一下hibernate配置文件。
在資源文件下新建一個hibernate.cfg.xml配置文件。并輸入以下內(nèi)容。
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory> </hibernate-configuration>
如圖:

第四步:調(diào)出idea實體類生成工具。
調(diào)出生成實體類的配置工具

保存后。在主面板左側(cè)有persistence,在hibernate圖標(biāo)上點擊右鍵-Generate Persistence Mapping-By Database Scheme。


一開始是沒有選中數(shù)據(jù)源的。

配置選項
(1)數(shù)據(jù)源選擇
(2)生成實體類的位置
(3)實體類的前綴和后綴
(4)可以全選表,或是全不選表
(5)可以生成hibernate的實體類對應(yīng)的xml文件
(6)展開表之后可以修改對應(yīng)之間的類型。

第五步:選中需要執(zhí)行的數(shù)據(jù)庫表。

第六步:查看導(dǎo)出的效果。
生成過程

導(dǎo)出的結(jié)果
可以查看其中的一個實體類,看看效果。
package com.souvc.entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* Created by Administrator on 2017/3/22.
*/
@Entity
@Table(name = "authorities", schema = "test", catalog = "")
public class SouvcAuthoritiesEntity {
private String username;
private String authority;
@Basic
@Column(name = "username", nullable = false, length = 50)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "authority", nullable = false, length = 50)
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SouvcAuthoritiesEntity that = (SouvcAuthoritiesEntity) o;
if (username != null ? !username.equals(that.username) : that.username != null) return false;
if (authority != null ? !authority.equals(that.authority) : that.authority != null) return false;
return true;
}
@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
result = 31 * result + (authority != null ? authority.hashCode() : 0);
return result;
}
}
hibernate主配置文件
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <mapping class="com.souvc.entity.SouvcAuthoritiesEntity"/> <mapping resource="com/souvc/entity/SouvcAuthoritiesEntity.hbm.xml"/> <mapping resource="com/souvc/entity/SouvcCustomEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcCustomEntity"/> <mapping class="java.lang.String"/> <mapping resource="java/lang/java.lang.String.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcDataDictionaryEntity"/> <mapping resource="com/souvc/entity/SouvcRcDataDictionaryEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcDataDictionaryListEntity"/> <mapping resource="com/souvc/entity/SouvcRcDataDictionaryListEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcEmailAccountInfoEntity"/> <mapping resource="com/souvc/entity/SouvcRcEmailAccountInfoEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcEmailInfoEntity"/> <mapping resource="com/souvc/entity/SouvcRcEmailInfoEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcPermissionEntity"/> <mapping resource="com/souvc/entity/SouvcRcPermissionEntity.hbm.xml"/> <mapping resource="com/souvc/entity/SouvcRcRoleEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcRoleEntity"/> <mapping class="com.souvc.entity.SouvcRcRolePermissionsEntity"/> <mapping resource="com/souvc/entity/SouvcRcRolePermissionsEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcUserEntity"/> <mapping resource="com/souvc/entity/SouvcRcUserEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcUserLoginLogsEntity"/> <mapping resource="com/souvc/entity/SouvcRcUserLoginLogsEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRcUserRoleEntity"/> <mapping resource="com/souvc/entity/SouvcRcUserRoleEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcRoleEntity"/> <mapping resource="com/souvc/entity/SouvcRoleEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcUserEntity"/> <mapping resource="com/souvc/entity/SouvcUserEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcUserRoleEntity"/> <mapping resource="com/souvc/entity/SouvcUserRoleEntity.hbm.xml"/> <mapping class="com.souvc.entity.SouvcUsersEntity"/> <mapping resource="com/souvc/entity/SouvcUsersEntity.hbm.xml"/> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory> </hibernate-configuration>
其他配置文件 、
<?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.souvc.entity.SouvcAuthoritiesEntity" table="authorities" schema="test"> <property name="username"> <column name="username" sql-type="varchar(50)" length="50"/> </property> <property name="authority"> <column name="authority" sql-type="varchar(50)" length="50"/> </property> </class> </hibernate-mapping>

第七步:修正。
如果還沒有符合項目的要求,那么我們可以自己進行修改一下。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微服務(wù)分布式架構(gòu)實現(xiàn)日志鏈路跟蹤的方法
在現(xiàn)有的系統(tǒng)中,由于大量的其他用戶/其他線程的日志也一起輸出穿行其中導(dǎo)致很難篩選出指定請求的全部相關(guān)日志。那我們?nèi)绾蝸硖幚砟??帶著這個問題一起通過本文學(xué)習(xí)下吧2021-08-08
Springboot在有鎖的情況下正確使用事務(wù)的實現(xiàn)代碼
這篇文章主要介紹了Springboot在有鎖的情況下如何正確使用事務(wù),今天通過一個實驗給大家分析一下商品超賣問題,模擬場景分析通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-12-12
spring boot 項目中使用thymeleaf模板的案例分析
這篇文章主要介紹了spring boot 項目中使用thymeleaf模板的案例分析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java并發(fā)編程ThreadLocalRandom類詳解
這篇文章主要介紹了Java并發(fā)編程ThreadLocalRandom類詳解,通過提出問題為什么需要ThreadLocalRandom展開詳情,感興趣的朋友可以參考一下2022-06-06
一文詳解各種ElasticSearch查詢在Java中的實現(xiàn)
Elasticsearch是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是當(dāng)前流行的企業(yè)級搜索引擎,下面這篇文章主要給大家介紹了關(guān)于各種ElasticSearch查詢在Java中實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-11-11

