Browse Source

修改 updateById sqlSet ew 异常

hubin 7 years ago
parent
commit
47e8d74358

+ 8 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -117,11 +117,12 @@ public abstract class AbstractMethod {
      * </p>
      *
      * @param selective 是否选择判断
+     * @param ew        是否存在 UpdateWrapper 条件
      * @param table     表信息
      * @param prefix    前缀
      * @return
      */
-    protected String sqlSet(boolean selective, TableInfo table, String prefix) {
+    protected String sqlSet(boolean selective, boolean ew, TableInfo table, String prefix) {
         StringBuilder set = new StringBuilder();
         set.append("<trim prefix=\"SET\" suffixOverrides=\",\">");
 
@@ -155,8 +156,10 @@ public abstract class AbstractMethod {
             }
         }
         // UpdateWrapper SqlSet 部分
-        set.append("<if test=\"ew != null and ew.sqlSet != null\">${ew.sqlSet}</if>")
-            .append("</trim>");
+        if (ew) {
+            set.append("<if test=\"ew != null and ew.sqlSet != null\">${ew.sqlSet}</if>");
+        }
+        set.append("</trim>");
         return set.toString();
     }
 
@@ -314,8 +317,8 @@ public abstract class AbstractMethod {
             .append("<if test=\"cm!=null and !cm.isEmpty\">")
             .append("<where>")
             .append("<foreach collection=\"cm\" index=\"k\" item=\"v\" separator=\"AND\">")
-            .append("<choose><when test=\"v==null\">${k} IS NULL</when>")
-            .append("<otherwise>${k}=#{v}</otherwise></choose>")
+            .append("<choose><when test=\"v==null\"> ${k} IS NULL </when>")
+            .append("<otherwise> ${k}=#{v} </otherwise></choose>")
             .append("</foreach>")
             .append("</where>")
             .append("</if>")

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Update.java

@@ -35,7 +35,7 @@ public class Update extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.UPDATE;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, tableInfo, "et."),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, true, tableInfo, "et."),
             this.sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateAllColumnById.java

@@ -37,7 +37,7 @@ public class UpdateAllColumnById extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.UPDATE_ALL_COLUMN_BY_ID;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(false, tableInfo, "et."),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(false, false, tableInfo, "et."),
             tableInfo.getKeyColumn(), "et." + tableInfo.getKeyProperty(),
             "<if test=\"et instanceof java.util.Map\">"
             + "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateById.java

@@ -35,7 +35,7 @@ public class UpdateById extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
-        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, tableInfo, "et."),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, false, tableInfo, "et."),
             tableInfo.getKeyColumn(), new StringBuilder("et.").append(tableInfo.getKeyProperty()).toString(),
             new StringBuilder("<if test=\"et instanceof java.util.Map\">")
                 .append("<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">")

+ 6 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserMapperTest.java

@@ -89,6 +89,7 @@ public class H2UserMapperTest extends BaseTest {
         Assert.assertTrue(count == userMapper.deleteBatchIds(h2UserList
             .stream().map(u -> u.getId()).collect(toList())));
 
+        // 更新
         h2User = new H2User();
         h2User.setAge(2);
         h2User.setDesc("测试置空");
@@ -106,6 +107,11 @@ public class H2UserMapperTest extends BaseTest {
 
         log(userMapper.selectOne(new H2User().setName(NQQ).setAge(3)));
 
+        // 根据主键更新 age = 18
+        h2User.setAge(18);
+        Assert.assertNotNull(1 == userMapper.updateById(h2User));
+
+        // 查询一条记录
         Assert.assertNotNull(userMapper.selectOne(new H2User().setName(NQQ)));
 
         log(h2User.toString());