- 先找到需要修改的脚本位置,一张图告诉你。
- 编辑groovy里面的脚本,你可以直接复制我的脚本进去
import com.intellij.database.model.DasTable import com.intellij.database.model.ObjectKind import com.intellij.database.util.Case import com.intellij.database.util.DasUtil
/*
* Available context bindings:
* SELECTION Iterable
packageName = "com.sample;"
typeMapping = [
(/(?i)bigint/) : "Long",
(/(?i)int/) : "Integer",
(/(?i)float|double|decimal|real/): "java.math.BigDecimal",
(/(?i)datetime|timestamp/) : "java.util.Date",
(/(?i)date/) : "java.util.Date",
(/(?i)time/) : "java.util.Date",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.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 implements java.io.Serializable{" out.println "" fields.each() { out.println "" out.println " /**" out.println " * ${it.comment}" out.println " */" 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 commentStr = col.getComment() 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: "", comment : commentStr]] } }
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] }
- 然后就可以进行导出了,在你的表,右键鼠标
- 导出的代码样例如下
package org.xjy.jgcc.dao.po;
public class OpenAdminDept implements java.io.Serializable{
/** * null */ private Long id;
/** * 部门名称 */ private String name;
/** * 企业编码 */ private String enterpriseCodeArr;
/** * 创建时间 */ private java.util.Date createTime;
/** * 更新时间 */ private java.util.Date updateTime;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEnterpriseCodeArr() { return enterpriseCodeArr; }
public void setEnterpriseCodeArr(String enterpriseCodeArr) { this.enterpriseCodeArr = enterpriseCodeArr; }
public java.util.Date getCreateTime() { return createTime; }
public void setCreateTime(java.util.Date createTime) { this.createTime = createTime; }
public java.util.Date getUpdateTime() { return updateTime; }
public void setUpdateTime(java.util.Date updateTime) { this.updateTime = updateTime; }
}