最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Intellij IDEA 如何通過數(shù)據(jù)庫表生成帶注解的實體類(圖文詳細(xì)教程)

 更新時間:2019年11月22日 09:35:34   作者:悠悠-我心  
這篇文章主要介紹了Intellij IDEA 如何通過數(shù)據(jù)庫表生成帶注解的實體類(圖文詳細(xì)教程),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

第一步:新建一個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ù)庫連接之后,是可以直接通過腳本進(jìn)行導(dǎo)出數(shù)據(jù)庫實體類了,但是這個導(dǎo)出的實體類比較簡陋,需要進(jìn)行修改比較多,或是需要自己進(jìn)行修改生成腳本語句。如:

通過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>

第七步:修正。

如果還沒有符合項目的要求,那么我們可以自己進(jìn)行修改一下。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot使用jsr303校驗的實現(xiàn)

    SpringBoot使用jsr303校驗的實現(xiàn)

    這篇文章主要介紹了SpringBoot使用jsr303校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 微服務(wù)分布式架構(gòu)實現(xià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ù)的實現(xiàn)代碼

    這篇文章主要介紹了Springboot在有鎖的情況下如何正確使用事務(wù),今天通過一個實驗給大家分析一下商品超賣問題,模擬場景分析通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • spring boot 項目中使用thymeleaf模板的案例分析

    spring boot 項目中使用thymeleaf模板的案例分析

    這篇文章主要介紹了spring boot 項目中使用thymeleaf模板的案例分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Dubbo負(fù)載均衡策略介紹

    Dubbo負(fù)載均衡策略介紹

    負(fù)載均衡改善了跨多個計算資源(例如計算機,計算機集群,網(wǎng)絡(luò)鏈接,中央處理單元或磁盤驅(qū)動的的工作負(fù)載分布。負(fù)載平衡旨在優(yōu)化資源使用,最大化吞吐量,最小化響應(yīng)時間,并避免任何單個資源的過載
    2022-09-09
  • Java并發(fā)編程ThreadLocalRandom類詳解

    Java并發(fā)編程ThreadLocalRandom類詳解

    這篇文章主要介紹了Java并發(fā)編程ThreadLocalRandom類詳解,通過提出問題為什么需要ThreadLocalRandom展開詳情,感興趣的朋友可以參考一下
    2022-06-06
  • 一文詳解各種ElasticSearch查詢在Java中的實現(xiàn)

    一文詳解各種ElasticSearch查詢在Java中的實現(xiàn)

    Elasticsearch是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是當(dāng)前流行的企業(yè)級搜索引擎,下面這篇文章主要給大家介紹了關(guān)于各種ElasticSearch查詢在Java中實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Java?mybatis?開發(fā)自定義插件

    Java?mybatis?開發(fā)自定義插件

    這篇文章主要介紹了Java?mybatis開發(fā)自定義插件,MyBatis允許你在映射語句執(zhí)行過程中的某一點進(jìn)行攔截調(diào)用,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • SpringBoot常見問題小結(jié)

    SpringBoot常見問題小結(jié)

    這篇文章主要介紹了SpringBoot常見問題小結(jié),需要的朋友可以參考下
    2017-07-07
  • springboot整合couchbase集群的步驟

    springboot整合couchbase集群的步驟

    couchbase是一款開源的,分布式的nosql數(shù)據(jù)庫,主要用于分布式緩存和數(shù)據(jù)存儲領(lǐng)域,本文給大家介紹springboot整合couchbase集群的步驟,感興趣的朋友一起看看吧
    2025-03-03

最新評論

泽库县| 旅游| 秦皇岛市| 开远市| 南开区| 沾益县| 波密县| 象山县| 无极县| 卢氏县| 犍为县| 西吉县| 鹤岗市| 古丈县| 绿春县| 南华县| 乌兰浩特市| 绍兴县| 攀枝花市| 商南县| 日喀则市| 云林县| 伊吾县| 望谟县| 会泽县| 福安市| 香河县| 福泉市| 怀宁县| 吴川市| 常山县| 绥棱县| 吕梁市| 商城县| 无极县| 桓台县| 巴楚县| 成武县| 广安市| 石阡县| 新竹县|