idea生成数据库实体类 之 Generate POJOs.groovy 配置

    技术2022-07-12  69

    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 = "com.sample;" typeMapping = [ (~/(?i)int/) : "long", (~/(?i)float|double|decimal|real/): "double", (~/(?i)datetime|timestamp/) : "java.sql.Timestamp", (~/(?i)date/) : "Date", (~/(?i)time/) : "java.sql.Time", // (~/(?i)blob|binary|bfile|clob|raw|image/): "InputStream", (~/(?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) { 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, fields, table) } } // 获取包所在文件夹路径 def getPackageName(dir) { return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";" } def generate(out, className, fields, table) { out.println "package $packageName" out.println "" out.println "import lombok.Data;" out.println "import javax.persistence.*;" out.println "import java.io.Serializable;" out.println "import io.swagger.annotations.ApiModel;" out.println "import io.swagger.annotations.ApiModelProperty;" Set types = new HashSet() fields.each() { types.add(it.type) } if (types.contains("Date")) { out.println "import java.util.Date;" out.println "import com.fasterxml.jackson.annotation.JsonFormat;" out.println "import org.springframework.format.annotation.DateTimeFormat;" } if (types.contains("InputStream")) { out.println "import java.io.InputStream;" } out.println "" out.println "" out.println "/**\n" + " * ${table.comment}\n"+ " * \n"+ " * @Author Gavino\n" + //1. 修改idea为自己名字 " * @Create " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " \n" + " * @Description \n" + " */" out.println "@Data" out.println "@ApiModel(value=\"${table.comment}\")" out.println "@Table ( name =\"" + table.getName() + "\" , schema = \"\")" //2. schema = \"后面添加自己的表空间名称(mysql可以不添加, 不用这个schema属性也行) out.println "public class $className implements Serializable{" out.println "" out.println " "+genSerialID() out.println "" fields.each() { out.println "" // 输出注释 if (isNotEmpty(it.commoent)) { out.println " /**" out.println " * ${it.commoent.toString()}" out.println " */" } if ((it.annos+"").indexOf("[@Id]") >= 0){ out.println " @Id" out.println " @GeneratedValue(strategy = GenerationType.IDENTITY,generator = \"select sequence.nextval from dual\")" } if (it.type == "Date"){ out.println " @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")" out.println " @DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")" } out.println " @ApiModelProperty(value = \"${it.commoent.toString()}\")" out.println " @Column(name = \"${it.colum}\")" out.println " private ${it.type} ${it.colum};" } 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 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), colum: col.getName(), type : typeStr, commoent: col.getComment(), annos : "@Column(name = \"" + col.getName() + "\" )"] if ("n_a".equals(Case.LOWER.apply(col.getName()))) comm.annos += "[@Id]" 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] } def isNotEmpty(content) { return content != null && content.toString().trim().length() > 0 } static String genSerialID() { return "\tprivate static final long serialVersionUID = " + Math.abs(new Random().nextLong()) + "L;" }

     

    Processed: 0.015, SQL: 10