Browse Source

解决逻辑删除 BUG

hubin 7 years ago
parent
commit
58e237a00b

+ 18 - 21
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/LogicAbstractMethod.java

@@ -90,8 +90,8 @@ public abstract class LogicAbstractMethod extends AbstractMethod {
     protected String sqlWhereEntityWrapper(TableInfo table) {
         if (table.isLogicDelete()) {
             StringBuilder where = new StringBuilder(128);
-            where.append("<where>");
-            where.append("<choose><when test=\"ew!=null\">");
+            where.append("<if test=\"ew!=null and !ew.emptyOfWhere\">");
+            where.append("<trim prefix=\"WHERE\" prefixOverrides=\"AND|OR\">");
             where.append("<if test=\"ew.entity!=null\">");
             if (StringUtils.isNotEmpty(table.getKeyProperty())) {
                 where.append("<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">");
@@ -107,12 +107,12 @@ public abstract class LogicAbstractMethod extends AbstractMethod {
                 where.append(convertIfTag(fieldInfo, true));
             }
             where.append("</if>");
+            // 删除逻辑
             where.append(getLogicDeleteSql(table));
-            where.append("<if test=\"ew.sqlSegment!=null\">${ew.sqlSegment}</if>");
-            where.append("</when><otherwise>");
-            where.append(getLogicDeleteSql(table));
-            where.append("</otherwise></choose>");
-            where.append("</where>");
+            // SQL 片段
+            where.append("<if test=\"ew.sqlSegment!=null and ew.sqlSegment!=''\"> AND ${ew.sqlSegment}</if>");
+            where.append("</trim>");
+            where.append("</if>");
             return where.toString();
         }
         // 正常逻辑
@@ -122,20 +122,17 @@ public abstract class LogicAbstractMethod extends AbstractMethod {
     @Override
     protected String sqlWhereByMap(TableInfo table) {
         if (table.isLogicDelete()) {
-            StringBuilder where = new StringBuilder();
-            where.append("<where>");
-            // MAP 逻辑
-            where.append("<if test=\"cm!=null and !cm.isEmpty\">");
-            where.append("<foreach collection=\"cm.keys\" item=\"k\" separator=\"AND\">");
-            where.append("<if test=\"cm[k] != null\">");
-            where.append("${k} = #{cm[${k}]}");
-            where.append("</if>");
-            where.append("</foreach>");
-            where.append("</if>");
-            // 过滤逻辑
-            where.append(getLogicDeleteSql(table));
-            where.append("</where>");
-            return where.toString();
+            return new StringBuilder()
+                .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("</foreach>")
+                .append(getLogicDeleteSql(table))
+                .append("</where>")
+                .append("</if>")
+                .toString();
         }
         // 正常逻辑
         return super.sqlWhereByMap(table);

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

@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.test.base.entity.TestData;
 import com.baomidou.mybatisplus.test.h2.config.H2Db;
 import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMapper;
 import com.baomidou.mybatisplus.test.h2.entity.persistent.H2User;
@@ -138,4 +139,12 @@ public class H2UserMapperTest extends BaseTest {
             new QueryWrapper<>(new H2User().setAge(3)).eq("name", NQQ))));
     }
 
+
+    @Test
+    public void update() {
+        UpdateWrapper uw = new UpdateWrapper<TestData>();
+        uw.set("age", 1);
+        uw.eq("test_id", 101L);
+        userMapper.update(new H2User().setName("咩咩"), uw);
+    }
 }

+ 6 - 4
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/config/MybatisPlusConfig.java

@@ -13,6 +13,7 @@ import org.springframework.core.io.ResourceLoader;
 
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
 import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
@@ -62,10 +63,11 @@ public class MybatisPlusConfig {
     @Bean
     public GlobalConfig globalConfiguration() {
         GlobalConfig conf = new GlobalConfig();
-//        LogicSqlInjector logicSqlInjector = new LogicSqlInjector();
-//        conf.setLogicDeleteValue("-1");
-//        conf.setLogicNotDeleteValue("1");
-        conf.setDbConfig(new GlobalConfig.DbConfig().setIdType(IdType.ID_WORKER));
+        conf.setSqlInjector(new LogicSqlInjector());
+        conf.setDbConfig(new GlobalConfig.DbConfig()
+            .setLogicDeleteValue("1")
+            .setLogicNotDeleteValue("0")
+            .setIdType(IdType.ID_WORKER));
         return conf;
     }
 }

+ 5 - 7
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/SuSuperEntity.java

@@ -5,6 +5,9 @@ import java.util.Date;
 import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.TableField;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 多层集成测试
@@ -14,16 +17,11 @@ import com.baomidou.mybatisplus.annotation.TableField;
  * @author yuxiaobin
  * @since 2017/12/7
  */
+@Data
+@Accessors(chain = true)
 public abstract class SuSuperEntity {
 
     @TableField(value = "last_updated_dt", fill = FieldFill.UPDATE)
     private Date lastUpdatedDt;
 
-    public Date getLastUpdatedDt() {
-        return lastUpdatedDt;
-    }
-
-    public void setLastUpdatedDt(Date lastUpdatedDt) {
-        this.lastUpdatedDt = lastUpdatedDt;
-    }
 }

+ 5 - 7
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/SuperEntity.java

@@ -19,6 +19,9 @@ import java.io.Serializable;
 
 import com.baomidou.mybatisplus.annotation.TableId;
 
+import lombok.Data;
+import lombok.experimental.Accessors;
+
 /**
  * <p>
  * 测试父类情况
@@ -27,18 +30,13 @@ import com.baomidou.mybatisplus.annotation.TableId;
  * @author hubin
  * @since 2016-06-26
  */
+@Data
+@Accessors(chain = true)
 public class SuperEntity extends SuSuperEntity implements Serializable {
 
     /* 主键ID 注解,value 字段名,type 用户输入ID */
     @TableId(value = "test_id")
     private Long id;
 
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
 }
 

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

@@ -20,6 +20,7 @@ import java.util.Date;
 
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.baomidou.mybatisplus.annotation.Version;
 import com.baomidou.mybatisplus.test.h2.entity.SuperEntity;
@@ -60,6 +61,9 @@ public class H2User extends SuperEntity {
     @Version
     private Integer version;
 
+    @TableLogic
+    private Integer deleted;
+
 
     public H2User() {
 

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -59,7 +59,7 @@ public class MysqlTestDataMapperTest {
     @Test
     public void updateTest() {
         mapper.update(new TestData().setTestInt(222222222), new UpdateWrapper<TestData>()
-            .set("test_str", "我佛慈悲2").eq("id",1014132605058056193L));//todo 发现一个bug
+            .set("test_str", "我佛慈悲2").eq("id",1014132605058056193L));
     }
 
     @Test

+ 1 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/config/MybatisPlusConfig.java

@@ -2,19 +2,18 @@ package com.baomidou.mybatisplus.test.mysql.config;
 
 import javax.sql.DataSource;
 
-import com.baomidou.mybatisplus.annotation.IdType;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.type.JdbcType;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
 import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
-import com.baomidou.mybatisplus.test.h2.H2MetaObjectHandler;
 import com.baomidou.mybatisplus.test.mysql.MysqlMetaObjectHandler;
 
 /**

+ 1 - 0
mybatis-plus/src/test/resources/h2/user.ddl.sql

@@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS  h2user (
 	desc VARCHAR(30) NULL DEFAULT NULL ,
 	version INT(5) NULL DEFAULT NULL,
 	last_updated_dt TIMESTAMP NULL,
+	deleted INT(1) NULL DEFAULT 0 ,
 	PRIMARY KEY (test_id)
 )
 

+ 5 - 5
mybatis-plus/src/test/resources/h2/user.insert.sql

@@ -1,5 +1,5 @@
-insert into h2user(test_id, name, test_date, age, price, test_type, version) values (101,'Tomcat', '2017-1-1 1:1:1', 3, 9.99, 1, 1),
-insert into h2user(test_id, name, test_date, age, price, test_type, version) values (102,'Jerry', '2017-3-1 1:1:1', 2, 19.99, 2, 2);
-insert into h2user(test_id, name, test_date, age, price, test_type, version) values (103,'Bob', '2017-4-1 1:1:1', 13, 99.99, 3, 3);
-insert into h2user(test_id, name, test_date, age, price, test_type, version) values (104,'Joe', '2017-2-1 1:1:1', 18, 1.99, 1, 4);
-insert into h2user(test_id, name, test_date, age, price, test_type, version) values (105,'Tony', '2017-2-1 1:1:1', 18, 1.99, 1, 5);
+insert into h2user(test_id, name, test_date, age, price, test_type, version, deleted) values (101,'Tomcat', '2017-1-1 1:1:1', 3, 9.99, 1, 1, 0),
+insert into h2user(test_id, name, test_date, age, price, test_type, version, deleted) values (102,'Jerry', '2017-3-1 1:1:1', 2, 19.99, 2, 2, 0);
+insert into h2user(test_id, name, test_date, age, price, test_type, version, deleted) values (103,'Bob', '2017-4-1 1:1:1', 13, 99.99, 3, 3, 0);
+insert into h2user(test_id, name, test_date, age, price, test_type, version, deleted) values (104,'Joe', '2017-2-1 1:1:1', 18, 1.99, 1, 4, 0);
+insert into h2user(test_id, name, test_date, age, price, test_type, version, deleted) values (105,'Tony', '2017-2-1 1:1:1', 18, 1.99, 1, 5, 0);