现在的位置: 首页 > 编程语言 > 正文

IntellijIDEA如何通过数据库表生成带注解的实体类(图文详细教程)

2020年02月13日 编程语言 ⁄ 共 6731字 ⁄ 字号 评论关闭

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

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

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

选择相应的依赖jar包。

选择项目的位置

完成创建

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

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

选择Mysql。

配置数据库基本信息

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

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

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

效果如下:

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

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

第三步:配置hibernate文件。

如果没有配置该配置文件,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>

如图:

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

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

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

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

配置选项

(1)数据源选择

(2)生成实体类的位置

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

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

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

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

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

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

生成过程

导出的结果

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

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;}@Overridepublic 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;}@Overridepublic 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 如何通过数据库表生成带注解的实体类(图文详细教程)

以上就上有关IntellijIDEA如何通过数据库表生成带注解的实体类(图文详细教程)的相关介绍,要了解更多idea数据库生成实体类,ideaidea数据库表生成注解实体类内容请登录学步园。

抱歉!评论已关闭.