IDEA数据库表生成POJOs脚本修改

IDEA自带有的数据库table 生成对应的POJOs的脚本,但是不是很好用,自己就对着原版修改了下,希望对大家有用

1、添加了@Entity、@Table、@Data三个类注解,lombok依赖自己添加。

2、包名能不再写死会根据项目生成正确的包名。

3、去掉了get、set方法。

代码:


import com.intellij.database.model.DasTable

import com.intellij.database.util.Case

import com.intellij.database.util.DasUtil

/*

* Available context bindings:

*  SELECTION  Iterable

*  PROJECT    project

*  FILES      files helper

*/

//packageName = "com.sample;"

typeMapping = [

(~/(?i)int/)                      :"Integer",

(~/(?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{ itinstanceof 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(dir, out, className, fields, table)}

}

def generate(dir, out, className, fields, table) {

out.println"package " + dir.getAbsolutePath().split("main.java.")[1]

.replace("\\",".").replaceAll("/",".") +";"

    out.println""

    out.println""

    out.println""

    out.println"import lombok.Data;\n" +

"\n" +

"import javax.persistence.Column;\n" +

"import javax.persistence.Entity;\n" +

"import javax.persistence.Id;\n" +

"import javax.persistence.Table;"

    out.println""

    out.println""

    out.println""

    out.println""

    out.println"@Entity"

    out.println"@Table(name = \"" + table.getName() +"\")"

    out.println"@Data"

    out.println"public class $className{"

    out.println""

    fields.each(){

        if (it.annos !="") out.println"  ${it.annos}"

        if (it.name =="id") out.println"    @Id"

        else out.println"    @Column(name = \"" + it.colname +"\")"

        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,

colname: col.getName(),

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]

}

你可能感兴趣的:(IDEA数据库表生成POJOs脚本修改)