一、定制groovy脚本
1 import com.intellij.database.model.DasTable 2 import com.intellij.database.util.Case 3 import com.intellij.database.util.DasUtil 4 import java.io.* 5 import java.text.SimpleDateFormat 6 /* 7 * Available context bindings: 8 * SELECTION Iterable<DasObject> 9 * PROJECT project 10 * FILES files helper 11 */ 12 13 packageName = "com.demo.model;" 14 typeMapping = [ 15 (~/(?i)int/) : "Long", 16 (~/(?i)float|double|decimal|real/): "Double", 17 (~/(?i)datetime|timestamp/) : "Date", 18 (~/(?i)date/) : "Date", 19 (~/(?i)time/) : "java.sql.Time", 20 (~/(?i)/) : "String" 21 ] 22 23 FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> 24 SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) } 25 } 26 27 def generate(table, dir) { 28 def className = className(table.getName()).capitalize() 29 def fields = calcFields(table) 30 packageName = getPackageName(dir) 31 //new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) } 32 PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, className + ".java")), "UTF-8")) 33 printWriter.withPrintWriter {out -> generate(out, className, fields,table)} 34 } 35 36 def generate(out, className, fields,table) { 37 out.println "package $packageName" 38 out.println "" 39 40 // 判断是否需要引入Date 41 boolean dateFlag = false 42 fields.each() { 43 if(it.type.contains("Date")){ 44 dateFlag = true 45 } 46 } 47 if(dateFlag){ 48 out.println "import java.util.Date;" 49 } 50 51 // 判断表名的注释,为空时设置为表名 52 def tableComment = table.getComment() 53 if(null == tableComment){ 54 tableComment = table.getName() 55 } 56 57 out.println "import lombok.Data;" 58 out.println "import javax.persistence.Table;" 59 out.println "import javax.persistence.Column;" 60 out.println "import io.swagger.annotations.ApiModel;" 61 out.println "import io.swagger.annotations.ApiModelProperty;" 62 63 out.println "" 64 65 out.println "/**\n" + 66 " * @Description " + tableComment + " \n" + 67 " * @Author yangli\n" + 68 " * @Date "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " \n" + 69 "**/" 70 out.println "@Data" 71 out.println "@ApiModel(\"" + tableComment + "\")" 72 out.println "@Table(name = \"" + table.getName() + "\")" 73 out.println "public class $className {" 74 out.println "" 75 fields.each() { 76 // 输出注释 77 // if (isNotEmpty(it.commoent)) { 78 // out.println " /**" 79 // out.println " * ${it.commoent.toString()}" 80 // out.println " */" 81 // } 82 // 改为swagger注解 83 if (isNotEmpty(it.commoent)) { 84 out.println " @ApiModelProperty(value = \"" + it.commoent.toString() + "\")" 85 } 86 87 out.println " @Column(name = \"" + it.realColumnName.toString() + "\")" 88 89 if (it.annos != "") out.println " ${it.annos}" 90 out.println " private ${it.type} ${it.name};" 91 } 92 out.println "" 93 out.println "}" 94 } 95 96 def calcFields(table) { 97 DasUtil.getColumns(table).reduce([]) { fields, col -> 98 def spec = Case.LOWER.apply(col.getDataType().getSpecification()) 99 def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value 100 fields += [[ 101 name : javaName(col.getName(), false), 102 type : typeStr, 103 commoent: col.getComment(), 104 realColumnName : col.getName(), //20190825 新增未处理列名 105 annos: ""]] 106 } 107 } 108 109 def className(str) { 110 // 过滤表名最后的_T 111 boolean endFlag = str.endsWith('_T') 112 if(endFlag){ 113 str = str.substring(0, str.length()-2) 114 } 115 def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str) 116 .collect { Case.LOWER.apply(it).capitalize() } 117 .join("") 118 .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_") 119 s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1] 120 } 121 122 def javaName(str, capitalize) { 123 def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str) 124 .collect { Case.LOWER.apply(it).capitalize() } 125 .join("") 126 .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_") 127 capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1] 128 } 129 130 def isNotEmpty(content) { 131 return content != null && content.toString().trim().length() > 0 132 } 133 // 获取包所在文件夹路径 134 def getPackageName(dir) { 135 return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";" 136 }
二、操作
三、打完收功~