暫時(shí)只對(duì) MySQL進(jìn)行了測(cè)試
項(xiàng)目使用 Lombok MyBatis-Plus
一:使用步驟首先在項(xiàng)目右側(cè)找到 DataBase 如圖 沒(méi)有請(qǐng)參考 idea中database不顯示問(wèn)題
2.點(diǎn)開(kāi)之后進(jìn)行數(shù)據(jù)庫(kù)連接(注意沒(méi)有驅(qū)動(dòng)的請(qǐng)下載相關(guān)數(shù)據(jù)庫(kù)驅(qū)動(dòng))具體步驟如圖
點(diǎn)開(kāi) + 號(hào)
選擇Date Source
找到相應(yīng)的數(shù)據(jù)庫(kù) 這里我使用的是 mysql
如果沒(méi)有 Dirver 請(qǐng)下載 idea 會(huì)在窗口左下角給提示(這里具體在什么位置我也記不清楚)輸入相關(guān)連接信息
過(guò)程中出現(xiàn)任何問(wèn)題,請(qǐng)?jiān)诹粞詤^(qū)留言(萌新基本全天在線)連接上之后如果沒(méi)有需要的數(shù)據(jù)可以點(diǎn)擊如下圖方式
先設(shè)置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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
import com.intellij.database.model.DasTable import com.intellij.database.util.Case import com.intellij.database.util.DasUtil import java.time.LocalDate /* * Available context bindings: * SELECTION Iterable<DasObject> * PROJECT project * FILES files helper */ // 此處指定包路徑,路徑需要自行維護(hù); packageName = "com.qgy.web.entity;" // 此處指定對(duì)應(yīng)的類型映射,可按需修改,目前tinyint如果要映射到自定義枚舉類型,只能手動(dòng)修改 typeMapping = [ (~/(?i)bigint/) : "Long", (~/(?i)int/) : "Integer", (~/(?i)tinyint/) : "Boolean", (~/(?i)float|double|decimal|real/): "BigDecimal", (~/(?i)time|datetime|timestamp/) : "LocalDateTime", (~/(?i)date/) : "LocalDate", (~/(?i)/) : "String" ] // 上面用到類和它的導(dǎo)入路徑的之間的映射 importMap = [ "BigDecimal" : "java.math.BigDecimal", "LocalDate" : "java.time.LocalDate", "LocalDateTime": "java.time.LocalDateTime", ] // 導(dǎo)入路徑列表,下面引用的時(shí)候會(huì)去重,也可以直接聲明成一個(gè) HashSet importList = [] // 彈出選擇文件的對(duì)話框 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) + "Entity" def fields = calcFields(table) new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir, className + ".java")), "utf-8")).withPrintWriter { out -> generate(out, className, fields, table) } } // 從這里開(kāi)始,拼實(shí)體類的具體邏輯代碼 def generate(out, className, fields, table) { out.println "package $packageName" out.println "" // 引入所需的包 out.println "import lombok.Data;" out.println "import lombok.EqualsAndHashCode;" out.println "import lombok.experimental.Accessors;" out.println "import com.baomidou.mybatisplus.annotation.*;" out.println "import java.io.Serializable;" // 去重后導(dǎo)入列表 importList.unique().each() { pkg -> out.println "import " + pkg + ";" } out.println "" // 添加類注釋 out.println "/**" // 如果添加了表注釋,會(huì)加到類注釋上 if (isNotEmpty(table.getComment())) { out.println " * " + table.getComment() } out.println " *" out.println " * @author 輸入作者" out.println " * @date " + LocalDate.now() out.println " */" // 添加類注解 out.println "@Data" out.println "@EqualsAndHashCode(callSuper = false)" out.println "@Accessors(chain = true)" out.println "@TableName(\"${table.getName()}\")" out.println "public class $className implements Serializable {" out.println "" out.println genSerialID() boolean isId = true // 遍歷字段,按下面的規(guī)則生成 fields.each() { // 輸出注釋 if (isNotEmpty(it.comment)) { out.println "\t/**" out.println "\t * ${it.comment}" out.println "\t */ " } // 這邊默認(rèn)第一個(gè)字段為主鍵,實(shí)際情況大多數(shù)如此,遇到特殊情況可能需要手動(dòng)修改 if (isId) { out.println "\t@TableId(type = IdType.AUTO)" isId = false } if ((it.annos + "" ).indexOf( "[@Id]" ) >= 0 ) out.println "\t@Id" if (it.annos != "" ) out.println " ${it.annos.replace(" [ @Id ] ", " ")}" out.println "\tprivate ${it.type} ${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 if (importMap.containsKey(typeStr)) { importList.add(importMap.get(typeStr)) } fields += [[ name : javaName(col.getName(), false ), type : typeStr, comment: col.getComment(), annos : "\t@TableField(\"" + col.getName() + "\" )" ]] } } def isNotEmpty(content) { return content != null && content.toString().trim().length() > 0 } 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 ] } static String genSerialID() { return "\tprivate static final long serialVersionUID = " + Math.abs( new Random().nextLong()) + "L;" } |
選中需要的數(shù)據(jù)庫(kù),找到需要生成實(shí)體類的表這里我就隨便選擇一個(gè)。右鍵選擇
在左側(cè)列表找到文件名之后點(diǎn)擊會(huì)有彈窗選擇你要存放的地方點(diǎn)擊