diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
index d7f49d3..7944337 100644
--- a/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+++ b/src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
@@ -352,19 +352,28 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
@Override
public boolean clientUpdateWithVersionByExampleSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
- // column枚举,找出全字段对应的Model
- FullyQualifiedJavaType fullFieldModel = introspectedTable.getRules().calculateAllFieldsClass();
- FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(fullFieldModel.getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
- method.addParameter(new Parameter(selectiveType, "selective", "@Param(\"selective\")", true));
+ // issue#69 OptimisticLockerPlugin 插件updateWithVersionByExampleSelective方法的生成是基于updateByExampleSelective的,
+ // 这个方法在配置了SelectiveEnhancedPlugin时可能已经被先配置的SelectiveEnhancedPlugin改变了
+ if (!"selective".equals(method.getParameters().get(method.getParameters().size() - 1).getName())) {
+ // column枚举,找出全字段对应的Model
+ FullyQualifiedJavaType fullFieldModel = introspectedTable.getRules().calculateAllFieldsClass();
+ FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(fullFieldModel.getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
+ method.addParameter(new Parameter(selectiveType, "selective", "@Param(\"selective\")", true));
+ }
+
return true;
}
@Override
public boolean clientUpdateWithVersionByPrimaryKeySelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
- // column枚举,找出全字段对应的Model
- FullyQualifiedJavaType fullFieldModel = introspectedTable.getRules().calculateAllFieldsClass();
- FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(fullFieldModel.getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
- method.addParameter(new Parameter(selectiveType, "selective", "@Param(\"selective\")", true));
+ // issue#69 OptimisticLockerPlugin 插件updateWithVersionByExampleSelective方法的生成是基于updateByExampleSelective的,
+ // 这个方法在配置了SelectiveEnhancedPlugin时可能已经被先配置的SelectiveEnhancedPlugin改变了
+ if (!"selective".equals(method.getParameters().get(method.getParameters().size() - 1).getName())) {
+ // column枚举,找出全字段对应的Model
+ FullyQualifiedJavaType fullFieldModel = introspectedTable.getRules().calculateAllFieldsClass();
+ FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(fullFieldModel.getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
+ method.addParameter(new Parameter(selectiveType, "selective", "@Param(\"selective\")", true));
+ }
return true;
}
diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/BugFixedTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/BugFixedTest.java
index e44a330..4476400 100644
--- a/src/test/java/com/itfsw/mybatis/generator/plugins/BugFixedTest.java
+++ b/src/test/java/com/itfsw/mybatis/generator/plugins/BugFixedTest.java
@@ -24,6 +24,7 @@ import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.MyBatisGenerator;
import java.lang.reflect.Array;
+import java.sql.ResultSet;
import java.util.List;
/**
@@ -155,9 +156,63 @@ public class BugFixedTest {
MyBatisGenerator generator = tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-63.sql"));
for (GeneratedJavaFile file : generator.getGeneratedJavaFiles()) {
String fileName = file.getFileName();
- if (fileName.startsWith("Repaydetail")){
+ if (fileName.startsWith("Repaydetail")) {
Assert.assertTrue("官方自己的问题", true);
}
}
}
+
+ /**
+ * 乐观锁插件配合SelectiveEnhancedPlugin多生成了selective参数的问题
+ * https://github.com/itfsw/mybatis-generator-plugin/issues/69
+ * @throws Exception
+ */
+ @Test
+ public void issues69() throws Exception {
+ for (int i = 0; i < 2; i++) {
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/issues-69-" + i + ".xml");
+ tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-69.sql"), new AbstractShellCallback() {
+ @Override
+ public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
+ ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
+
+ ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
+ ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
+ criteria.invoke("andIdEqualTo", 1l);
+
+ ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
+ tb.set("id", 1L);
+ tb.set("version", 152L);
+ tb.set("incF2", 10L);
+ tb.set("incF3", 5L);
+
+ // selective
+ ObjectUtil TbColumnId = new ObjectUtil(loader, packagz + ".Tb$Column#id");
+ ObjectUtil TbColumnVersion = new ObjectUtil(loader, packagz + ".Tb$Column#version");
+ ObjectUtil TbColumnIncF2 = new ObjectUtil(loader, packagz + ".Tb$Column#incF2");
+ Object columns = Array.newInstance(TbColumnId.getCls(), 3);
+ Array.set(columns, 0, TbColumnId.getObject());
+ Array.set(columns, 1, TbColumnVersion.getObject());
+ Array.set(columns, 2, TbColumnIncF2.getObject());
+
+ // sql
+ String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateWithVersionByExampleSelective", 100L, tb.getObject(), tbExample.getObject(), columns);
+ Assert.assertEquals(sql, "update tb SET version = version + 1, id = 1 , inc_f2 = 10 WHERE version = 100 and ( ( id = '1' ) )");
+
+ // 执行一次,因为版本号100不存在所以应该返回0
+ Object result = tbMapper.invoke("updateWithVersionByExampleSelective", 100L, tb.getObject(), tbExample.getObject(), columns);
+ Assert.assertEquals(result, 0);
+
+ // id = 1 的版本号应该是0
+ result = tbMapper.invoke("updateWithVersionByExampleSelective", 0L, tb.getObject(), tbExample.getObject(), columns);
+ Assert.assertEquals(result, 1);
+
+ // 执行完成后版本号应该加1
+ ResultSet rs = DBHelper.execute(sqlSession.getConnection(), "select * from tb where id = 1");
+ rs.first();
+ Assert.assertEquals(rs.getInt("version"), 1);
+ }
+ });
+ }
+ }
}
diff --git a/src/test/resources/scripts/BugFixedTest/issues-69-0.xml b/src/test/resources/scripts/BugFixedTest/issues-69-0.xml
new file mode 100644
index 0000000..714d16a
--- /dev/null
+++ b/src/test/resources/scripts/BugFixedTest/issues-69-0.xml
@@ -0,0 +1,61 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/BugFixedTest/issues-69-1.xml b/src/test/resources/scripts/BugFixedTest/issues-69-1.xml
new file mode 100644
index 0000000..a5ce36a
--- /dev/null
+++ b/src/test/resources/scripts/BugFixedTest/issues-69-1.xml
@@ -0,0 +1,60 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/BugFixedTest/issues-69.sql b/src/test/resources/scripts/BugFixedTest/issues-69.sql
new file mode 100644
index 0000000..33c97a0
--- /dev/null
+++ b/src/test/resources/scripts/BugFixedTest/issues-69.sql
@@ -0,0 +1,37 @@
+/*
+Navicat MySQL Data Transfer
+
+Source Server : localhost
+Source Server Version : 50617
+Source Host : localhost:3306
+Source Database : mybatis-generator-plugin
+
+Target Server Type : MYSQL
+Target Server Version : 50617
+File Encoding : 65001
+
+Date: 2017-07-05 17:21:41
+*/
+
+SET FOREIGN_KEY_CHECKS=0;
+
+-- ----------------------------
+-- Table structure for tb
+-- ----------------------------
+DROP TABLE IF EXISTS `tb`;
+CREATE TABLE `tb` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `version` bigint(20) NOT NULL DEFAULT '0',
+ `inc_f2` bigint(20) DEFAULT '0',
+ `inc_f3` bigint(20) DEFAULT '0',
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb
+-- ----------------------------
+INSERT INTO `tb` VALUES ('1', 'fd1', '0', '2', '0');
+INSERT INTO `tb` VALUES ('2', 'fd2', '1', '2', '3');
+INSERT INTO `tb` VALUES ('3', null, '3', '2', '1');
+INSERT INTO `tb` VALUES ('4', 'fd3', '1', '1', '1');
\ No newline at end of file