• Idea 根据 表 生成 实体类


    来源: https://blog.csdn.net/lp2388163/article/details/86480480

    点击这个 

    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 = "com.sample;"
    typeMapping = [
            (~/(?i)bigint/)                   : "Long",
            (~/(?i)int/)                      : "Integer",
            (~/(?i)float|double|decimal|real/): "Double",
            (~/(?i)datetime|timestamp/)       : "java.util.Date",
            (~/(?i)time/)                     : "java.util.Date",
            (~/(?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 PrintWriter(new OutputStreamWriter(new FileOutputStream( new File(dir, className + ".java")), "utf-8")).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 (isNotEmpty(it.commoent)) {
                out.println "	/**"
                out.println "	 * ${it.commoent}"
                out.println "	 */"
            }
    
    
            if (it.annos != "") out.println "  ${it.annos}"
            out.println "  private ${it.type} ${it.name};"
        }
        out.println ""
        // 注释掉 set get 方法
        /*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,
                               commoent: col.getComment(),
                               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]*/
        return str
    }
    
    def isNotEmpty(content) {
        return content != null && content.toString().trim().length() > 0
    }
  • 相关阅读:
    重要的API运算函数
    Chicken的代码解剖 :4 ChickenPawn_Chicken一小部分
    Chicken的代码解剖:5 Chicken中的两个接口及其相关
    项目实例:深投控股star rating评分插件
    项目实例:深投控股JQueryXmlMenu
    编程经验:VS2008注册方法
    编程经验:SQL Server Management Studio使用注意事项
    程序员面试题精选100题(07)翻转句子中单词的顺序
    程序员面试100题精选(8)
    Ogre框架的搭建过程
  • 原文地址:https://www.cnblogs.com/whm-blog/p/10971945.html
Copyright © 2020-2023  润新知