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

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

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

選擇項(xiàng)目的位置

完成創(chuàng)建

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

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

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

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

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


效果如下:

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

配置一下hibernate配置文件。
在資源文件下新建一個(gè)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實(shí)體類生成工具。
調(diào)出生成實(shí)體類的配置工具

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


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

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

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

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

導(dǎo)出的結(jié)果
可以查看其中的一個(gè)實(shí)體類,看看效果。
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>

第七步:修正。
如果還沒有符合項(xiàng)目的要求,那么我們可以自己進(jìn)行修改一下。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個(gè)關(guān)鍵的接口,用于創(chuàng)建和管理對(duì)象實(shí)例,它們?cè)赟pring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11
學(xué)習(xí)Java多線程之線程定義、狀態(tài)和屬性
這篇文章主要為大家詳細(xì)介紹了Java多線程之線程定義、狀態(tài)和屬性,感興趣的小伙伴們可以參考一下2016-02-02
淺談Java中SimpleDateFormat 多線程不安全原因
SimpleDateFormat是Java中用于日期時(shí)間格式化的一個(gè)類,本文主要介紹了淺談Java中SimpleDateFormat 多線程不安全原因,感興趣的可以了解一下2024-01-01
基于mybatis中<include>標(biāo)簽的作用說明
這篇文章主要介紹了基于mybatis中<include>標(biāo)簽的作用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Maven坐標(biāo)和依賴的實(shí)現(xiàn)示例
本文主要介紹了Maven坐標(biāo)和依賴的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04

