這篇文章將為大家詳細(xì)講解有關(guān)Intellij IDEA怎么通過(guò)數(shù)據(jù)庫(kù)表生成帶注解的實(shí)體類,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)專注于稱多網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供稱多營(yíng)銷型網(wǎng)站建設(shè),稱多網(wǎng)站制作、稱多網(wǎng)頁(yè)設(shè)計(jì)、稱多網(wǎng)站官網(wǎng)定制、成都小程序開(kāi)發(fā)服務(wù),打造稱多網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供稱多網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
第一步:新建一個(gè)Maven項(xiàng)目。項(xiàng)目的名稱為JpaDemo。
我這里是通過(guò)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ù)庫(kù)連接。
選擇MySQL。
配置數(shù)據(jù)庫(kù)基本信息
其實(shí)配置了這個(gè)數(shù)據(jù)庫(kù)連接之后,是可以直接通過(guò)腳本進(jìn)行導(dǎo)出數(shù)據(jù)庫(kù)實(shí)體類了,但是這個(gè)導(dǎo)出的實(shí)體類比較簡(jiǎn)陋,需要進(jìn)行修改比較多,或是需要自己進(jìn)行修改生成腳本語(yǔ)句。如:
通過(guò)generate POJOs.clj即可導(dǎo)出實(shí)體類。
需要選一下實(shí)體類放置的地方。
效果如下:
但是以上的實(shí)體類沒(méi)有帶注解。那么我們通過(guò)項(xiàng)目中用到hibernate,或是jpa需要加注解怎么辦,總不能一個(gè)個(gè)注解加上去吧。idea當(dāng)然不會(huì)這么干啦。
使用IntelliJ IDEA快編碼速度:我們程序員的工作不是寫(xiě)程序,而是寫(xiě)程序解決問(wèn)題。那我們刪了之前生成的實(shí)體類。我們重新生成一份帶注解的實(shí)體類。
第三步:配置hibernate文件。
如果沒(méi)有配置該配置文件,idea則沒(méi)有顯示出生成實(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。
一開(kāi)始是沒(méi)有選中數(shù)據(jù)源的。
配置選項(xiàng)
(1)數(shù)據(jù)源選擇
(2)生成實(shí)體類的位置
(3)實(shí)體類的前綴和后綴
(4)可以全選表,或是全不選表
(5)可以生成hibernate的實(shí)體類對(duì)應(yīng)的xml文件
(6)展開(kāi)表之后可以修改對(duì)應(yīng)之間的類型。
第五步:選中需要執(zhí)行的數(shù)據(jù)庫(kù)表。
第六步:查看導(dǎo)出的效果。
生成過(guò)程
導(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>
關(guān)于“Intellij IDEA怎么通過(guò)數(shù)據(jù)庫(kù)表生成帶注解的實(shí)體類”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文名稱:IntellijIDEA怎么通過(guò)數(shù)據(jù)庫(kù)表生成帶注解的實(shí)體類
網(wǎng)站鏈接:http://chinadenli.net/article24/ihisce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站策劃、微信公眾號(hào)、網(wǎng)站收錄、定制網(wǎng)站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)