瀏覽代碼

调整删除条件.

nieqiurong 4 月之前
父節點
當前提交
77ccb93e68

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

@@ -61,7 +61,8 @@ public class DeleteById extends AbstractMethod {
                 .collect(toList());
             if (CollectionUtils.isNotEmpty(fieldInfos)) {
                 String sqlSet = "SET " + SqlScriptUtils.convertIf(fieldInfos.stream()
-                    .map(i -> i.getSqlSet(EMPTY)).collect(joining(EMPTY)), "!@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(_parameter.getClass())", true)
+                    .map(i -> i.getSqlSet(EMPTY)).collect(joining(EMPTY)),
+                    "@org.apache.ibatis.reflection.SystemMetaObject@forObject(_parameter).findProperty('" + tableInfo.getKeyProperty() + "', false) != null", true)
                     + tableInfo.getLogicDeleteSql(false, false);
                 sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(),
                     tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));

+ 9 - 11
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByIds.java

@@ -62,16 +62,19 @@ public class DeleteByIds extends AbstractMethod {
             return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
         } else {
             sqlMethod = SqlMethod.DELETE_BY_IDS;
-            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
-                SqlScriptUtils.convertForeach(
-                    SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
-                        "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
-                    COLL, null, "item", COMMA));
+            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), getConvertForeachScript(tableInfo));
             SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
             return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
         }
     }
 
+    protected String getConvertForeachScript(TableInfo tableInfo) {
+        return SqlScriptUtils.convertForeach(
+            SqlScriptUtils.convertChoose("item!=null and @org.apache.ibatis.reflection.SystemMetaObject@forObject(item).findProperty('" + tableInfo.getKeyProperty() + "', false) != null",
+                "#{item." + tableInfo.getKeyProperty() + "}", "#{item}"),
+            COLL, null, "item", COMMA);
+    }
+
     /**
      * @param tableInfo 表信息
      * @return 逻辑删除脚本
@@ -89,12 +92,7 @@ public class DeleteByIds extends AbstractMethod {
         }
         sqlSet += StringPool.EMPTY + tableInfo.getLogicDeleteSql(false, false);
         return String.format(sqlMethod.getSql(), tableInfo.getTableName(),
-            sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach(
-                //TODO 待优化,看是否改成判断只使用实体(顺便考虑下子类)??
-                SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass()) or (mpFillEt!=null and item.getClass() != mpFillEt.getClass())",
-                    "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
-                COLL, null, "item", COMMA),
-            tableInfo.getLogicDeleteSql(true, true));
+            sqlSet, tableInfo.getKeyColumn(), getConvertForeachScript(tableInfo), tableInfo.getLogicDeleteSql(true, true));
     }
 
 }

+ 16 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/DeleteByIdDto.java

@@ -0,0 +1,16 @@
+package com.baomidou.mybatisplus.test.uuid;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class DeleteByIdDto<T> implements Serializable {
+
+    private T id;
+
+}

+ 0 - 8
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDEntity.java

@@ -13,12 +13,4 @@ public class UUIDEntity {
     private UUID id;
 
     private String name;
-
-    @TableField(fill = FieldFill.UPDATE)
-    private String deleteBy;
-
-    @TableField(fill = FieldFill.UPDATE)
-    @TableLogic(delval = "true", value = "false")
-    private Boolean deleted;
-
 }

+ 18 - 24
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDEntityTest.java

@@ -1,15 +1,13 @@
 package com.baomidou.mybatisplus.test.uuid;
 
-import com.baomidou.mybatisplus.core.config.GlobalConfig;
-import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.test.BaseDbTest;
-import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.session.Configuration;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 import java.util.function.Consumer;
 
@@ -21,7 +19,6 @@ public class UUIDEntityTest extends BaseDbTest<UUIDEntityMapper> {
             var uuidEntity = new UUIDEntity();
             uuidEntity.setId(UUID.randomUUID());
             uuidEntity.setName("test1");
-            uuidEntity.setDeleted(false);
             Assertions.assertEquals(1, m.insert(uuidEntity));
             Assertions.assertNotNull(m.selectById(uuidEntity.getId()));
             Assertions.assertEquals(1, m.deleteById(uuidEntity));
@@ -31,7 +28,6 @@ public class UUIDEntityTest extends BaseDbTest<UUIDEntityMapper> {
             var uuidEntity = new UUIDEntity();
             uuidEntity.setId(UUID.randomUUID());
             uuidEntity.setName("test2");
-            uuidEntity.setDeleted(false);
             Assertions.assertEquals(1, m.insert(uuidEntity));
             Assertions.assertEquals(1, m.deleteByIds(List.of(uuidEntity.getId())));
         });
@@ -40,10 +36,25 @@ public class UUIDEntityTest extends BaseDbTest<UUIDEntityMapper> {
             var uuidEntity = new UUIDEntity();
             uuidEntity.setId(UUID.randomUUID());
             uuidEntity.setName("test3");
-            uuidEntity.setDeleted(false);
             Assertions.assertEquals(1, m.insert(uuidEntity));
             Assertions.assertEquals(1, m.deleteByIds(List.of(uuidEntity)));
         });
+
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteByIds(
+            List.of(
+                UUID.randomUUID().toString(),
+                UUID.randomUUID(), 123, 321L,
+                Map.of("id", UUID.randomUUID()),
+                Map.of("id", UUID.randomUUID().toString()),
+                new DeleteByIdDto<>(UUID.randomUUID()),
+                new DeleteByIdDto<>(UUID.randomUUID().toString())
+            ))));
+
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(UUID.randomUUID())));
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(UUID.randomUUID().toString())));
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(new UUIDEntity(){})));
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(new DeleteByIdDto<>(UUID.randomUUID()))));
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(new DeleteByIdDto<>(UUID.randomUUID().toString()))));
     }
 
 
@@ -54,24 +65,9 @@ public class UUIDEntityTest extends BaseDbTest<UUIDEntityMapper> {
         };
     }
 
-    @Override
-    protected GlobalConfig globalConfig() {
-        return super.globalConfig().setMetaObjectHandler(new MetaObjectHandler() {
-            @Override
-            public void insertFill(MetaObject metaObject) {
-
-            }
-
-            @Override
-            public void updateFill(MetaObject metaObject) {
-                metaObject.setValue("deleteBy", "baomidou");
-            }
-        });
-    }
-
     @Override
     protected String tableDataSql() {
-        return "insert into entity values('0824eb71-e124-5ba1-56b9-87185d91f309','test',null, false)";
+        return "insert into entity values('0824eb71-e124-5ba1-56b9-87185d91f309','test')";
     }
 
     @Override
@@ -80,8 +76,6 @@ public class UUIDEntityTest extends BaseDbTest<UUIDEntityMapper> {
             "CREATE TABLE IF NOT EXISTS entity (\n" +
                 "id VARCHAR(50) NOT NULL,\n" +
                 "name VARCHAR(30) NULL DEFAULT NULL,\n" +
-                "delete_by VARCHAR(30) NULL DEFAULT NULL," +
-                "deleted BOOLEAN NOT NULL DEFAULT false," +
                 "PRIMARY KEY (id)" +
                 ")");
     }

+ 24 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDLogicEntity.java

@@ -0,0 +1,24 @@
+package com.baomidou.mybatisplus.test.uuid;
+
+import com.baomidou.mybatisplus.annotation.*;
+import lombok.Data;
+
+import java.util.UUID;
+
+@Data
+@TableName("entity")
+public class UUIDLogicEntity {
+
+    @TableId(value = "id",type = IdType.INPUT)
+    private UUID id;
+
+    private String name;
+
+    @TableField(fill = FieldFill.UPDATE)
+    private String deleteBy;
+
+    @TableField(fill = FieldFill.UPDATE)
+    @TableLogic(delval = "true", value = "false")
+    private Boolean deleted;
+
+}

+ 7 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDLogicEntityMapper.java

@@ -0,0 +1,7 @@
+package com.baomidou.mybatisplus.test.uuid;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+public interface UUIDLogicEntityMapper extends BaseMapper<UUIDLogicEntity> {
+
+}

+ 107 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/uuid/UUIDLogicEntityTest.java

@@ -0,0 +1,107 @@
+package com.baomidou.mybatisplus.test.uuid;
+
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import com.baomidou.mybatisplus.test.BaseDbTest;
+import org.apache.ibatis.reflection.MetaObject;
+import org.apache.ibatis.session.Configuration;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.function.Consumer;
+
+public class UUIDLogicEntityTest extends BaseDbTest<UUIDLogicEntityMapper> {
+
+    @Test
+    void test() {
+        doTest(m -> {
+            var uuidEntity = new UUIDLogicEntity();
+            uuidEntity.setId(UUID.randomUUID());
+            uuidEntity.setName("test1");
+            uuidEntity.setDeleted(false);
+            Assertions.assertEquals(1, m.insert(uuidEntity));
+            Assertions.assertNotNull(m.selectById(uuidEntity.getId()));
+            Assertions.assertEquals(1, m.deleteById(uuidEntity));
+        });
+
+        doTest(m -> {
+            var uuidEntity = new UUIDLogicEntity();
+            uuidEntity.setId(UUID.randomUUID());
+            uuidEntity.setName("test2");
+            uuidEntity.setDeleted(false);
+            Assertions.assertEquals(1, m.insert(uuidEntity));
+            Assertions.assertEquals(1, m.deleteByIds(List.of(uuidEntity.getId())));
+        });
+
+        doTest(m -> {
+            var uuidEntity = new UUIDLogicEntity();
+            uuidEntity.setId(UUID.randomUUID());
+            uuidEntity.setName("test3");
+            uuidEntity.setDeleted(false);
+            Assertions.assertEquals(1, m.insert(uuidEntity));
+            Assertions.assertEquals(1, m.deleteByIds(List.of(uuidEntity)));
+        });
+
+
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteByIds(
+            List.of(
+                UUID.randomUUID().toString(),
+                UUID.randomUUID(), 123, 321L,
+                Map.of("id", UUID.randomUUID()),
+                Map.of("id", UUID.randomUUID().toString()),
+                new DeleteByIdDto<>(UUID.randomUUID()),
+                new DeleteByIdDto<>(UUID.randomUUID().toString())
+            ))));
+
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(UUID.randomUUID())));
+        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(new UUIDLogicEntity(){})));
+        // TODO 下面三种类型无法转为UUID,看是否增加一个类型转换器让用户能注册处理自己的类型转换
+//        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(UUID.randomUUID().toString())));
+//        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(new DeleteByIdDto<>(UUID.randomUUID()))));
+//        doTest(m -> Assertions.assertDoesNotThrow(()-> m.deleteById(new DeleteByIdDto<>(UUID.randomUUID().toString()))));
+    }
+
+
+    @Override
+    protected Consumer<Configuration> consumer() {
+        return configuration -> {
+            configuration.getTypeHandlerRegistry().register(UUIDTypeHandler.class);
+        };
+    }
+
+    @Override
+    protected GlobalConfig globalConfig() {
+        return super.globalConfig().setMetaObjectHandler(new MetaObjectHandler() {
+            @Override
+            public void insertFill(MetaObject metaObject) {
+
+            }
+
+            @Override
+            public void updateFill(MetaObject metaObject) {
+                metaObject.setValue("deleteBy", "baomidou");
+            }
+        });
+    }
+
+    @Override
+    protected String tableDataSql() {
+        return "insert into entity values('0824eb71-e124-5ba1-56b9-87185d91f309','test',null, false)";
+    }
+
+    @Override
+    protected List<String> tableSql() {
+        return Arrays.asList("drop table if exists entity",
+            "CREATE TABLE IF NOT EXISTS entity (\n" +
+                "id VARCHAR(50) NOT NULL,\n" +
+                "name VARCHAR(30) NULL DEFAULT NULL,\n" +
+                "delete_by VARCHAR(30) NULL DEFAULT NULL," +
+                "deleted BOOLEAN NOT NULL DEFAULT false," +
+                "PRIMARY KEY (id)" +
+                ")");
+    }
+}