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.text.SimpleDateFormat
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = ""
typeMapping = [
(~/(?i)tinyint|smallint|mediumint|int/) : "Integer",
(~/(?i)bigint/) : "Long",
(~/(?i)bool|bit/) : "Boolean",
(~/(?i)float/) : "Float",
(~/(?i)decimal/) : "BigDecimal",
(~/(?i)double|real/) : "Double",
(~/(?i)datetime/) : "LocalDateTime",
(~/(?i)date/) : "LocalDate",
(~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def fields = calcFields(table)
packageName = getPackageName(dir)
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, className + ".java")), "UTF-8"))
printWriter.withPrintWriter { out -> generate(out, className, fields, table) }
}
// 获取包所在文件夹路径
def getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import com.ciic.domain.Insert;"
out.println "import com.ciic.domain.Update;"
out.println "import io.swagger.annotations.ApiModel;"
out.println "import io.swagger.annotations.ApiModelProperty;"
out.println "import lombok.Data;"
out.println "import lombok.EqualsAndHashCode;"
out.println "import lombok.experimental.Accessors;"
out.println ""
out.println "import java.io.Serializable;"
out.println "import javax.validation.constraints.NotNull;"
Set types = new HashSet()
fields.each() {
types.add(it.type)
}
if (types.contains("Date") || types.contains("timestamp")) {
out.println "import java.time.LocalDate;"
out.println "import java.time.LocalDateTime;"
}
if (types.contains("InputStream")) {
out.println "import java.io.InputStream;"
}
if (types.contains("BigDecimal")) {
out.println "import java.math.BigDecimal;"
}
out.println ""
out.println "/**\n" +
" * <p> \n" +
" * ${table.comment}\n" +
" * </p> \n" +
" * \n" +
" * @author dingjm\n" +
" * @since " + new SimpleDateFormat("yyyy/MM/dd").format(new Date()) + " \n" +
" */"
out.println "@Data"
out.println "@EqualsAndHashCode(callSuper = false)"
out.println "@Accessors(chain = true)"
out.println "@ApiModel(value = \"${className}对象\", description = \"${table.comment}\")"
out.println "public class ${className} implements Serializable {"
out.println ""
out.println genSerialID()
int index = 0;
fields.each() {
index++;
out.println ""
if (index == 1) {
out.println " @NotNull(message = \"${it.comment}不能为空\", groups = { Update.class})"
out.println " @TableId(value = \"${it.colName}\", type = IdType.AUTO)"
}
// 输出注释
if (isNotEmpty(it.comment)) {
out.println "\t@ApiModelProperty(value = \"${it.comment.toString()}\")"
}
if (it.name == "isDelete") {
out.println "\t@TableLogic"
}
if (it.name == "createTime" || it.name == "createUser" || it.name == "createUserName") {
out.println "\t@TableField(fill = FieldFill.INSERT)"
}
if (it.name == "modifyTime" || it.name == "modifyUser" || it.name == "modifyUserName") {
out.println "\t@TableField(fill = FieldFill.UPDATE)"
}
if (it.annos != "") out.println "\t${it.annos.replace("@Id", "")}"
// 输出成员变量
out.println "\tprivate ${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 = [
colName: col.getName(),
name : javaName(col.getName(), false),
type : typeStr,
comment: col.getComment(),
annos : ""]
//annos : "\t@Column(name = \"" + col.getName() + "\" )"]
if ("id".equals(Case.LOWER.apply(col.getName())))
comm.annos += "@TableId(type = IdType.AUTO)"
fields += [comm]
}
}
// 处理类名(这里是因为我的表都是以t_命名的,所以需要处理去掉生成类名时的开头的T,
// 如果你不需要那么请查找用到了 javaClassName这个方法的地方修改为 javaName 即可)
def javaClassName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
// 去除开头的T http://developer.51cto.com/art/200906/129168.htm
s = s[1..s.size() - 1]
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
def javaName(str, capitalize) {
// def s = str.split(/(?<=[^\p{IsLetter}])/).collect { Case.LOWER.apply(it).capitalize() }
// .join("").replaceAll(/[^\p{javaJavaIdentifierPart}]/, "_")
// capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
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
}
static String changeStyle(String str, boolean toCamel) {
if (!str || str.size() <= 1)
return str
if (toCamel) {
String r = str.toLowerCase().split('_').collect { cc -> Case.LOWER.apply(cc).capitalize() }.join('')
return r[0].toLowerCase() + r[1..-1]
} else {
str = str[0].toLowerCase() + str[1..-1]
return str.collect { cc -> ((char) cc).isUpperCase() ? '_' + cc.toLowerCase() : cc }.join('')
}
}
static String genSerialID() {
//return "\tprivate static final long serialVersionUID = " + Math.abs(new Random().nextLong()) + "L;"
return "\tprivate static final long serialVersionUID = 1L;"
}
package com.ciic.domain.job;
import com.baomidou.mybatisplus.annotation.*;
import com.ciic.domain.Update;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 服务类别表
* </p>
*
* @author dingjm
* @since 2022/12/07
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "JobServiceCategory对象", description = "服务类别表")
public class JobServiceCategory implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull(message = "主键ID不能为空", groups = { Update.class})
@TableId(value = "category_id", type = IdType.AUTO)
@ApiModelProperty(value = "主键ID")
private Integer categoryId;
@ApiModelProperty(value = "类别名称")
private String categoryName;
@ApiModelProperty(value = "级别")
private Integer categoryLevel;
@ApiModelProperty(value = "上级ID")
private Integer categoryParentId;
@ApiModelProperty(value = "是否锁定:0:未锁定 1:锁定")
private Integer isLock;
@ApiModelProperty(value = "是否删除;0:未删除,1:已删除")
@TableLogic
private Integer isDelete;
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@ApiModelProperty(value = "创建人ID")
@TableField(fill = FieldFill.INSERT)
private Integer createUser;
@ApiModelProperty(value = "修改时间")
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime modifyTime;
@ApiModelProperty(value = "修改人ID")
@TableField(fill = FieldFill.UPDATE)
private Integer modifyUser;
}