温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Intellij IDEA怎么通过数据库表生成带注解的实体类

发布时间:2021-02-02 11:45:57 来源:亿速云 阅读:888 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Intellij IDEA怎么通过数据库表生成带注解的实体类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

第一步:新建一个Maven项目。项目的名称为JpaDemo。

我这里是通过idea插件对应的spring项目生成器https://start.spring.io,直接生成项目。如图:

Intellij IDEA怎么通过数据库表生成带注解的实体类

下一步,修改成对应项目的基本信息。如图:

Intellij IDEA怎么通过数据库表生成带注解的实体类

选择相应的依赖jar包。

Intellij IDEA怎么通过数据库表生成带注解的实体类

选择项目的位置

Intellij IDEA怎么通过数据库表生成带注解的实体类

完成创建

Intellij IDEA怎么通过数据库表生成带注解的实体类

温馨提示,之前需要安装好maven。

Intellij IDEA怎么通过数据库表生成带注解的实体类

第二步:配置数据库连接。

选择Mysql

Intellij IDEA怎么通过数据库表生成带注解的实体类

配置数据库基本信息

Intellij IDEA怎么通过数据库表生成带注解的实体类

其实配置了这个数据库连接之后,是可以直接通过脚本进行导出数据库实体类了,但是这个导出的实体类比较简陋,需要进行修改比较多,或是需要自己进行修改生成脚本语句。如:

Intellij IDEA怎么通过数据库表生成带注解的实体类

通过generate POJOs.clj即可导出实体类。

需要选一下实体类放置的地方。

Intellij IDEA怎么通过数据库表生成带注解的实体类

Intellij IDEA怎么通过数据库表生成带注解的实体类

效果如下:

Intellij IDEA怎么通过数据库表生成带注解的实体类

但是以上的实体类没有带注解。那么我们通过项目中用到hibernate,或是jpa需要加注解怎么办,总不能一个个注解加上去吧。idea当然不会这么干啦。

使用IntelliJ IDEA快编码速度:我们程序员的工作不是写程序,而是写程序解决问题。那我们删了之前生成的实体类。我们重新生成一份带注解的实体类。

第三步:配置hibernate文件。

如果没有配置该配置文件,idea则没有显示出生成实体类的工具选项。

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

如图:

Intellij IDEA怎么通过数据库表生成带注解的实体类

第四步:调出idea实体类生成工具。

调出生成实体类的配置工具

Intellij IDEA怎么通过数据库表生成带注解的实体类

保存后。在主面板左侧有persistence,在hibernate图标上点击右键-Generate Persistence Mapping-By Database Scheme。

Intellij IDEA怎么通过数据库表生成带注解的实体类

Intellij IDEA怎么通过数据库表生成带注解的实体类

一开始是没有选中数据源的。

Intellij IDEA怎么通过数据库表生成带注解的实体类

配置选项

(1)数据源选择

(2)生成实体类的位置

(3)实体类的前缀和后缀

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

(5)可以生成hibernate的实体类对应的xml文件

(6)展开表之后可以修改对应之间的类型。

Intellij IDEA怎么通过数据库表生成带注解的实体类

第五步:选中需要执行的数据库表。

Intellij IDEA怎么通过数据库表生成带注解的实体类

第六步:查看导出的效果。

生成过程

Intellij IDEA怎么通过数据库表生成带注解的实体类

导出的结果

可以查看其中的一个实体类,看看效果。

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>

Intellij IDEA怎么通过数据库表生成带注解的实体类

关于“Intellij IDEA怎么通过数据库表生成带注解的实体类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI