idea生成mysql表_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]

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值