1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;
17
18 import org.mybatis.generator.api.IntrospectedColumn;
19 import org.mybatis.generator.api.dom.xml.Attribute;
20 import org.mybatis.generator.api.dom.xml.TextElement;
21 import org.mybatis.generator.api.dom.xml.XmlElement;
22 import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
23
24
25
26
27
28
29 public class UpdateByExampleSelectiveElementGenerator extends
30 AbstractXmlElementGenerator {
31
32 public UpdateByExampleSelectiveElementGenerator() {
33 super();
34 }
35
36 @Override
37 public void addElements(XmlElement parentElement) {
38 XmlElement answer = new XmlElement("update");
39
40 answer
41 .addAttribute(new Attribute(
42 "id", introspectedTable.getUpdateByExampleSelectiveStatementId()));
43
44 answer.addAttribute(new Attribute("parameterType", "map"));
45
46 context.getCommentGenerator().addComment(answer);
47
48 StringBuilder sb = new StringBuilder();
49 sb.append("update ");
50 sb.append(introspectedTable
51 .getAliasedFullyQualifiedTableNameAtRuntime());
52 answer.addElement(new TextElement(sb.toString()));
53
54 XmlElement dynamicElement = new XmlElement("set");
55 answer.addElement(dynamicElement);
56
57 for (IntrospectedColumn introspectedColumn : introspectedTable
58 .getAllColumns()) {
59 XmlElement isNotNullElement = new XmlElement("if");
60 sb.setLength(0);
61 sb.append(introspectedColumn.getJavaProperty("record."));
62 sb.append(" != null");
63 isNotNullElement.addAttribute(new Attribute("test", sb.toString()));
64 dynamicElement.addElement(isNotNullElement);
65
66 sb.setLength(0);
67 sb.append(MyBatis3FormattingUtilities
68 .getAliasedEscapedColumnName(introspectedColumn));
69 sb.append(" = ");
70 sb.append(MyBatis3FormattingUtilities.getParameterClause(
71 introspectedColumn, "record."));
72 sb.append(',');
73
74 isNotNullElement.addElement(new TextElement(sb.toString()));
75 }
76
77 answer.addElement(getUpdateByExampleIncludeElement());
78
79 if (context.getPlugins()
80 .sqlMapUpdateByExampleSelectiveElementGenerated(answer,
81 introspectedTable)) {
82 parentElement.addElement(answer);
83 }
84 }
85 }