Idea 通过数据库表生成实体类详细教程
·
原表
Idea 添加数据源
添加数据库地址 用户名 密码 和 数据库名
修改生成脚本
可复制以下脚本直接替换上图里 Generate POJOs.groovy 文件。(关键点请看 TODO 说明)
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.text.SimpleDateFormat
/* 关键点请看 TODO */
/* 关键点请看 TODO */
/* 关键点请看 TODO */
/* TODO 项目包名 */
packageName = "com.jgyang.test.entity;"
typeMapping = [
(~/(?i)int/) : "long",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp",
(~/(?i)date/) : "java.sql.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def fields = calcFields(table)
/* TODO 需要设置编码,否则,导出的脚本会有乱码*/
new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className, fields,table) }
}
def generate(out, className, fields,table) {
out.println "package $packageName"
/* TODO 手动添加项目需要的包*/
out.println "import lombok.Data;"
out.println "import com.baomidou.mybatisplus.annotation.TableName;"
out.println "import lombok.EqualsAndHashCode;"
out.println "import lombok.experimental.Accessors;"
out.println "import io.swagger.annotations.ApiModel;"
out.println "import java.util.Date;"
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println ""
/* TODO 添加注释*/
out.println "/**\n" +
" * @Description \n" +
" * @Author jgYang\n" +
" * @Date: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"\n" +
" */"
out.println ""
/* TODO
1.添加@Data 注解,使用lombok,所以不生成 get/set 方法
2.需要生成文档,所以使用swagger注解
*/
out.println "@Data"
out.println "@TableName(\""+table.getName()+"\")"
out.println "@EqualsAndHashCode(callSuper = false)"
out.println "@Accessors(chain = true)"
/* TODO 添加表名和表说明*/
out.println "@ApiModel(value=\" " + table.getName()+" \", description=\" "+table.getComment()+" \")"
out.println "public class $className {"
out.println ""
fields.each() {
/* TODO 添加字段属性和注解*/
out.println " @Excel(name = \""+it.commoent+"\")"
out.println " @ApiModelProperty(value = \""+it.commoent+"\")"
out.println " private ${it.type} ${it.name};"
out.println " \n"
}
out.println "}"
}
def calcFields(table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.getDataType().getSpecification())
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
fields += [[
/* TODO name:字段名 type:字段类型 commoent:字段说明*/
name : javaName(col.getName(), false),
type : typeStr,
commoent: col.getComment()]]
}
}
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
生成实体类
生成结果
package com.jgyang.test.entity;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import io.swagger.annotations.ApiModel;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @Description
* @Author jgYang
* @Date: 2020-04-30 15:50:22
*/
@Data
@TableName("app_user")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value=" app_user ", description=" 用户主表 ")
public class AppUser {
@Excel(name = "用户id")
@ApiModelProperty(value = "用户id")
private String id;
@Excel(name = "用户名称")
@ApiModelProperty(value = "用户名称")
private String name;
@Excel(name = "手机号码")
@ApiModelProperty(value = "手机号码")
private String phone;
@Excel(name = "年龄")
@ApiModelProperty(value = "年龄")
private long age;
@Excel(name = "创建人")
@ApiModelProperty(value = "创建人")
private String createBy;
@Excel(name = "创建时间")
@ApiModelProperty(value = "创建时间")
private java.sql.Timestamp createTime;
@Excel(name = "修改人")
@ApiModelProperty(value = "修改人")
private String updateBy;
@Excel(name = "修改时间")
@ApiModelProperty(value = "修改时间")
private java.sql.Timestamp updateTime;
}
更多推荐
已为社区贡献1条内容
所有评论(0)