目录
3,在文件夹下新建新建MyGenerate POJOs.groovy
6,想要有@Data,@Entity和@ Column注释,请加入lombok和persistence-api依赖
1,idea连接mysql数据库
2,右键数据库表格,打开代码生成器规则文件地址
3,在文件夹下新建新建MyGenerate POJOs.groovy
4,自定义MyGenerate POJOs.groovy
import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
/*
* Available context bindings:
* SELECTION Iterable
* PROJECT project
* FILES files helper
* update by yitianRen 20200324
*/
packageName = ""
typeMapping = [
(~/(?i)int/) : "Integer", //数据库类型和Jave类型映射关系
(~/(?i)float|double|decimal|real/): "Double",
(~/(?i)bool|boolean/) : "Boolean",
(~/(?i)datetime|timestamp/) : "Date",
(~/(?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 tablecomment = javaName(table.getComment(), true)
def className1 = javaName(table.getName(), true)
def className = className1.drop(1) +"Vo"
packageName = getPackageName(dir)
def fields = calcFields(table)//更改实体生成规则
PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, className + ".java")), "utf-8"))
output.withPrintWriter { out -> generate(out, className,tablecomment, fields,table) }//输出实体类
// new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) }
}
// 获取包所在文件夹路径
def getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
def generate(out, className,tablecomment, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import lombok.Data;"
out.println "import com.baomidou.mybatisplus.annotation.TableName;"
out.println "import com.baomidou.mybatisplus.annotation.IdType;"
out.println "import com.baomidou.mybatisplus.annotation.TableId;"
out.println "import lombok.AllArgsConstructor;"
out.println "import lombok.Builder;"
out.println "import lombok.NoArgsConstructor;"
out.println "import io.swagger.annotations.ApiModel;"
out.println "import io.swagger.annotations.ApiModelProperty;"
Set types = new HashSet()
fields.each() {
types.add(it.type)
}
if (types.contains("Date")) {
out.println "import java.util.Date;"
}
if (types.contains("InputStream")) {
out.println "import java.io.InputStream;"
}
out.println "/**\n" +
" * \n" +
" * $tablecomment\n" +
" *\n" +
" * @author ch\n" +
" */"
out.println ""
out.println "@Data"
out.println "@Builder"
out.println "@AllArgsConstructor"
out.println "@NoArgsConstructor"
out.println "@TableName(value = \"${table.toString().replaceAll("table:","")}\")"
out.println "@ApiModel(value = \"$className\", description = \"$tablecomment\")"
out.println "public class $className{"
out.println ""
fields.each() {
out.println ""
// 输出注释 这里和下面的 comm是一一对应的
/* if (isNotEmpty(it.commoent)) {//定义非空校验,it.commoent != ""手写无效
out.println "// ${it.commoent}"
}*/
if (it.annos != "") /*out.println " ${it.annos}"*/
out.println " @ApiModelProperty( value=\"${it.commoent}\")"
out.println " private ${it.type} ${it.name};"
}
out.println ""
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
def comm = [
tablecomment : table.getComment(),
name : javaName(col.getName(), false),
type : typeStr,
commoent: col.getComment(),
// annos: "@Column(name = \"" + col.getName() + "\" )"
]
//对于表中主键自定义注解
if ("pk".equals(Case.LOWER.apply(col.getName()))){
// comm.annos = "\t@Id\n"
//自增主键需要
// comm.annos += "@Column(name = \"" + col.getName() + "\")"
}
fields += [comm]//字段对照
}
}
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]
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
这工具好用的就像她
5,通过自定义规则生成代码
6,想要有@Data,@Entity和@ Column注释,请加入lombok和persistence-api依赖
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
7,生成实体类效果如下
package com.bim.model.manage.platform.vo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* 平台用户
*
* @author ch
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "UserVo", description = "平台用户")
public class UserVo {
@TableId(type = IdType.ASSIGN_ID)
@ApiModelProperty(value = "主键id")
private String id;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "编号")
private String code;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "角色id")
private String roleId;
@ApiModelProperty(value = "创建人id")
private String createdById;
@ApiModelProperty(value = "创建人名称")
private String createdByName;
@ApiModelProperty(value = "创建时间")
private Date createdTime;
@ApiModelProperty(value = "更新人id")
private String updatedById;
@ApiModelProperty(value = "更新时间")
private Date updatedByTime;
@ApiModelProperty(value = "更新人名称")
private String updatedByName;
@ApiModelProperty(value = "邮箱")
private String email;
@ApiModelProperty(value = "手机号")
private String phone;
@ApiModelProperty(value = "岗位id")
private String postId;
@ApiModelProperty(value = "部门id")
private String deptId;
}
压缩包里面包含:
Generate POJOs.groovy、My Generate POJOs.groovy、MyDto Generate POJOs.groovy、MyInsertDto Generate POJOs.groovy、MyUpdateDto Generate POJOs.groovy、MyVO Generate POJOs.groovy
即可放在文件夹下即可生成实体类,下载后有需要改动可咨询VX:ch1633105493
8.文件下载处:
https://download.csdn.net/download/qq_40800950/20530257?spm=1001.2014.3001.5501