Generate POJOs.groovy脚本:
import com.intellij.database.model.DasTableimport com.intellij.database.util.Caseimport com.intellij.database.util.DasUtil packageName = ""typeMapping = [ (~/(?i)tinyint|smallint|mediumint/) : "Integer", (~/(?i)bool|bit/) : "Boolean", (~/(?i)int/) : "Long", (~/(?i)float|double|decimal|real/): "BigDecimal", (~/(?i)datetime|timestamp/) : "Date", (~/(?i)date/) : "Date", (~/(?i)time/) : "Date", (~/(?i)/) : "String", (~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream"] 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) packageName = getPackageName(dir) new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className,table, fields) }} def generate(out, className,table, fields) { Set<String> types = new HashSet<String>() fields.each() { types.add(it.type) } out.println "package $packageName" out.println "" out.println "" out.println "import antu.com.annotations.Column;" out.println "import antu.com.annotations.PrimaryKey;" out.println "import antu.com.annotations.TableName;" if (types.contains("Date")) { out.println "import java.util.Date;" } 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" + " * @Author CYH \n" + "**/" out.println "@TableName(\"${table.toString().replaceAll("table:","")}\")" out.println "public class $className {" out.println "" fields.each() { out.println "" if (it.annos != "") out.println " ${it.annos}" if (it.comment != "") out.println " /*** ${it.comment} */" out.println " @Column(\"${it.name.toUpperCase()}\")" out.println " private ${it.type} ${it.name};" } out.println "" 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("^.*src(\\.main\\.java\\.)?", "") + ";"} 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 += [[ column : col.getName(), comment: col.getComment(), name : javaName(col.getName(), false), type : typeStr, 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]}
在idea右侧Database工具栏中新建数据库连接后选中表如图操作即可生成实体类
原文地址:https://www.cnblogs.com/cyh1282656849/p/11969499.html
时间: 2024-11-03 21:41:58