格式效果展示
脚本如下
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.text.SimpleDateFormat
/* 关键点请看 TODO */
/* TODO 项目包名 */
packageName = "***.***.***.***.domain;"
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 com.baomidou.mybatisplus.annotation.TableName;"
out.println "import io.swagger.annotations.ApiModel;"
out.println ""
/* TODO 添加注释*/
out.println "/**\n" +
" * @Description \n" +
" * @Author ***\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.annos +"\")"
out.println " @ApiModelProperty(value = \""+it.name+"\")"
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()]]
// }
//}
/**
* 数据
* @param table
* @return
*/
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 annos = "\t@Column(name = \"" + col.getName() + "\" )";
def annos = col.getName() ;
if (null != col.getComment() && "" != col.getComment()) {
annos = "\t@Column(name = \"" + col.getName() + "\" , columnDefinition = \"" + col.getComment() + "\'\")";
}
// if ("id".equals(Case.LOWER.apply(col.getName()))) {
// annos = "\t@Id \r" + annos
// }
def comm = [
colName : col.getName(),
name : javaName(col.getName(), false),
type : typeStr,
commoent: col.getComment(),
annos : annos]
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]
}