importcom.intellij.database.model.DasTable
importcom.intellij.database.util.Case
importcom.intellij.database.util.DasUtil
importjava.io.*
importjava.text.SimpleDateFormat
importcom.intellij.database.model.ObjectKind
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName="com.cscec;"
typeMapping= [
(~/(?i)tinyint|smallint|mediumint/) :"Integer",
(~/(?i)int/) :"Integer",
(~/(?i)bool|bit/) :"Boolean",
(~/(?i)float|double|real/) :"Double",
(~/(?i)decimal/) :"BigDecimal",
(~/(?i)datetime|timestamp|date|time/) :"Date",
(~/(?i)blob|binary|bfile|clob|raw|image/):"InputStream",
(~/(?i)number/) :"Integer",
(~/(?i)/) :"String"
]
FILES.chooseDirectoryAndSave("Choose directory","Choose where to store generated files"){dir->
SELECTION.filter{itinstanceofDasTable}.each{generate(it, dir)}
}
defgenerate(table, dir) {
defclassName= javaName(table.getName(),true)
deffields= calcFields(table)
packageName= getPackageName(dir)
newFile(dir,className+".java").withPrintWriter("utf-8"){out->generate(out,className,fields, table)}
}
//获取包所在文件夹路径
defgetPackageName(dir) {
returndir.toString().replaceAll("\\\\",".").replaceAll("/",".").replaceAll("^.*src(\\.main\\.java\\.)?","") +";"
}
defgenerate(out, className, fields, table) {
out.println"package$packageName"
out.println""
// out.println "import com.baomidou.mybatisplus.annotation.TableField;"
out.println"import io.swagger.annotations.ApiModelProperty;"
out.println"import com.baomidou.mybatisplus.annotation.TableId;"
out.println"import com.baomidou.mybatisplus.annotation.TableName;"
out.println"import lombok.Data;"
Set types=newHashSet()
fields.each(){
types.add(it.type)
}
if(types.contains("Date")) {
out.println""
out.println"import java.util.Date;"
}
if(types.contains("InputStream")) {
out.println"import java.io.InputStream;"
}
out.println""
out.println"/**"
out.println" * @author zhangkepeng"
out.println" * @date "+newSimpleDateFormat("yyyy-MM-dd").format(newDate())
out.println" */"
out.println"@Data"
out.println"@TableName(\""+ table.getName() +"\")"
out.println"public class$className{"
out.println""
fields.each(){
//输出注释
if(isNotEmpty(it.commoent)) {
out.println"\t/**"
out.println"\t*${it.commoent.toString()}"
out.println"\t*/"
}
if(it.annos !="") out.println"${it.annos}"
out.println"\tprivate${it.type}${it.name};"
}
out.println""
out.println"}"
}
defisNotEmpty(content) {
returncontent !=null&& content.toString().trim().length() >0
}
defcalcFields(table) {
DasUtil.getColumns(table).reduce([]){fields, col->
defspec= Case.LOWER.apply(col.getDataType().getSpecification())
deftypeStr=typeMapping.find{p, t->p.matcher(spec).find()}.value
defcomm= [
colName: col.getName(),
name: javaName(col.getName(),false),
type:typeStr,
commoent: col.getComment(),
// annos : "\t@TableField(\""+col.getName()+"\" )"
annos:"\t@ApiModelProperty(\""+col.getComment()+"\")"
]
if("id".equals(Case.LOWER.apply(col.getName())))
comm.annos="\t@TableId(value =\"id\")"
fields += [comm]
}
}
defjavaName(str, capitalize) {
defs= 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]
}