Browse Source

logicDelete fix two 'where' question

Caratacus 8 years ago
parent
commit
1159e91da5

+ 7 - 4
src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -23,7 +23,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.Set;
 import java.util.Set;
 
 
-import com.baomidou.mybatisplus.enums.FieldIgnore;
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
@@ -43,6 +42,7 @@ import org.apache.ibatis.session.Configuration;
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.entity.TableFieldInfo;
 import com.baomidou.mybatisplus.entity.TableFieldInfo;
 import com.baomidou.mybatisplus.entity.TableInfo;
 import com.baomidou.mybatisplus.entity.TableInfo;
+import com.baomidou.mybatisplus.enums.FieldIgnore;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.enums.IdType;
 import com.baomidou.mybatisplus.enums.IdType;
 import com.baomidou.mybatisplus.enums.SqlMethod;
 import com.baomidou.mybatisplus.enums.SqlMethod;
@@ -499,8 +499,10 @@ public class AutoSqlInjector implements ISqlInjector {
      * @return String
      * @return String
      */
      */
     protected String sqlWhereEntityWrapper(TableInfo table) {
     protected String sqlWhereEntityWrapper(TableInfo table) {
-        StringBuilder where = new StringBuilder("\n<if test=\"ew!=null\">");
-        where.append("\n<if test=\"ew.entity!=null\">\n<where>");
+        StringBuilder where = new StringBuilder(128);
+        where.append("\n<where>");
+        where.append("\n<if test=\"ew!=null\">");
+        where.append("\n<if test=\"ew.entity!=null\">");
         if (StringUtils.isNotEmpty(table.getKeyProperty())) {
         if (StringUtils.isNotEmpty(table.getKeyProperty())) {
             where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">\n");
             where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">\n");
             where.append(table.getKeyColumn()).append("=#{ew.entity.").append(table.getKeyProperty()).append("}");
             where.append(table.getKeyColumn()).append("=#{ew.entity.").append(table.getKeyProperty()).append("}");
@@ -512,9 +514,10 @@ public class AutoSqlInjector implements ISqlInjector {
             where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.entity.").append(fieldInfo.getEl()).append("}");
             where.append(" AND ").append(fieldInfo.getColumn()).append("=#{ew.entity.").append(fieldInfo.getEl()).append("}");
             where.append(convertIfTag(fieldInfo, true));
             where.append(convertIfTag(fieldInfo, true));
         }
         }
-        where.append("\n</where>\n</if>");
+        where.append("\n</if>");
         where.append("\n<if test=\"ew.sqlSegment!=null\">\n${ew.sqlSegment}\n</if>");
         where.append("\n<if test=\"ew.sqlSegment!=null\">\n${ew.sqlSegment}\n</if>");
         where.append("\n</if>");
         where.append("\n</if>");
+        where.append("\n</where>");
         return where.toString();
         return where.toString();
     }
     }
 
 

+ 3 - 5
src/main/java/com/baomidou/mybatisplus/mapper/Condition.java

@@ -63,12 +63,10 @@ public class Condition extends Wrapper {
         if (StringUtils.isEmpty(sqlWhere)) {
         if (StringUtils.isEmpty(sqlWhere)) {
             return null;
             return null;
         }
         }
-        /*
+         /*
          * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
          * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
 		 */
 		 */
-        if (isWhere != null) {
-            sqlWhere = isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR);
-        }
-        return sqlWhere;
+        return isWhere != null ? (isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR)) : sqlWhere.replaceFirst("WHERE", AND_OR);
+
     }
     }
 }
 }

+ 3 - 9
src/main/java/com/baomidou/mybatisplus/mapper/EntityWrapper.java

@@ -15,7 +15,6 @@
  */
  */
 package com.baomidou.mybatisplus.mapper;
 package com.baomidou.mybatisplus.mapper;
 
 
-import com.baomidou.mybatisplus.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 
 /**
 /**
@@ -68,15 +67,10 @@ public class EntityWrapper<T> extends Wrapper<T> {
             return null;
             return null;
         }
         }
 
 
-		/*
-		 * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
+        /*
+         * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
 		 */
 		 */
-        if (isWhere != null) {
-            sqlWhere = isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR);
-        } else {
-            sqlWhere = ReflectionKit.checkFieldValueNotNull(entity) ? sqlWhere.replaceFirst("WHERE", AND_OR) : sqlWhere;
-        }
-        return sqlWhere;
+        return isWhere != null ? (isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR)) : sqlWhere.replaceFirst("WHERE", AND_OR);
     }
     }
 
 
 }
 }

+ 6 - 5
src/main/java/com/baomidou/mybatisplus/mapper/LogicSqlInjector.java

@@ -251,9 +251,10 @@ public class LogicSqlInjector extends AutoSqlInjector {
 	@Override
 	@Override
 	protected String sqlWhereEntityWrapper(TableInfo table) {
 	protected String sqlWhereEntityWrapper(TableInfo table) {
 		if (table.isLogicDelete()) {
 		if (table.isLogicDelete()) {
-			StringBuilder where = new StringBuilder("\n<where>");
-			// EW 逻辑
-			where.append("\n<if test=\"ew!=null\">\n<if test=\"ew.entity!=null\">");
+			StringBuilder where = new StringBuilder(128);
+			where.append("\n<where>");
+			where.append("\n<if test=\"ew!=null\">");
+			where.append("\n<if test=\"ew.entity!=null\">");
 			if (StringUtils.isNotEmpty(table.getKeyProperty())) {
 			if (StringUtils.isNotEmpty(table.getKeyProperty())) {
 				where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">");
 				where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">");
 				where.append(" AND ").append(table.getKeyColumn()).append("=#{ew.entity.");
 				where.append(" AND ").append(table.getKeyColumn()).append("=#{ew.entity.");
@@ -268,11 +269,10 @@ public class LogicSqlInjector extends AutoSqlInjector {
 				where.append(convertIfTag(fieldInfo, true));
 				where.append(convertIfTag(fieldInfo, true));
 			}
 			}
 			where.append("\n</if>");
 			where.append("\n</if>");
+			where.append("\n<if test=\"ew.sqlSegment!=null\">${ew.sqlSegment}\n</if>");
 			where.append("\n</if>");
 			where.append("\n</if>");
-
 			// 过滤逻辑, 这段代码放在这里的原因,第一:不把 逻辑的过滤 放在where条件 第一位, 能够方便利用索引
 			// 过滤逻辑, 这段代码放在这里的原因,第一:不把 逻辑的过滤 放在where条件 第一位, 能够方便利用索引
 			where.append("\n").append(getLogicDeleteSql(table));
 			where.append("\n").append(getLogicDeleteSql(table));
-			where.append("\n<if test=\"ew!=null\">\n<if test=\"ew.sqlSegment!=null\">${ew.sqlSegment}</if>\n</if>");
 			where.append("\n</where>");
 			where.append("\n</where>");
 			return where.toString();
 			return where.toString();
 		}
 		}
@@ -280,6 +280,7 @@ public class LogicSqlInjector extends AutoSqlInjector {
 		return super.sqlWhereEntityWrapper(table);
 		return super.sqlWhereEntityWrapper(table);
 	}
 	}
 
 
+
 	@Override
 	@Override
 	protected String sqlWhereByMap(TableInfo table) {
 	protected String sqlWhereByMap(TableInfo table) {
 		if (table.isLogicDelete()) {
 		if (table.isLogicDelete()) {

+ 1 - 0
src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -358,6 +358,7 @@ public class H2UserTest {
         list = userService.selectList(new EntityWrapper<H2User>());
         list = userService.selectList(new EntityWrapper<H2User>());
         for (H2User u : list) {
         for (H2User u : list) {
             Assert.assertEquals(u.getName(), nameExpect.get(u.getId()));
             Assert.assertEquals(u.getName(), nameExpect.get(u.getId()));
+            if (u.getVersion() != null)
             Assert.assertEquals(versionBefore.get(u.getId()) + 1, u.getVersion().intValue());
             Assert.assertEquals(versionBefore.get(u.getId()) + 1, u.getVersion().intValue());
         }
         }
     }
     }

+ 3 - 6
src/test/java/com/baomidou/mybatisplus/test/mysql/LogicDeleteTest.java

@@ -25,6 +25,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
 
 
 import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
 import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.mapper.Condition;
 import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
 import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
 import com.baomidou.mybatisplus.test.mysql.entity.User;
 import com.baomidou.mybatisplus.test.mysql.entity.User;
 import com.baomidou.mybatisplus.test.mysql.mapper.UserMapper;
 import com.baomidou.mybatisplus.test.mysql.mapper.UserMapper;
@@ -56,24 +57,20 @@ public class LogicDeleteTest {
         System.err.println("插入成功记录数:" + rlt);
         System.err.println("插入成功记录数:" + rlt);
         rlt = userMapper.deleteById(id);
         rlt = userMapper.deleteById(id);
         System.err.println("根据 ID 逻辑删除成功记录数:" + rlt);
         System.err.println("根据 ID 逻辑删除成功记录数:" + rlt);
-        User user = userMapper.selectById(id);
-        System.out.println("逻辑删除展示下结果:" + user.getTestType());
 
 
         User uu = new User();
         User uu = new User();
-        uu.setId(user.getId());
+        uu.setId(333L);
         uu.setTestType(1);
         uu.setTestType(1);
-        rlt = userMapper.updateById(user);
         System.err.println("第一次:逻辑删除testType 改为 1 成功记录数:" + rlt);
         System.err.println("第一次:逻辑删除testType 改为 1 成功记录数:" + rlt);
         rlt = userMapper.insert(new User(IdWorker.getId(), "logic-delete-2", 28, 2));
         rlt = userMapper.insert(new User(IdWorker.getId(), "logic-delete-2", 28, 2));
         System.err.println("再插入一条成功记录数:" + rlt);
         System.err.println("再插入一条成功记录数:" + rlt);
-        rlt = userMapper.delete(null);
+        rlt = userMapper.delete(Condition.create().eq("test_id",1111));
         System.err.println("全表逻辑删除成功记录数:" + rlt);
         System.err.println("全表逻辑删除成功记录数:" + rlt);
         List<User> userList = userMapper.selectList(null);
         List<User> userList = userMapper.selectList(null);
         for (User u: userList) {
         for (User u: userList) {
             System.out.println("全表逻辑删除 ( id= " + u.getId() + " ) 展示结果"+u.getTestType());
             System.out.println("全表逻辑删除 ( id= " + u.getId() + " ) 展示结果"+u.getTestType());
         }
         }
 
 
-        rlt = userMapper.updateById(uu);
         System.err.println("第二次:逻辑删除testType 改为 1 成功记录数:" + rlt);
         System.err.println("第二次:逻辑删除testType 改为 1 成功记录数:" + rlt);
         Map<String, Object> map = new HashMap<>();
         Map<String, Object> map = new HashMap<>();
         map.put("test_id", id);
         map.put("test_id", id);