-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeignKeyPlugin.java
230 lines (210 loc) · 12 KB
/
ForeignKeyPlugin.java
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.revengemission.plugins.mybatis;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.VisitableElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 外键对象
* 修改文件
* 1、xxxExample.xml, BaseResultMap、Base_Column_List
* 2、XXXEntity.java, 添加引用对象
*/
public class ForeignKeyPlugin extends AbstractXmbgPlugin {
private static final Logger log = LoggerFactory.getLogger(ForeignKeyPlugin.class);
@Override
public boolean validate(List<String> list) {
return true;
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
log.info("enter modelBaseRecordClassGenerated");
String tableName = getTableName(introspectedTable);
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
log.info("table {} foreignKey size {}", tableName, foreignKeyItemList.size());
for (ForeignKeyItem foreignKeyItem : foreignKeyItemList) {
String domainNameName = tableNameToEntityName(foreignKeyItem.getPkTableName());
String fieldName = lowerCaseFirstChar(domainNameName.replace("Entity", ""));
Field field = new Field(fieldName, new FullyQualifiedJavaType(domainNameName));
field.setVisibility(JavaVisibility.PRIVATE);
topLevelClass.addField(field);
Method setMethod = new Method("set" + domainNameName.replace("Entity", ""));
setMethod.setVisibility(JavaVisibility.PUBLIC);
setMethod.addBodyLine("this." + fieldName + " = " + fieldName + ";");
setMethod.addParameter(new Parameter(new FullyQualifiedJavaType(domainNameName), fieldName));
topLevelClass.addMethod(setMethod);
Method getMethod = new Method("get" + domainNameName.replace("Entity", ""));
getMethod.setVisibility(JavaVisibility.PUBLIC);
getMethod.setReturnType(new FullyQualifiedJavaType(domainNameName));
getMethod.addBodyLine("return " + fieldName + ";");
topLevelClass.addMethod(getMethod);
}
return true;
}
@Override
public boolean sqlMapBaseColumnListElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
log.info("enter sqlMapBaseColumnListElementGenerated");
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
if (!foreignKeyItemList.isEmpty()) {
StringBuilder stringBuilder = new StringBuilder();
introspectedTable.getPrimaryKeyColumns().forEach(column -> {
stringBuilder.append(",").append("mt").append(".").append(column.getActualColumnName());
});
introspectedTable.getBaseColumns().forEach(column -> {
stringBuilder.append(",").append("mt").append(".").append(column.getActualColumnName());
});
AtomicInteger atomicInteger = new AtomicInteger(1);
foreignKeyItemList.forEach(foreignKeyItem -> {
Map<String, String> tableColumnsMap = getTableColumns(foreignKeyItem.getPkTableName());
tableColumnsMap.keySet().forEach(columnName -> {
stringBuilder.append(", childT").append(atomicInteger.get()).append(".").append(columnName).append(" as childT").append(atomicInteger.get()).append("_").append(columnName);
});
atomicInteger.incrementAndGet();
});
element.getElements().clear();
element.addElement(new TextElement(stringBuilder.toString().replaceFirst(",", "")));
}
return true;
}
@Override
public boolean sqlMapExampleWhereClauseElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
AtomicBoolean selectFlag = new AtomicBoolean(true);
element.getAttributes().forEach(attribute -> {
if ("id".equalsIgnoreCase(attribute.getName()) && attribute.getValue().contains("Update_By")) {
selectFlag.set(false);
}
});
if (selectFlag.get() && !foreignKeyItemList.isEmpty()) {
XmlElement chooseChild = findFirstMatchedXmlElement(element, "choose");
if (chooseChild != null) {
List<VisitableElement> tempList = chooseChild.getElements();
chooseChild.getElements().forEach(whenElement -> {
if (whenElement instanceof XmlElement xmlElement) {
for (int i = 0; i < xmlElement.getElements().size(); i++) {
VisitableElement visitableElement = xmlElement.getElements().get(i);
if (visitableElement instanceof TextElement textElement) {
String content = textElement.getContent();
xmlElement.getElements().remove(i);
xmlElement.getElements().add(i, new TextElement("and mt." + content.replace("and ", "")));
}
}
}
});
}
}
return true;
}
@Override
public boolean sqlMapCountByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
String tableName = getTableName(introspectedTable);
log.info("enter sqlMapCountByExampleElementGenerated {}", tableName);
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
if (!foreignKeyItemList.isEmpty()) {
String oldStr = "select count(*) from " + tableName;
String newStr = "select count(*) from " + tableName + " mt \n";
Map<String, String> replacement = new LinkedHashMap<>();
replacement.put(oldStr, newStr);
replaceElement(element, replacement);
}
return true;
}
@Override
public boolean sqlMapDeleteByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
String tableName = getTableName(introspectedTable);
log.info("enter sqlMapDeleteByExampleElementGenerated {}", tableName);
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
if (!foreignKeyItemList.isEmpty()) {
String oldStr = "delete from " + tableName;
String newStr = "delete from " + tableName + " mt \n";
Map<String, String> replacement = new LinkedHashMap<>();
replacement.put(oldStr, newStr);
replaceElement(element, replacement);
}
return true;
}
@Override
public boolean sqlMapResultMapWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
log.info("enter sqlMapBaseColumnListElementGenerated");
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
if (!foreignKeyItemList.isEmpty()) {
AtomicInteger atomicInteger = new AtomicInteger(1);
foreignKeyItemList.forEach(foreignKeyItem -> {
String domainName = tableNameToEntityName(foreignKeyItem.getPkTableName());
String fieldName = lowerCaseFirstChar(domainName.replace("Entity", ""));
XmlElement associationElement = new XmlElement("association");
associationElement.addAttribute(new Attribute("property", fieldName));
associationElement.addAttribute(new Attribute("javaType", context.getJavaModelGeneratorConfiguration().getTargetPackage() + "." + domainName));
Map<String, String> tableColumnsMap = getTableColumns(foreignKeyItem.getPkTableName());
tableColumnsMap.keySet().forEach(columnName -> {
if ("id".equals(columnName)) {
XmlElement associationChild = new XmlElement("id");
associationChild.addAttribute(new Attribute("property", "id"));
associationChild.addAttribute(new Attribute("column", "childT" + atomicInteger.get() + "_id"));
associationElement.addElement(associationChild);
} else {
XmlElement associationChild = new XmlElement("result");
associationChild.addAttribute(new Attribute("property", camelName(columnName)));
associationChild.addAttribute(new Attribute("column", "childT" + atomicInteger.get() + "_" + columnName));
associationElement.addElement(associationChild);
}
});
atomicInteger.incrementAndGet();
element.addElement(associationElement);
});
}
return true;
}
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
String tableName = getTableName(introspectedTable);
log.info("enter sqlMapSelectByExampleWithoutBLOBsElementGenerated {}", tableName);
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
if (!foreignKeyItemList.isEmpty()) {
AtomicInteger atomicInteger = new AtomicInteger(1);
StringBuilder stringBuilder = new StringBuilder("from " + tableName + " mt\n");
foreignKeyItemList.forEach(foreignKeyItem -> {
String childTableName = "childT" + atomicInteger.get();
stringBuilder.append("\tleft join ").append(foreignKeyItem.getPkTableName()).append(" ").append(childTableName).append(" on mt.").append(foreignKeyItem.getFkColumnName()).append(" = ").append(childTableName).append(".").append(foreignKeyItem.getPkColumnName()).append("\n");
atomicInteger.incrementAndGet();
});
Map<String, String> replacement = new LinkedHashMap<>();
replacement.put("from " + tableName, stringBuilder.toString());
replaceElement(element, replacement);
}
return true;
}
@Override
public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
String tableName = getTableName(introspectedTable);
log.info("enter sqlMapSelectByPrimaryKeyElementGenerated {}", tableName);
List<ForeignKeyItem> foreignKeyItemList = getForeignKeys(introspectedTable);
if (!foreignKeyItemList.isEmpty()) {
AtomicInteger atomicInteger = new AtomicInteger(1);
StringBuilder stringBuilder = new StringBuilder("from " + tableName + " mt\n");
foreignKeyItemList.forEach(foreignKeyItem -> {
String childTableName = "childT" + atomicInteger.get();
stringBuilder.append("\tleft join ").append(foreignKeyItem.getPkTableName()).append(" ").append(childTableName).append(" on mt.").append(foreignKeyItem.getFkColumnName()).append(" = ").append(childTableName).append(".").append(foreignKeyItem.getPkColumnName()).append("\n");
atomicInteger.incrementAndGet();
});
Map<String, String> replacement = new LinkedHashMap<>();
replacement.put("from " + tableName, stringBuilder.toString());
replacement.put("where id = #{id,jdbcType=BIGINT}", "where mt.id = #{id,jdbcType=BIGINT}");
replaceElement(element, replacement);
}
return true;
}
}