暫時只對 MySQL進行了測試
項目使用 Lombok MyBatis-Plus
一:使用步驟首先在項目右側找到 DataBase 如圖 沒有請參考 idea中database不顯示問題
2.點開之后進行數據庫連接(注意沒有驅動的請下載相關數據庫驅動)具體步驟如圖
點開 + 號
選擇Date Source
找到相應的數據庫 這里我使用的是 mysql
如果沒有 Dirver 請下載 idea 會在窗口左下角給提示(這里具體在什么位置我也記不清楚)輸入相關連接信息
過程中出現任何問題,請在留言區留言(萌新基本全天在線)連接上之后如果沒有需要的數據可以點擊如下圖方式
先設置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 */ // 此處指定包路徑,路徑需要自行維護; packageName = "com.qgy.web.entity;" // 此處指定對應的類型映射,可按需修改,目前tinyint如果要映射到自定義枚舉類型,只能手動修改 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" ] // 上面用到類和它的導入路徑的之間的映射 importMap = [ "BigDecimal" : "java.math.BigDecimal", "LocalDate" : "java.time.LocalDate", "LocalDateTime": "java.time.LocalDateTime", ] // 導入路徑列表,下面引用的時候會去重,也可以直接聲明成一個 HashSet importList = [] // 彈出選擇文件的對話框 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) } } // 從這里開始,拼實體類的具體邏輯代碼 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;" // 去重后導入列表 importList.unique().each() { pkg -> out.println "import " + pkg + ";" } out.println "" // 添加類注釋 out.println "/**" // 如果添加了表注釋,會加到類注釋上 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 // 遍歷字段,按下面的規則生成 fields.each() { // 輸出注釋 if (isNotEmpty(it.comment)) { out.println "\t/**" out.println "\t * ${it.comment}" out.println "\t */ " } // 這邊默認第一個字段為主鍵,實際情況大多數如此,遇到特殊情況可能需要手動修改 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;" } |
選中需要的數據庫,找到需要生成實體類的表這里我就隨便選擇一個。右鍵選擇
在左側列表找到文件名之后點擊會有彈窗選擇你要存放的地方點擊