欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java开发中通过脚本生成JavaBean类

程序员文章站 2022-04-15 17:46:45
打开Generate POJOs.groovyimport com.intellij.database.model.DasTableimport com.intellij.database.util.Caseimport com.intellij.database.util.DasUtil/* * Available context bindings: * SELECTION Iterable * PROJECT project...

打开Generate POJOs.groovy
1.先在中连接mysql数据库
Java开发中通过脚本生成JavaBean类
Java开发中通过脚本生成JavaBean类
Java开发中通过脚本生成JavaBean类

import com.intellij.database.model.DasTable import com.intellij.database.util.Case import com.intellij.database.util.DasUtil /*
 * Available context bindings:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */ packageName = "cn.ithers.blog.pojo;" typeMapping = [ (~/(?i)int/) : "long", (~/(?i)float|double|decimal|real/): "double", (~/(?i)datetime|timestamp/) : "java.sql.Timestamp", (~/(?i)date/) : "java.sql.Date", (~/(?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) { def className = javaName(table.getName(), true) def fields = calcFields(table) new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) } } def generate(out, className, fields) { out.println "package $packageName" out.println "" out.println "" out.println "public class $className {" out.println "" fields.each() { if (it.annos != "") out.println "  ${it.annos}" 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 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), 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] } 

修改后

import com.intellij.database.model.DasTable import com.intellij.database.util.Case import com.intellij.database.util.DasUtil /*
 * Available context bindings:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */ //包名 packageName = "cn.ithers.blog.pojo;" typeMapping = [ (~/(?i)int/) : "long", (~/(?i)float|double|decimal|real/): "double", (~/(?i)datetime|timestamp/) : "java.sql.Timestamp", (~/(?i)date/) : "java.sql.Date", (~/(?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) { def className = javaName(table.getName(), true) def fields = calcFields(table) def targetClassName = className.substring(2, className.size()) new File(dir, targetClassName + ".java").withPrintWriter { out -> generate(out, targetClassName, fields) } } def generate(out, className, fields) { out.println "package $packageName" out.println "" out.println "import javax.persistence.Entity;" out.println "import javax.persistence.Id;" out.println "import javax.persistence.Table;" out.println "import javax.persistence.Column;" out.println "" out.println "@Entity" out.println "@Table ( name =\"tb_" + Case.LOWER.apply(className) + "\" )" out.println "public class $className {" out.println "" fields.each() { if ('id'.equals(Case.LOWER.apply(it.name))) { it.annos = '\t@Id' } else if ('order'.equals(Case.LOWER.apply(it.name))) { it.annos = "\t@Column(name = \"`" + it.name + "`\" )" } else { it.annos = "\t@Column(name = \"" + it.name + "\" )" } if (it.annos != "") out.println "  ${it.annos}" out.println "\tprivate ${it.type} ${it.name};\n" } out.println "" fields.each() { out.println "" out.println "\tpublic ${it.type} get${it.name.capitalize()}() {" out.println "\t\treturn ${it.name};" out.println "\t}" out.println "" out.println "\tpublic void set${it.name.capitalize()}(${it.type} ${it.name}) {" out.println "\t\tthis.${it.name} = ${it.name};" out.println "\t}" 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), name : col.getName(), 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] } 

使用

Java开发中通过脚本生成JavaBean类
需要选择生成到哪个路径下

结果

Java开发中通过脚本生成JavaBean类

本文地址:https://blog.csdn.net/qq_43554997/article/details/108263686