参考:https://www.cnblogs.com/zhulei2/p/13265719.html
先找到Generate POJOs.groovy所在路径,然后copy一份再修改
修改后My Custom Generate POJOs.groovy
import com.intellij.database.model.DasTable
import com.intellij.database.util.DasUtil
import com.intellij.database.util.Case
import java.text.SimpleDateFormat
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "com.sample;"
typeMapping = [
(~/(?i)int|tinyint|smallint|mediumint/) : "Integer",
(~/(?i)long/) : "Long",
(~/(?i)float|double|real/) : "double",
(~/(?i)decimal/) : "BigDecimal",
(~/(?i)bool|bit|tinyint(1)/) : "Boolean",
// (~/(?i)datetime|timestamp/) : "java.sql.Timestamp",
(~/(?i)datetime|timestamp/) : "LocalDateTime",
(~/(?i)date/) : "LocalDate",
(~/(?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) {
// 跟据路径获取包名
packageName = getPackageName(dir)
def className = javaName(table.getName(), true)
def fields = calcFields(table)
new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className, fields, table) }
}
def generate(out, className, fields, table) {
out.println "package $packageName"
out.println ""
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println "import java.io.Serializable;"
out.println "import lombok.Data;"
out.println "import lombok.AllArgsConstructor;"
out.println "import lombok.NoArgsConstructor;"
out.println "import javax.persistence.Id;"
Set types = new HashSet()
fields.each() {
types.add(it.type)
}
if (types.contains("LocalDateTime")) {
out.println "import java.time.LocalDateTime;"
}
if (types.contains("LocalDate")) {
out.println "import java.time.LocalDate;"
}
out.println ""
// out.println "/**\n" +
// " * @Description \n" +
// " * @Author liuzhenpeng\n" +
// " * @Date "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " \n" +
// " */"
out.println ""
out.println "@Data"
out.println "@AllArgsConstructor"
out.println "@NoArgsConstructor"
out.println "@Entity"
out.println "@Table ( name =\""+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 * "+new String(it.commoent.toString().getBytes("UTF-8"))
out.println "\t */"
}
if (it.annos != "") out.println " ${it.annos}"
out.println "\tprivate ${it.type} ${it.name};"
}
out.println ""
// 使用lombok,不需要生成get、set方法
// fields.each() {
// out.println ""
// 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 getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
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 = [
name : javaName(col.getName(), false),
colName : col.getName(),
type : typeStr,
commoent: col.getComment(),
annos: "\t@Column(name = \""+col.getName()+"\" )"
]
//对于表中主键自定义注解
if ("id" == Case.LOWER.apply(col.getName().toString())){
comm.annos = "\t@Id\n"
//自增主键需要
comm.annos += "\t@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]
}
再生成