版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yk10010/article/details/88999600
IDEA 快速生成实体
第一步 : 打开DataBase面板,然后连接数据库。
第二步: 创建 Generate MyPOJOs.groovy
文件
文件内容如下: 根据需要调整代码,可以生成不同格式的实体
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.io.*
/*
* Available context bindings:
* SELECTION Iterable<DasObject>
* PROJECT project
* FILES files helper
*/
packageName = "com.xxx.busi.entity;;"
typeMapping = [
(~/(?i)int/) : "Long",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?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 }.each { generate(it, dir) }
}
def generate(table, dir) {
def tableNameDispose = table.getName().substring(0, table.getName().length())
out.println tableNameDispose
def className = javaName(tableNameDispose, true)
out.println className
def fields = calcFields(table)
PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream( new File(dir, className + ".java")), "utf-8"))
output.withPrintWriter { out -> generate(out, className, fields,table.getName()) }
}
def generate(out, className, fields,tableName) {
out.println "package $packageName"
out.println "import java.io.Serializable;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println ""
//out.println "@Entity"
//out.println "@Table(name = \"${tableName}\")"
out.println "public class $className implements Serializable{"
out.println ""
def msum= (long)(Math.random()*1000000000000000000+1000000000000000000)
out.println " private static final long serialVersionUID = ${msum}L;"
fields.each() {
if (isNotEmpty(it.commoent)) {
out.println ""
out.println "\t/**"
out.println "\t * ${it.commoent}"
out.println "\t */"
}
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 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]
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
第三步: 生成实体
如图选中需要生成实体的表:
然后右键 -> Scripted Extensions -> Generate MyPOJOs.groovy , 然后弹出窗口,选择生成实体位置
打开生成的文件如下:
package com.xxx.busi.entity;;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
public class ActEvtLog implements Serializable{
private static final long serialVersionUID = 1113433628863057152L;
private Long logNr;
private String type;
private String procDefId;
private String procInstId;
private String executionId;
private String taskId;
private java.util.Date timeStamp;
private String userId;
private String data;
private String lockOwner;
private java.util.Date lockTime;
private Long isProcessed;
public Long getLogNr() {
return logNr;
}
public void setLogNr(Long logNr) {
this.logNr = logNr;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getProcDefId() {
return procDefId;
}
public void setProcDefId(String procDefId) {
this.procDefId = procDefId;
}
public String getProcInstId() {
return procInstId;
}
public void setProcInstId(String procInstId) {
this.procInstId = procInstId;
}
public String getExecutionId() {
return executionId;
}
public void setExecutionId(String executionId) {
this.executionId = executionId;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public java.util.Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(java.util.Date timeStamp) {
this.timeStamp = timeStamp;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getLockOwner() {
return lockOwner;
}
public void setLockOwner(String lockOwner) {
this.lockOwner = lockOwner;
}
public java.util.Date getLockTime() {
return lockTime;
}
public void setLockTime(java.util.Date lockTime) {
this.lockTime = lockTime;
}
public Long getIsProcessed() {
return isProcessed;
}
public void setIsProcessed(Long isProcessed) {
this.isProcessed = isProcessed;
}
}