Generate POJOs.groovy

简介: Generate POJOs.groovy
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
packageName = ""
typeMapping = [
        (~/(?i)tinyint|smallint|mediumint/)      : "BigDecimal",
        (~/(?i)bool|bit/)                        : "BigDecimal",
        (~/(?i)int/)                             : "BigDecimal",
        (~/(?i)float|double|decimal|real|number/): "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 "import org.hibernate.annotations.GenericGenerator;"
    out.println "import lombok.Data;"
    out.println "import javax.persistence.*;"
    out.println ""
    if (types.contains("Date") || types.contains("timestamp")) {
        out.println "import java.util.Date;"
        out.println "import com.fasterxml.jackson.annotation.JsonFormat;"
    }
    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 hanshuhao \n" +
            " * @description $className \n" +
            " **/"
    out.println "@Data"
    out.println "@Entity"
    out.println "@Table(name = \"" + table.getName() + "\")"
    out.println "public class $className {"
    out.println "    private static final long serialVersionUID = 1734425407533156215L;"
    fields.each() {
        out.println ""
        if (isNotEmpty(it.comment)) {
            out.println "\t/**"
            out.println "\t * ${it.comment.toString()}"
            out.println "\t */"
        }
        if (it.name.toUpperCase().equals("ID")) {
            out.println "    @Id"
            out.println "    @GeneratedValue(generator = \"uuidGenerator\")"
            out.println "    @GenericGenerator(name = \"uuidGenerator\", strategy = \"uuid\")"
        }
        out.println "    @Column(name = \"${it.column}\")"
        if (it.type.toLowerCase().contains("date") || it.type.toLowerCase().contains("timestamp")) {
            out.println "    @JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")"
        }
        if (it.annos != "") out.println "  ${it.annos}"
//    if (it.comment != "")  out.println "  /*** ${it.comment} */"
        out.println "    private ${it.type} ${it.name};"
    }
    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]
}
def isNotEmpty(content) {
    return content != null && content.toString().trim().length() > 0
}
相关文章
|
3月前
|
Dart C语言 Windows
Dart ffi 使用问题之要生成plugin_ffi_sample_bindings_generated.dart文件该如何操作
Dart ffi 使用问题之要生成plugin_ffi_sample_bindings_generated.dart文件该如何操作
|
4月前
|
存储 Java Maven
Maven依赖全爆红,一刷新就JAVA_HOME environment variable is not defined correctlyThis environment variable is
Maven依赖全爆红,一刷新就JAVA_HOME environment variable is not defined correctlyThis environment variable is
|
6月前
|
算法 项目管理 开发者
【Conan 入门教程 】深入解析Conan中的依赖关系的定义方法(In-depth Analysis of Dependency Definition Methods in Conan)
【Conan 入门教程 】深入解析Conan中的依赖关系的定义方法(In-depth Analysis of Dependency Definition Methods in Conan)
264 0
|
编译器
[√]添加预处理的2个不同方式:target_compile_definitions / add_definitions
[√]添加预处理的2个不同方式:target_compile_definitions / add_definitions
219 0
VsCode通过snippet generator快速生成自定义代码片段
VsCode通过snippet generator快速生成自定义代码片段
989 0
VsCode通过snippet generator快速生成自定义代码片段
IDEA告警详解:Optional.isPresent can be replaced with functional-style expression
类似 if(Optional.isPresent()) 的条件语句,可以被重写成函数式风格。
115 0
IDEA告警详解:Optional.isPresent can be replaced with functional-style expression
|
API Android开发
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
|
C++
VS Code注释插件doxygen documentation generator
VS Code注释插件doxygen documentation generator
751 0
VS Code注释插件doxygen documentation generator
|
存储 安全 API
Objective-C Runtime 基本使用
在上一篇文章Objective-C Runtime详解 中我们探讨了Runtime的基本原理,这篇文章我们将总结一下Runtime的一些基本使用
200 0