Skip to content

Commit 50b139f

Browse files
author
hewei
committed
[issues#26][issues#27]:插件因为是复用的,需要进行属性的清空。
1 parent 567279d commit 50b139f

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public class SelectOneByExamplePlugin extends BasePlugin {
4141
private XmlElement selectOneByExampleEle;
4242
private XmlElement selectOneByExampleWithBLOBsEle;
4343

44+
@Override
45+
public void initialized(IntrospectedTable introspectedTable) {
46+
super.initialized(introspectedTable);
47+
// bug:26,27
48+
this.selectOneByExampleWithBLOBsEle = null;
49+
this.selectOneByExampleEle = null;
50+
}
51+
4452
/**
4553
* Java Client Methods 生成
4654
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html

src/main/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePlugin.java

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import com.itfsw.mybatis.generator.plugins.utils.hook.ISelectOneByExamplePluginHook;
2121
import org.mybatis.generator.api.IntrospectedColumn;
2222
import org.mybatis.generator.api.IntrospectedTable;
23-
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24-
import org.mybatis.generator.api.dom.java.Interface;
25-
import org.mybatis.generator.api.dom.java.Method;
26-
import org.mybatis.generator.api.dom.java.Parameter;
23+
import org.mybatis.generator.api.dom.java.*;
2724
import org.mybatis.generator.api.dom.xml.*;
2825
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
2926

@@ -44,6 +41,9 @@ public class SelectSelectivePlugin extends BasePlugin implements ISelectOneByExa
4441
public static final String METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE = "selectByPrimaryKeySelective";
4542
public static final String METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE = "selectOneByExampleSelective";
4643
public static final String ID_FOR_PROPERTY_BASED_RESULT_MAP = "BasePropertyResultMap";
44+
private XmlElement selectByExampleSelectiveEle;
45+
private XmlElement selectByPrimaryKeySelectiveEle;
46+
private XmlElement basePropertyResultMapEle;
4747

4848
/**
4949
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
@@ -62,6 +62,20 @@ public boolean validate(List<String> warnings) {
6262
return super.validate(warnings);
6363
}
6464

65+
/**
66+
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
67+
* @param introspectedTable
68+
*/
69+
@Override
70+
public void initialized(IntrospectedTable introspectedTable) {
71+
super.initialized(introspectedTable);
72+
73+
// bug:26,27
74+
this.selectByExampleSelectiveEle = null;
75+
this.selectByPrimaryKeySelectiveEle = null;
76+
this.basePropertyResultMapEle = null;
77+
}
78+
6579
// =========================================== client 方法生成 ===================================================
6680

6781
@Override
@@ -121,18 +135,23 @@ public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface
121135
// ============================================== sqlMap 生成 ===================================================
122136

123137
@Override
124-
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
125-
return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
138+
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
139+
this.selectByExampleSelectiveEle = this.generateSelectSelectiveElement(METHOD_SELECT_BY_EXAMPLE_SELECTIVE, introspectedTable, false, true);
140+
return super.clientSelectByExampleWithBLOBsMethodGenerated(method, topLevelClass, introspectedTable);
126141
}
127142

128143
@Override
129-
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
130-
return super.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable);
144+
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
145+
if (!introspectedTable.hasBLOBColumns()) {
146+
this.selectByExampleSelectiveEle = this.generateSelectSelectiveElement(METHOD_SELECT_BY_EXAMPLE_SELECTIVE, introspectedTable, false, true);
147+
}
148+
return super.clientSelectByExampleWithoutBLOBsMethodGenerated(method, topLevelClass, introspectedTable);
131149
}
132150

133151
@Override
134-
public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
135-
return super.sqlMapSelectByPrimaryKeyElementGenerated(element, introspectedTable);
152+
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
153+
this.selectByPrimaryKeySelectiveEle = this.generateSelectSelectiveElement(METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE, introspectedTable, false, false);
154+
return super.clientSelectByPrimaryKeyMethodGenerated(method, topLevelClass, introspectedTable);
136155
}
137156

138157
/**
@@ -144,27 +163,19 @@ public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, Intr
144163
@Override
145164
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
146165
// issues#16
147-
if (introspectedTable.isConstructorBased()) {
148-
XmlElement resultMapEle = new XmlElement("resultMap");
149-
resultMapEle.addAttribute(new Attribute("id", ID_FOR_PROPERTY_BASED_RESULT_MAP));
150-
resultMapEle.addAttribute(new Attribute("type", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
151-
commentGenerator.addComment(resultMapEle);
152-
153-
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
154-
resultMapEle.addElement(XmlElementGeneratorTools.generateResultMapResultElement("id", introspectedColumn));
155-
}
156-
for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
157-
resultMapEle.addElement(XmlElementGeneratorTools.generateResultMapResultElement("result", introspectedColumn));
158-
}
159-
160-
document.getRootElement().getElements().add(0, resultMapEle);
166+
if (this.basePropertyResultMapEle != null) {
167+
document.getRootElement().addElement(0, this.basePropertyResultMapEle);
161168
}
162169

163170
// 1. selectByExampleSelective 方法
164-
FormatTools.addElementWithBestPosition(document.getRootElement(), this.generateSelectSelectiveElement(METHOD_SELECT_BY_EXAMPLE_SELECTIVE, introspectedTable, false, true));
171+
if (this.selectByExampleSelectiveEle != null) {
172+
FormatTools.addElementWithBestPosition(document.getRootElement(), this.selectByExampleSelectiveEle);
173+
}
165174

166175
// 2. selectByPrimaryKeySelective
167-
FormatTools.addElementWithBestPosition(document.getRootElement(), this.generateSelectSelectiveElement(METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE, introspectedTable, false, false));
176+
if (this.selectByPrimaryKeySelectiveEle != null) {
177+
FormatTools.addElementWithBestPosition(document.getRootElement(), this.selectByPrimaryKeySelectiveEle);
178+
}
168179

169180
return true;
170181
}
@@ -232,6 +243,19 @@ private XmlElement generateSelectSelectiveElement(String id, IntrospectedTable i
232243
selectSelectiveEle.addAttribute(new Attribute("id", id));
233244
// issues#16
234245
if (introspectedTable.isConstructorBased()) {
246+
XmlElement resultMapEle = new XmlElement("resultMap");
247+
resultMapEle.addAttribute(new Attribute("id", ID_FOR_PROPERTY_BASED_RESULT_MAP));
248+
resultMapEle.addAttribute(new Attribute("type", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
249+
commentGenerator.addComment(resultMapEle);
250+
251+
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
252+
resultMapEle.addElement(XmlElementGeneratorTools.generateResultMapResultElement("id", introspectedColumn));
253+
}
254+
for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
255+
resultMapEle.addElement(XmlElementGeneratorTools.generateResultMapResultElement("result", introspectedColumn));
256+
}
257+
this.basePropertyResultMapEle = resultMapEle;
258+
235259
selectSelectiveEle.addAttribute(new Attribute("resultMap", ID_FOR_PROPERTY_BASED_RESULT_MAP));
236260
} else if (introspectedTable.hasBLOBColumns()) {
237261
selectSelectiveEle.addAttribute(new Attribute("resultMap", introspectedTable.getResultMapWithBLOBsId()));

0 commit comments

Comments
 (0)