1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.plugins;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.mybatis.generator.api.FullyQualifiedTable;
25 import org.mybatis.generator.api.IntrospectedTable;
26 import org.mybatis.generator.api.IntrospectedTable.TargetRuntime;
27 import org.mybatis.generator.api.PluginAdapter;
28 import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
29 import org.mybatis.generator.api.dom.java.Interface;
30 import org.mybatis.generator.api.dom.java.Method;
31 import org.mybatis.generator.api.dom.java.Parameter;
32 import org.mybatis.generator.api.dom.xml.Attribute;
33 import org.mybatis.generator.api.dom.xml.Document;
34 import org.mybatis.generator.api.dom.xml.XmlElement;
35
36
37
38
39
40
41
42
43 public class RowBoundsPlugin extends PluginAdapter {
44
45 private FullyQualifiedJavaType rowBounds;
46 private Map<FullyQualifiedTable, List<XmlElement>> elementsToAdd;
47
48 public RowBoundsPlugin() {
49 rowBounds = new FullyQualifiedJavaType("org.apache.ibatis.session.RowBounds");
50 elementsToAdd = new HashMap<FullyQualifiedTable, List<XmlElement>>();
51 }
52
53 public boolean validate(List<String> warnings) {
54 return true;
55 }
56
57 @Override
58 public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method,
59 Interface interfaze, IntrospectedTable introspectedTable) {
60 if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
61 copyAndAddMethod(method, interfaze);
62 }
63 return true;
64 }
65
66 @Override
67 public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(
68 Method method, Interface interfaze,
69 IntrospectedTable introspectedTable) {
70 if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
71 copyAndAddMethod(method, interfaze);
72 }
73 return true;
74 }
75
76 @Override
77 public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
78 XmlElement element, IntrospectedTable introspectedTable) {
79 if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
80 copyAndSaveElement(element, introspectedTable.getFullyQualifiedTable());
81 }
82 return true;
83 }
84
85 @Override
86 public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
87 XmlElement element, IntrospectedTable introspectedTable) {
88 if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
89 copyAndSaveElement(element, introspectedTable.getFullyQualifiedTable());
90 }
91 return true;
92 }
93
94
95
96
97
98 @Override
99 public boolean sqlMapDocumentGenerated(Document document,
100 IntrospectedTable introspectedTable) {
101 List<XmlElement> elements = elementsToAdd.get(introspectedTable.getFullyQualifiedTable());
102 if (elements != null) {
103 for (XmlElement element : elements) {
104 document.getRootElement().addElement(element);
105 }
106 }
107
108 return true;
109 }
110
111
112
113
114
115
116
117
118 private void copyAndAddMethod(Method method, Interface interfaze) {
119 Method newMethod = new Method(method);
120 newMethod.setName(method.getName() + "WithRowbounds");
121 newMethod.addParameter(new Parameter(rowBounds, "rowBounds"));
122 interfaze.addMethod(newMethod);
123 interfaze.addImportedType(rowBounds);
124 }
125
126
127
128
129
130
131
132 private void copyAndSaveElement(XmlElement element, FullyQualifiedTable fqt) {
133 XmlElement newElement = new XmlElement(element);
134
135
136 for (Iterator<Attribute> iterator = newElement.getAttributes().iterator(); iterator.hasNext();) {
137 Attribute attribute = iterator.next();
138 if ("id".equals(attribute.getName())) {
139 iterator.remove();
140 Attribute newAttribute = new Attribute("id", attribute.getValue() + "WithRowbounds");
141 newElement.addAttribute(newAttribute);
142 break;
143 }
144 }
145
146
147
148 List<XmlElement> elements = elementsToAdd.get(fqt);
149 if (elements == null) {
150 elements = new ArrayList<XmlElement>();
151 elementsToAdd.put(fqt, elements);
152 }
153 elements.add(newElement);
154 }
155 }