groovy程式碼
import com.intellij.database.model.DasTable
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 = ""
dtoPackageName = ""
daoPackageName = ""
servicePackageName = ""
serviceImplPackageName = ""
entityImport = ""
daoImport = ""
serviceImport = ""
baseEntityDir = ""
typeMapping = [
(~/(?i)int|number/) : "Long",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp/) : "Timestamp",
(~/(?i)date/) : "Date",
(~/(?i)time/) : "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) {
/*entity*/
def tableName = table.getName()
def className = javaName(tableName, true) + "Entity"
def fields = calcFields(table)
def entityDir = dir.toString() + "\\model\\entity"
def entityBaseDir = "net\\core\\base"
packageName = entityDir.replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
baseEntityDir = entityBaseDir.replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "")+".BaseEntity;"
new File(entityDir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className, fields, tableName, table.getComment()) }
entityImport = entityDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + "." + className + ";"
/*DTO*/
def dtoDir = dir.toString() + "\\model\\dto"
def dtoClassName = javaName(tableName, true) + "DTO"
dtoPackageName = dtoDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
new File(dtoDir, dtoClassName + ".java").withPrintWriter("utf-8") { out -> generateDTO(out, className, dtoClassName, table.getComment()) }
/*DAO*/
def daoDir = dir.toString() + "\\dao"
def daoClassName = javaName(tableName, true) + "Dao"
daoPackageName = daoDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
new File(daoDir, daoClassName + ".java").withPrintWriter("utf-8") { out -> generateDAO(out, className, daoClassName, table.getComment()) }
daoImport = daoDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + "." + daoClassName + ";"
/*Service*/
def serviceDir = dir.toString() + "\\service"
def serviceClassName = javaName(tableName, true) + "Service"
servicePackageName = serviceDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
new File(serviceDir, serviceClassName + ".java").withPrintWriter("utf-8") { out -> generateService(out, className, serviceClassName, table.getComment()) }
serviceImport = serviceDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + "." + serviceClassName + ";"
/*ServiceImpl*/
def serviceImplDir = dir.toString() + "\\service\\impl"
def serviceImplClassName = javaName(tableName, true) + "ServiceImpl"
serviceImplPackageName = serviceImplDir.replaceAll("\\\\", ".")
.replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
new File(serviceImplDir, serviceImplClassName + ".java").withPrintWriter("utf-8") { out -> generateServiceImpl(out, className, serviceImplClassName, serviceClassName, daoClassName, table.getComment()) }
}
def generateDAO(out, entityClassName, daoClassName, tableComment){
out.println "package $daoPackageName"
out.println ""
out.println "import io.swagger.annotations.ApiModelProperty;"
out.println ""
out.println "import $entityImport"
out.println "import com.baomidou.mybatisplus.core.mapper.BaseMapper;"
out.println "import org.apache.ibatis.annotations.Mapper;"
out.println ""
out.println "/**\n" +
" * <P><B>Description</B>$tableComment Dao</P>\n" +
" * RevisionTrail:(Date/Author/Description)\n" +
" * " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()) + " CREATE\n" +
" *\n" +
" * @author Ryan Huang\n" +
" * @version 1.0\n" +
" */"
out.println "@Mapper"
out.println "public interface $daoClassName extends BaseMapper<$entityClassName> {"
out.println "}"
}
def generateService(out, entityClassName, serviceClassName, tableComment){
out.println "package $servicePackageName"
out.println ""
out.println "import $entityImport"
out.println "import com.baomidou.mybatisplus.extension.service.IService;"
out.println ""
out.println "/**\n" +
" * <P><B>Description</B>$tableComment Service</P>\n" +
" * RevisionTrail:(Date/Author/Description)\n" +
" * " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()) + " CREATE\n" +
" *\n" +
" * @author Ryan Huang\n" +
" * @version 1.0\n" +
" */"
out.println "public interface $serviceClassName extends IService<$entityClassName> {"
out.println "}"
}
def generateServiceImpl(out, entityClassName, serviceImplClassName, serviceClassName, daoClassName, tableComment){
out.println "package $serviceImplPackageName"
out.println "import $entityImport"
out.println "import org.springframework.stereotype.Service;"
out.println "import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;"
out.println "import $daoImport"
out.println "import $serviceImport"
out.println ""
out.println "/**\n" +
" * <P><B>Description</B>$tableComment ServiceImpl</P>\n" +
" * RevisionTrail:(Date/Author/Description)\n" +
" * " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()) + " CREATE\n" +
" *\n" +
" * @author Ryan Huang\n" +
" * @version 1.0\n" +
" */"
out.println "public class $serviceImplClassName extends ServiceImpl<$daoClassName, $entityClassName> implements $serviceClassName {"
out.println "}"
}
def generateDTO(out, entityClassName, dtoClassName, tableComment) {
out.println "package $dtoPackageName"
out.println ""
out.println "import com.baomidou.mybatisplus.annotation.TableName;"
out.println "import io.swagger.annotations.ApiModel;"
out.println ""
out.println "import $entityImport"
out.println ""
out.println "/**\n" +
" * <P><B>Description</B>$tableComment DTO</P>\n" +
" * RevisionTrail:(Date/Author/Description)\n" +
" * " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()) + " CREATE\n" +
" *\n" +
" * @author Ryan Huang\n" +
" * @version 1.0\n" +
" */"
out.println "@ApiModel(value = \"$tableComment" + "DTO\")"
out.println "public class $dtoClassName extends $entityClassName {"
out.println "}"
}
def generate(out, className, fields, tableName, tableComment) {
out.println "package $packageName"
out.println ""
out.println "import com.baomidou.mybatisplus.annotation.TableName;"
out.println "import com.baomidou.mybatisplus.annotation.TableField;"
out.println "import io.swagger.annotations.ApiModelProperty;"
out.println "import lombok.Getter;"
out.println "import lombok.Setter;"
out.println "import $baseEntityDir"
out.println ""
out.println "import java.sql.Timestamp;"
out.println "import java.util.Date;"
out.println "import java.sql.Time;"
out.println ""
out.println "/**\n" +
" * <P><B>Description</B>$tableComment</P>\n" +
" * RevisionTrail:(Date/Author/Description)\n" +
" * " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()) + " CREATE\n" +
" *\n" +
" * @author Ryan Huang\n" +
" * @version 1.0\n" +
" */"
out.println "@Getter"
out.println "@Setter"
out.println "@TableName(\"$tableName\")"
out.println "public class ${className} extends BaseEntity {"
fields.each() {
if ("id".compareToIgnoreCase(it.name) != 0
&& "Creator".compareToIgnoreCase(it.name) != 0
&& "Crtname".compareToIgnoreCase(it.name) != 0
&& "Create".compareToIgnoreCase(it.name) != 0
&& "Modifiedby".compareToIgnoreCase(it.name) != 0
&& "Mfyname".compareToIgnoreCase(it.name) != 0
&& "Modified".compareToIgnoreCase(it.name) != 0
&& "Status".compareToIgnoreCase(it.name) != 0) {
if (it.annos != "") out.println " ${it.annos}"
out.println ""
out.println " /**"
out.println " * ${it.commoent.toString()}"
out.println " */"
if (isNotEmpty(it.commoent)) {
out.println " @ApiModelProperty(value = \"" + it.commoent.toString() + "\")"
}
out.println " @TableField(value=\"${it.tableCol}\")"
out.println " private ${it.type} ${it.name};"
}
}
out.println ""
fields.each() {
//過濾欄位
if ("id".compareToIgnoreCase(it.name) != 0
&& "Creator".compareToIgnoreCase(it.name) != 0
&& "Crtname".compareToIgnoreCase(it.name) != 0
&& "Create".compareToIgnoreCase(it.name) != 0
&& "Modifiedby".compareToIgnoreCase(it.name) != 0
&& "Mfyname".compareToIgnoreCase(it.name) != 0
&& "Modified".compareToIgnoreCase(it.name) != 0
&& "Status".compareToIgnoreCase(it.name) != 0) {
//get set方法
// out.println " public ${it.type} get${it.name.capitalize()}() {"
// out.println " return ${it.name};"
// out.println " }"
// out.println ""
// out.println " public void set${it.name.capitalize()}(${it.type} ${it.name}) {"
// out.println " this.${it.name} = ${it.name};"
// out.println " }"
// 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
fields += [[
name : javaName(col.getName(), false),
tableCol: col.getName(),
type : typeStr,
commoent: col.getComment(),
annos : ""]]
}
}
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
}
IDEA使用
1、將groovy檔案新增到IDEA
2、選擇需要生成的表,右鍵Tools->Scripted Extendsions->generate POJOs.groovy