Переглянути джерело

UpdateWrapper 完美支持 空 null 对象设置

hubin 7 роки тому
батько
коміт
0e5660abc2

+ 3 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -158,7 +158,9 @@ public abstract class AbstractMethod {
                 set.append(fieldInfo.getEl()).append("},");
             }
         }
-        set.append("\n</trim>");
+        // UpdateWrapper SqlSet 部分
+        set.append("<if test=\"ew != null and ew.sqlSet != null\">${ew.sqlSet}</if>");
+        set.append("</trim>");
         return set.toString();
     }
 

+ 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(), sqlSet(true, tableInfo, "et."),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, tableInfo, "et."),
             this.sqlWhereEntityWrapper(tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);

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

@@ -24,18 +24,20 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
 
 /**
  * <p>
- * 根据 ID 更新所有字段
+ * 根据 ID 更新所有字段<br/>
+ * 该方法 3.1 删除,建议迁移到 update 使用 UpdateWrapper 处理空字段问题
  * </p>
  *
  * @author hubin
  * @since 2018-04-06
  */
+@Deprecated
 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(), sqlSet(false, tableInfo, "et."),
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(false, tableInfo, "et."),
             tableInfo.getKeyColumn(), "et." + tableInfo.getKeyProperty(),
             "<if test=\"et instanceof java.util.Map\">"
             + "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"

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

@@ -35,14 +35,12 @@ 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(), sqlSet(true, tableInfo, "et."),
-            tableInfo.getKeyColumn(), "et." + tableInfo.getKeyProperty(),
-            "<if test=\"et instanceof java.util.Map\">"
-            + "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">"
-            + "  AND ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}"
-            + "</if>"
-            + "</if>"
-        );
+        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlSet(true, 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\">")
+                .append("  AND ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}")
+                .append("</if></if>"));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
     }

+ 10 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserMapperTest.java

@@ -18,6 +18,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.test.h2.config.H2Db;
 import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMapper;
@@ -90,12 +91,20 @@ public class H2UserMapperTest extends BaseTest {
 
         h2User = new H2User();
         h2User.setAge(2);
+        h2User.setDesc("测试置空");
         Assert.assertTrue(1 == userMapper.update(h2User,
             new QueryWrapper<H2User>().eq("name", NQQ)));
 
+        log(userMapper.selectOne(new H2User().setName(NQQ).setAge(2)));
+
         h2User.setAge(3);
+        h2User.setDesc(null);
         Assert.assertTrue(1 == userMapper.update(h2User,
-            new QueryWrapper<H2User>().lambda().eq(H2User::getName, NQQ)));
+            new UpdateWrapper<H2User>().lambda()
+                .set(H2User::getDesc, "")
+                .eq(H2User::getName, NQQ)));
+
+        log(userMapper.selectOne(new H2User().setName(NQQ).setAge(3)));
 
         Assert.assertNotNull(userMapper.selectOne(new H2User().setName(NQQ)));
 

+ 11 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2User.java

@@ -101,4 +101,15 @@ public class H2User extends SuperEntity {
         this.testType = testType;
     }
 
+    @Override
+    public String toString() {
+        return null == this ? "h2user is null." : new StringBuilder()
+            .append("h2user:{name=").append(name).append(",")
+            .append("age=").append(age).append(",")
+            .append("price=").append(price).append(",")
+            .append("testType=").append(testType).append(",")
+            .append("desc=").append(desc).append(",")
+            .append("testDate=").append(testDate).append(",")
+            .append("version=").append(version).toString();
+    }
 }