/JPAEntityGenerator

a swing tool for generating JPA entity, support java and kotlin

Primary LanguageKotlin

前言

笔者在开发springboot项目时,持久层使用JPA来支撑,需要创建大量的实体类,当然市面上如idea旗舰版已经提供了实体生成工具,奈何是付费版本,如果单纯使用这个功能未免太重,并且互联网找了一圈也没有发现比较合适好用的可视化实体生成器JPAEntityGenerator,所以花了几天时间写了这么一个工具,支持java和kotlin语言的实体生成,并且因为是基于swing开发,所以同时支持windows、macos、linux平台运行。目前1.0版本只支持mysql数据源。

工具下载地址

https://github.com/aladdin0414/JPAEntityGenerator/releases

工具运行

需要安装jre1.8或以上环节,下载zip包解压,进入bin目录,在windows平台双击JPAEntityGenerator.bat,linux或macos环境直接运行JPAEntityGenerator文件,注意执行权限。

工具介绍

数据源配置

可以支持多数据源配置,点击File->New,新建数据源

5.png

3.png

输入数据库连接必填字段包括:

  1. Connection name: 连接名称
  2. Hostname: ip地址和端口号,以冒号隔开。如果数据库端口为3306默认,可不填。
  3. Password: 数据库密码

完成后,点击connect按钮,如果提示连接成功,则可以保存该数据源配置。

实体生成

  • 配置完数据库,并连接成功,schema会出现所有待选数据库,选择后下面会出现所有的数据表列表。
  • 选择需要导出的实体语言,java或者kotlin
  • 填写在项目工程中实体存放的packagename,以后后期需要手动修改
  • 填写导出实体文件的目录,目前手动填写
  • 选择需要导出的数据表,可以单选或者多选
  • 执行导出操作,在对应的文件夹folder/schema/connectionname/language 可以看到生成的实体类

4.png

生成的java实体代码示例:

package com.aladdin0414.test.domain;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Objects;

/**
 * Generated by JPAEntityGenerator
 * @date 2021/06/10 17:51:13
 * 
 */
@Entity
@Table(name = "columns_priv", schema = "mysql", catalog = "")
public class ColumnsPriv {
	private String host;
	private String db;
	private String user;
	private String tableName;
	private String columnName;
	private Timestamp timestamp;
	private String columnPriv;

	@Basic
	@Column(name = "Host")
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	@Basic
	@Column(name = "Db")
	public String getDb() {
		return db;
	}

	public void setDb(String db) {
		this.db = db;
	}

	@Id
	@Column(name = "User")
	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	@Basic
	@Column(name = "Table_name")
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	@Basic
	@Column(name = "Column_name")
	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	@Basic
	@Column(name = "Timestamp")
	public Timestamp getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Timestamp timestamp) {
		this.timestamp = timestamp;
	}

	@Basic
	@Column(name = "Column_priv")
	public String getColumnPriv() {
		return columnPriv;
	}

	public void setColumnPriv(String columnPriv) {
		this.columnPriv = columnPriv;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		ColumnsPriv columnsPriv = (ColumnsPriv) o;
		return Objects.equals(host, columnsPriv.host) && Objects.equals(db, columnsPriv.db) && Objects.equals(user, columnsPriv.user) && Objects.equals(tableName, columnsPriv.tableName) && Objects.equals(columnName, columnsPriv.columnName) && Objects.equals(timestamp, columnsPriv.timestamp) && Objects.equals(columnPriv, columnsPriv.columnPriv);
	}

	@Override
	public int hashCode() {
		return Objects.hash(host, db, user, tableName, columnName, timestamp, columnPriv);
	}
}

kotlin代码示例:

package com.aladdin0414.test.domain;

import java.sql.Timestamp
import java.util.*
import javax.persistence.*

/**
 * Generated by JPAEntityGenerator
 * @date 2021/06/10 17:51:18
 */
@Entity
@Table(name = "columns_priv", schema = "mysql", catalog = "")
class ColumnsPriv {

	@Column(name = "Host")
	@Basic
	var host: String? = null

	@Column(name = "Db")
	@Basic
	var db: String? = null

	@Column(name = "User")
	@Id
	var user: String? = null

	@Column(name = "Table_name")
	@Basic
	var tableName: String? = null

	@Column(name = "Column_name")
	@Basic
	var columnName: String? = null

	@Column(name = "Timestamp")
	@Basic
	var timestamp: Timestamp? = null

	@Column(name = "Column_priv")
	@Basic
	var columnPriv: String? = null

	override fun equals(o: Any?): Boolean {
		if (this === o) return true
		if (o == null || javaClass != o.javaClass) return false
		val columnsPriv = o as ColumnsPriv
		return host == columnsPriv.host && db == columnsPriv.db && user == columnsPriv.user && tableName == columnsPriv.tableName && columnName == columnsPriv.columnName && timestamp == columnsPriv.timestamp && columnPriv == columnsPriv.columnPriv
	}

	override fun hashCode(): Int {
		return Objects.hash(host, db, user, tableName, columnName, timestamp, columnPriv)
	}
}

开源地址

目前该项目在github上开源,基于kotlin语言编写,有兴趣的朋友可以看看

https://github.com/aladdin0414/JPAEntityGenerator