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當然不會這么干啦。
使用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圖標上點擊右鍵-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)文章
Spring中的FactoryBean與BeanFactory詳細解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細解析,在Spring框架中,FactoryBean和BeanFactory是兩個關(guān)鍵的接口,用于創(chuàng)建和管理對象實例,它們在Spring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11學(xué)習(xí)Java多線程之線程定義、狀態(tài)和屬性
這篇文章主要為大家詳細介紹了Java多線程之線程定義、狀態(tài)和屬性,感興趣的小伙伴們可以參考一下2016-02-02淺談Java中SimpleDateFormat 多線程不安全原因
SimpleDateFormat是Java中用于日期時間格式化的一個類,本文主要介紹了淺談Java中SimpleDateFormat 多線程不安全原因,感興趣的可以了解一下2024-01-01