-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchUpdateByPrimaryKeyPlugin.java
123 lines (93 loc) · 4.75 KB
/
BatchUpdateByPrimaryKeyPlugin.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
package com.revengemission.plugins.mybatis;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
* 批量更新
*/
public class BatchUpdateByPrimaryKeyPlugin extends AbstractXmbgPlugin {
private static final String CLIENT_METHOD_NAME = "batchUpdateByPrimaryKey";
private static final String PROPERTY_PREFIX = "item.";
@Override
public void initialized(IntrospectedTable introspectedTable) {
}
@Override
public boolean validate(List<String> warnings) {
return true;
}
@Override
public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
String objectName = getEntityName(introspectedTable);
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
Method method = new Method(CLIENT_METHOD_NAME);
method.setAbstract(true);
FullyQualifiedJavaType type = new FullyQualifiedJavaType("java.util.List<" + objectName + ">");
method.addParameter(new Parameter(type, "list"));
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
importedTypes.add(type);
interfaze.addMethod(method);
interfaze.addImportedTypes(importedTypes);
return true;
}
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
XmlElement updateXmlElement = new XmlElement("update");
updateXmlElement.addAttribute(new Attribute("id", CLIENT_METHOD_NAME));
String parameterType = "java.util.List";
updateXmlElement.addAttribute(new Attribute("parameterType", parameterType));
updateXmlElement.addElement(new TextElement("<if test=\"list != null and list.size() > 0\">"));
XmlElement foreach = new XmlElement("foreach");
foreach.addAttribute(new Attribute("collection", "list"));
foreach.addAttribute(new Attribute("item", "item"));
foreach.addAttribute(new Attribute("index", "index"));
foreach.addAttribute(new Attribute("separator", ";"));
generateTextBlockAppendTableName("update ", introspectedTable, foreach);
TextElement setElement = new TextElement("set");
foreach.addElement(setElement);
// version = version + 1
generateParameterForSet(PROPERTY_PREFIX, introspectedTable.getNonPrimaryKeyColumns(), foreach);
generateWhereConditions(PROPERTY_PREFIX, introspectedTable.getPrimaryKeyColumns(), foreach);
updateXmlElement.addElement(foreach);
updateXmlElement.addElement(new TextElement("</if>"));
updateXmlElement.addElement(new TextElement("<if test=\"list == null or list.size() == 0\">"));
updateXmlElement.addElement(new TextElement(" select 0"));
updateXmlElement.addElement(new TextElement("</if>"));
document.getRootElement().addElement(updateXmlElement);
return true;
}
protected void generateParameterForSet(String fieldPrefix, List<IntrospectedColumn> columns, XmlElement dynamicElement) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < columns.size(); i++) {
IntrospectedColumn introspectedColumn = columns.get(i);
sb.setLength(0);
if ("version".equalsIgnoreCase(introspectedColumn.getActualColumnName())) {
sb.append(" version = version + 1");
} else if ("last_modified".equalsIgnoreCase(introspectedColumn.getActualColumnName())) {
sb.append(" last_modified = now()");
} else if ("modified_date".equalsIgnoreCase(introspectedColumn.getActualColumnName())) {
sb.append(" modified_date = now()");
} else {
sb.append(" ");
sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, fieldPrefix));
}
if (i != columns.size() - 1) {
sb.append(',');
}
TextElement tempElement = new TextElement(sb.toString());
dynamicElement.addElement(tempElement);
}
}
}