JPA自动生成实体类

创建数据库

创建实体包

创建生成实体的脚本

Generate POJOs.groovy(生成实体脚本)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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.x.laystall.uc.domain.entity;"
typeMapping = [
(~/(?i)int/) : "Long",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?i)bool|boolean/) : "Boolean",
(~/(?i)datetime|timestamp/) : "java.util.Date",
(~/(?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, table, className, fields) }
}

def generate(out, table, className, fields) {
def tableName = table.getName()
out.println "package $packageName"
out.println ""
out.println "import lombok.Data;"
out.println "import org.hibernate.annotations.GenericGenerator;"
out.println ""
out.println "import javax.persistence.*;"
out.println "import java.io.Serializable;"
out.println ""
out.println "@Data"
out.println "@Entity"
out.println "@Table(name = \"$tableName\")"
out.println "public class $className implements Serializable {"
out.println ""
// 判断自增
if ((tableName + "_id").equalsIgnoreCase(fields[0].colum) || "id".equalsIgnoreCase(fields[0].colum)) {
out.println "\t@Id"
out.println "\t@GenericGenerator(name=\"idGenerator\", strategy=\"uuid\")"
out.println "\t@GeneratedValue(generator=\"idGenerator\")"
}
fields.each() {
if (it.annos != "") out.println " ${it.annos}"
if (it.colum != it.name) {
out.println "\t@Column(name = \"${it.colum}\")"
}
out.println "\tprivate ${it.type} ${it.name};"
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),
colum: col.getName(),
type : typeStr,
annos: ""]]
}
}

def javaName(str, capitalize) {
def s = str.split(/(?<=[^\p{IsLetter}])/).collect { Case.LOWER.apply(it).capitalize() }
.join("").replaceAll(/[^\p{javaJavaIdentifierPart}]/, "_").replaceAll(/_/, "")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}

Generate Repository.groovy(生成仓库脚本)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.x.laystall.uc.domain.repository;"
typeMapping = [
(~/(?i)int/) : "Long",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?i)bool|boolean/) : "Boolean",
(~/(?i)datetime|timestamp/) : "java.util.Date",
(~/(?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 rClassName = className+'Repository'
new File(dir, rClassName + ".java").withPrintWriter { out -> generate(out, table, className) }
}

def generate(out, table, className) {
def rClassName = className+'Repository'
out.println "package $packageName"
out.println ""
out.println "import com.x.laystall.uc.domain.entity.$className;"
out.println "import org.springframework.stereotype.Repository;"
out.println "import org.springframework.data.jpa.repository.JpaRepository;"
out.println ""
out.println "@Repository"
out.println "public interface $rClassName extends JpaRepository<$className, String> {"
out.println ""

out.println "}"
}


def javaName(str, capitalize) {
def s = str.split(/(?<=[^\p{IsLetter}])/).collect { Case.LOWER.apply(it).capitalize() }
.join("").replaceAll(/[^\p{javaJavaIdentifierPart}]/, "_").replaceAll(/_/, "")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}

选择库表进行生成

生成代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

@Data
@Entity
@Table(name = "pv")
public class Pv implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String ip;

@Column(name = "channel_type")
private Long channelType;

private String openid;

@Column(name = "create_time")
private java.util.Date createTime;
}