第一次用jpa,分享個自己改的jap實體生成腳本

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<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */

packageName = "com.sample;"
typeMapping = [
  (~/(?i)int/)                      : "long",
  (~/(?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 { 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 {"
  out.println ""
  fields.each() {
    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,
                 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]
}

生成腳本效果

package C:.Users.guo.Desktop;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @Description  
 * @Author  guoziyue
 * @Date 2020-05-01 20:13:36 
 */

@Entity( name ="approvaltemplate")
@ApiModel(value = "")
public class ApprovaltemplateEntity  implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id" )
    @ApiModelProperty(name = "null")
    private Integer id;

    @Column(name = "templatename" )
    @ApiModelProperty(name = "null")
    private String templateName;

    @Column(name = "templatecode" )
    @ApiModelProperty(name = "模板編碼")
    private String templateCode;

    @Column(name = "templatetitle" )
    @ApiModelProperty(name = "模板標題")
    private String templateTitle;

    @Column(name = "workcontent" )
    @ApiModelProperty(name = "工作內容")
    private String workContent;

    @Column(name = "worktime" )
    @ApiModelProperty(name = "要求處理時間")
    private String workTime;

    @Column(name = "remark" )
    @ApiModelProperty(name = "備註")
    private String remark;

    @Column(name = "wxsend" )
    @ApiModelProperty(name = "微信模板消息開關:0不發送;1發送")
    private Integer wxSend;

    @Column(name = "emailsend" )
    @ApiModelProperty(name = "是否發送郵件短信微信等信息:0:不發送;1:發送")
    private Integer emailSend;

    @Column(name = "delstate" )
    @ApiModelProperty(name = "刪除狀態")
    private Integer delState;

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTemplateName() {
        return this.templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

    public String getTemplateCode() {
        return this.templateCode;
    }

    public void setTemplateCode(String templateCode) {
        this.templateCode = templateCode;
    }

    public String getTemplateTitle() {
        return this.templateTitle;
    }

    public void setTemplateTitle(String templateTitle) {
        this.templateTitle = templateTitle;
    }

    public String getWorkContent() {
        return this.workContent;
    }

    public void setWorkContent(String workContent) {
        this.workContent = workContent;
    }

    public String getWorkTime() {
        return this.workTime;
    }

    public void setWorkTime(String workTime) {
        this.workTime = workTime;
    }

    public String getRemark() {
        return this.remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getWxSend() {
        return this.wxSend;
    }

    public void setWxSend(Integer wxSend) {
        this.wxSend = wxSend;
    }

    public Integer getEmailSend() {
        return this.emailSend;
    }

    public void setEmailSend(Integer emailSend) {
        this.emailSend = emailSend;
    }

    public Integer getDelState() {
        return this.delState;
    }

    public void setDelState(Integer delState) {
        this.delState = delState;
    }

    @Override
    public String toString() {
        return "{" +
                    "id='" + id + '\'' +
                    "templateName='" + templateName + '\'' +
                    "templateCode='" + templateCode + '\'' +
                    "templateTitle='" + templateTitle + '\'' +
                    "workContent='" + workContent + '\'' +
                    "workTime='" + workTime + '\'' +
                    "remark='" + remark + '\'' +
                    "wxSend='" + wxSend + '\'' +
                    "emailSend='" + emailSend + '\'' +
                    "delState='" + delState + '\'' +
                '}';
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章