Skip to content

Commit 40aed67

Browse files
author
hewei
committed
bugfix:插件SelectiveEnhancedPlugin之前命名的判断方法isSelective在进行Json Serializer 时某些情况下可能造成冲突,重新命名为hasSelective
1 parent 826ad11 commit 40aed67

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* ---------------------------------------------------------------------------
4141
*/
4242
public class SelectiveEnhancedPlugin extends BasePlugin {
43+
public static final String METHOD_HAS_SELECTIVE = "hasSelective"; // 方法名
4344

4445
/**
4546
* {@inheritDoc}
@@ -80,22 +81,22 @@ public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, Intros
8081
selectiveColumnsField.setInitializationString("new HashMap<String, Boolean>()");
8182
topLevelClass.addField(selectiveColumnsField);
8283

83-
// Method isSelective
84-
Method mIsSelective = new Method("isSelective");
85-
commentGenerator.addGeneralMethodComment(mIsSelective, introspectedTable);
86-
mIsSelective.setVisibility(JavaVisibility.PUBLIC);
87-
mIsSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
88-
mIsSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
89-
topLevelClass.addMethod(mIsSelective);
84+
// Method hasSelective
85+
Method mHasSelective = new Method(METHOD_HAS_SELECTIVE);
86+
commentGenerator.addGeneralMethodComment(mHasSelective, introspectedTable);
87+
mHasSelective.setVisibility(JavaVisibility.PUBLIC);
88+
mHasSelective.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
89+
mHasSelective.addBodyLine("return this.selectiveColumns.size() > 0;");
90+
topLevelClass.addMethod(mHasSelective);
9091

91-
// Method isSelective
92-
Method mIsSelective1 = new Method("isSelective");
93-
commentGenerator.addGeneralMethodComment(mIsSelective1, introspectedTable);
94-
mIsSelective1.setVisibility(JavaVisibility.PUBLIC);
95-
mIsSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
96-
mIsSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
97-
mIsSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;");
98-
topLevelClass.addMethod(mIsSelective1);
92+
// Method hasSelective
93+
Method mHasSelective1 = new Method(METHOD_HAS_SELECTIVE);
94+
commentGenerator.addGeneralMethodComment(mHasSelective1, introspectedTable);
95+
mHasSelective1.setVisibility(JavaVisibility.PUBLIC);
96+
mHasSelective1.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
97+
mHasSelective1.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "column"));
98+
mHasSelective1.addBodyLine("return this.selectiveColumns.get(column) != null;");
99+
topLevelClass.addMethod(mHasSelective1);
99100

100101
// Method selective
101102
Method mSelective = new Method("selective");
@@ -195,7 +196,7 @@ private void replaceEle(XmlElement element, String prefix, IntrospectedTable int
195196
XmlElement chooseEle = new XmlElement("choose");
196197
// when
197198
XmlElement whenEle = new XmlElement("when");
198-
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
199+
whenEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "()"));
199200
for (Element ele : element.getElements()) {
200201
// 对于字符串主键,是没有if判断节点的
201202
if (ele instanceof XmlElement) {
@@ -232,7 +233,7 @@ private void replaceEle(XmlElement element, String prefix, IntrospectedTable int
232233

233234
XmlElement ifEle = new XmlElement("if");
234235

235-
ifEle.addAttribute(new Attribute("test", prefix + "isSelective(\'" + column.getActualColumnName() + "\')"));
236+
ifEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "(\'" + column.getActualColumnName() + "\')"));
236237
for (Element ifChild : xmlElement.getElements()) {
237238
ifEle.addElement(ifChild);
238239
}
@@ -269,10 +270,10 @@ private void replaceEleForUpsertByExampleSelective(XmlElement element, String pr
269270
XmlElement chooseEle = new XmlElement("choose");
270271
// when
271272
XmlElement whenEle = new XmlElement("when");
272-
whenEle.addAttribute(new Attribute("test", prefix + "isSelective()"));
273+
whenEle.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "()"));
273274
for (IntrospectedColumn introspectedColumn : (allColumns ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns())) {
274275
XmlElement eleIf = new XmlElement("if");
275-
eleIf.addAttribute(new Attribute("test", prefix + "isSelective(\'" + introspectedColumn.getActualColumnName() + "\')"));
276+
eleIf.addAttribute(new Attribute("test", prefix + METHOD_HAS_SELECTIVE + "(\'" + introspectedColumn.getActualColumnName() + "\')"));
276277

277278
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
278279
whenEle.addElement(eleIf);

src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public void reloadProject(SqlSession sqlSession, ClassLoader loader, String pack
7979
Array.set(columns, 1, TbColumnTsIncF2.getObject());
8080

8181
tb.invoke("selective", columns);
82-
Assert.assertTrue((Boolean) tb.invoke("isSelective"));
83-
Assert.assertTrue((Boolean) tb.invoke("isSelective", "field_1"));
84-
Assert.assertFalse((Boolean) tb.invoke("isSelective", "inc_f1"));
85-
Assert.assertTrue((Boolean) tb.invoke("isSelective", "inc_f2"));
82+
Assert.assertTrue((Boolean) tb.invoke("hasSelective"));
83+
Assert.assertTrue((Boolean) tb.invoke("hasSelective", "field_1"));
84+
Assert.assertFalse((Boolean) tb.invoke("hasSelective", "inc_f1"));
85+
Assert.assertTrue((Boolean) tb.invoke("hasSelective", "inc_f2"));
8686
}
8787
});
8888
}

0 commit comments

Comments
 (0)