IDEA自动生成实体类

2020-07-03 18:53:59 748

idea里先添加数据库

选择表,右键 ,先配置下脚本

1.png

编辑Generate POJOs.groovy

import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * Available context bindings:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */

packageName = "com.sample;"
typeMapping = [
        (~/(?i)bigint/)                             : "Long",
        (~/(?i)tinyint|smallint|mediumint|int/)     : "Integer",
        (~/(?i)bool|bit/)                           : "Boolean",
        (~/(?i)float/)                              : "Float",
        (~/(?i)double/)                             : "Double",
        (~/(?i)decimal|real/)                       : "BigDecimal",
        (~/(?i)datetime|timestamp|date|time/)       : "Date",
        (~/(?i)blob|binary|bfile|clob|raw|image/)   : "InputStream",
        (~/(?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 className = javaName(table.getName(), true)
    def fields = calcFields(table)
    def file = new File(dir, className + ".java");

    file.withPrintWriter("utf-8") { out -> generate(out, table, className, fields) }
}

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

    out.println "package $packageName"
    out.println ""
    out.println "import lombok.Data;"
    out.println "import javax.persistence.*;"
    out.println ""

    out.println "/**"
    out.println " * $table.comment"
    out.println " * generated by Generate POJOs.groovy"
    out.println " *"
    out.println " * @author Test"
    out.println " * @date " + getTime()
    out.println " */"
    out.println "@Data"
    out.println "@Entity"
    out.println "@Table(name = \"$table.name\")"
    out.println "public class $className {"
    out.println ""
    fields.each() {
        if (it.annos != "") out.println "  ${it.annos}"
        out.println "  /**"
        out.println "   * ${it.comment}"
        out.println "   */"

        out.println "  @Column(name= \"${it.field}\")"
        out.println "  private ${it.type} ${it.name};"
    }
    out.println ""
    //getter setter 方法
//    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  : "",
                           field  : col.getName(),
                           comment: col.getComment()]]
    }
}

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 getTime() {
    long timeStamp = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
    return sd;
}


编辑完后执行脚本,点击 上图 红框 上一栏 “Generate POJOs.groovy”

以上配置的效果

package com.sample;


public class Customer {

    /*
     * null
     */
    @Column(name = "Id")
    private Integer id;

    /*
     * 数据库one字段说明
     */
    @Column(name = "one")
    private String one;
    /*
     * 数据库two字段说明
     */
    @Column(name = "two")
    private String two;
    /*
     * 数据库three字段说明
     */
    @Column(name = "three")
    private String three;

}


IDEA方法注释模板

IDEA方法注释模板

设置tab键触发注释模板内容** * <p>$methodName$</p> *$param$ $return$ * @Title $methodName$ * @author kyaa * @date $date$ $time$ */ 变量填充param自己脚本模拟def result
2022-01-06
IDEA自动生成实体类

IDEA自动生成实体类

idea里先添加数据库选择表,右键 ,先配置下脚本编辑Generate POJOs.groovyimport com.intellij.database.model.DasTable import com.intellij.database.util.Case import com.intellij
2020-07-03
IDEA 2019.1 xml 不高亮

IDEA 2019.1 xml 不高亮

前几天更新了idea后,发现xml里的代码都没有了高亮,变得跟记事本一个德性了打开setting ,搜索 File Types,找到xml项, 查看下方的匹配格式,果然没有xml,(idea真是厉害)点击右方的+,输入*.xml,点击ok,解决问题
2020-03-28

freemarker 时间显示不正常 设置时区

项目在本地开发的时候显示正常,部署上服务器就一直差8个小时,最后发现freemarker官方文档有这样的说明time_zone:时区的名称来显示并格式化时间。 默认情况下,使用JVM的时区。 也可以是 Java 时区 API 接受的值,或者 "JVM default" (从 FreeMarker 2
2020-03-28
IDEA 2019.1 xml 不高亮

IDEA 2019.1 xml 不高亮

前几天更新了idea后,发现xml里的代码都没有了高亮,变得跟记事本一个德性了打开setting ,搜索 File Types,找到xml项, 查看下方的匹配格式,果然没有xml,(idea真是厉害)点击右方的+,输入*.xml,点击ok,解决问题
2020-03-28

npm install 淘宝镜像

npm install --registry=https://registry.npm.taobao.org
2020-03-28
Java中方法的参数传递机制

Java中方法的参数传递机制

来看一段代码 public class Man { private String name; private Integer age; public String getName() { return name; } publi
2020-03-28
基于自定义注解手写权限控制

基于自定义注解手写权限控制

方法一: AOP 方法二: 拦截器项目结构项目依赖<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-w
2020-03-28

Docker 部署 详细全过程 附代码

Docker 部署本站 全过程环境:CentOS7.61. 安装Docker其他版本CentOS可以参考这个https://help.aliyun.com/document_detail/187598.html查看本机内核版本,内核版本需高于 3.10uname -r 确保 yum 包最新yum u
2020-03-28

SpringBoot 启动普通java工程

引入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.9</version> </dependency>
2020-03-28

Vue.js DOM操作

<template> <input type="button" @click="reply($event)" value="回复"> </template> export default { methods: { replyFun(e) {
2020-03-29
CentOS7编译调试OpenJDK12

CentOS7编译调试OpenJDK12

1. 下载源码https://hg.openjdk.java.net/jdk/jdk12点击左侧的browse,再点击zip,就可以下载zip格式的源码压缩包。unzip xxx.zip 解压文件2. 安装jdkyum install java-11-openjdk-devel -y3. 运行con
2020-04-23
编写自己的Spring Boot Starter

编写自己的Spring Boot Starter

1.新建一个maven项目命名规则统一是xxx-spring-boot-starter完整pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
2020-06-29