1. 程式人生 > 程式設計 >Intellij IDEA 如何通過資料庫表生成帶註解的實體類(圖文詳細教程)

Intellij IDEA 如何通過資料庫表生成帶註解的實體類(圖文詳細教程)

第一步:新建一個Maven專案。專案的名稱為JpaDemo。

我這裡是通過idea外掛對應的spring專案生成器https://start.spring.io,直接生成專案。如圖:

下一步,修改成對應專案的基本資訊。如圖:

選擇相應的依賴jar包。

選擇專案的位置

完成建立

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

第二步:配置資料庫連線。

選擇Mysql。

配置資料庫基本資訊

其實配置了這個資料庫連線之後,是可以直接通過指令碼進行匯出資料庫實體類了,但是這個匯出的實體類比較簡陋,需要進行修改比較多,或是需要自己進行修改生成指令碼語句。如:

通過generate POJOs.clj即可匯出實體類。

需要選一下實體類放置的地方。

效果如下:

但是以上的實體類沒有帶註解。那麼我們通過專案中用到hibernate,或是jpa需要加註解怎麼辦,總不能一個個註解加上去吧。idea當然不會這麼幹啦。

使用IntelliJ IDEA快編碼速度:我們程式設計師的工作不是寫程式,而是寫程式解決問題。那我們刪了之前生成的實體類。我們重新生成一份帶註解的實體類。

第三步:配置hibernate檔案。

如果沒有配置該配置檔案,idea則沒有顯示出生成實體類的工具選項。

配置一下hibernate配置檔案。

在資原始檔下新建一個hibernate.cfg.xml配置檔案。並輸入以下內容。

<?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>

如圖:

第四步:調出idea實體類生成工具。

調出生成實體類的配置工具

儲存後。在主面板左側有persistence,在hibernate圖示上點選右鍵-Generate Persistence Mapping-By Database Scheme。

一開始是沒有選中資料來源的。

配置選項

(1)資料來源選擇

(2)生成實體類的位置

(3)實體類的字首和字尾

(4)可以全選表,或是全不選表

(5)可以生成hibernate的實體類對應的xml檔案

(6)展開表之後可以修改對應之間的型別。

第五步:選中需要執行的資料庫表。

第六步:檢視匯出的效果。

生成過程

匯出的結果

可以檢視其中的一個實體類,看看效果。

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",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>

第七步:修正。

如果還沒有符合專案的要求,那麼我們可以自己進行修改一下。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。