Browse Source

修复Bugs
1.like查询错误
2.对象不为空,但所有属性为空拼接sql错误

Caratacus 9 years ago
parent
commit
ada0e48a5e

+ 5 - 5
mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java

@@ -62,23 +62,23 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 	 *
 	 * @param entity
 	 *            实体对象
-	 * @param selective
+	 * @param isSelective
 	 *            true 选择字段 false 不选择字段
 	 * @return boolean
 	 */
 	@Transactional(rollbackFor = Exception.class)
-	public boolean insertOrUpdate(T entity, boolean selective) {
+	public boolean insertOrUpdate(T entity, boolean isSelective) {
 		if (null != entity) {
 			Class<?> cls = entity.getClass();
 			TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
 			if (null != tableInfo) {
 				try {
-					Method m = cls.getMethod("get" + StringUtils.capitalize(tableInfo.getKeyProperty()));
+					Method m = cls.getMethod(StringUtils.concatCapitalize("get",tableInfo.getKeyProperty()));
 					Object idVal = m.invoke(entity);
 					if (null != idVal) {
-						return selective ? updateSelectiveById(entity) : updateById(entity);
+						return isSelective ? updateSelectiveById(entity) : updateById(entity);
 					} else {
-						return selective ? insertSelective(entity) : insert(entity);
+						return isSelective ? insertSelective(entity) : insert(entity);
 					}
 				} catch (Exception e) {
 					e.printStackTrace();

+ 6 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/EntityWrapper.java

@@ -15,12 +15,13 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
+import com.baomidou.mybatisplus.toolkit.ReflectionKit;
+import com.baomidou.mybatisplus.toolkit.StringUtils;
+
 import java.io.Serializable;
 import java.text.MessageFormat;
 import java.util.List;
 
-import com.baomidou.mybatisplus.toolkit.StringUtils;
-
 /**
  * <p>
  * Entity 对象封装操作类,定义T-SQL语法
@@ -93,8 +94,9 @@ public class EntityWrapper<T> implements Serializable {
             return null;
         }
 
-        // 根据当前实体判断是否需要将WHERE替换成AND
-        sqlWhere = (null != entity) ? sqlWhere.replaceFirst("WHERE", "AND") : sqlWhere;
+        // 根据当前实体判断是否需要将WHERE替换成AND 增加实体不为空但所有属性为空的情况
+        sqlWhere = ((null != entity) && ReflectionKit.checkFieldValueNull(entity)) ? sqlWhere.replaceFirst("WHERE", "AND")
+                : sqlWhere;
 
 		/*
          * 使用防SQL注入处理后返回

+ 1 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/TSqlPlus.java

@@ -111,7 +111,7 @@ public class TSqlPlus extends MybatisAbstractSQL<TSqlPlus> {
 			if (isNot) {
 				inSql.append(" NOT");
 			}
-			inSql.append(MessageFormat.format(SQL_LIKE, "%", StringUtils.quotaMark(value), "%"));
+			inSql.append(MessageFormat.format(SQL_LIKE, "'%'", StringUtils.quotaMark(value), "'%'"));
 			WHERE(inSql.toString());
 		}
 	}

+ 89 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/ReflectionKit.java

@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.toolkit;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 反射工具类.
+ */
+public class ReflectionKit {
+
+	private static Logger logger = LoggerFactory.getLogger(ReflectionKit.class);
+
+	/**
+	 * 调用对象的get方法检查对象所以属性是否为null
+	 * 
+	 * @param bean
+	 * @return boolean true对象所有属性不为null,false对象所有属性为null
+	 */
+	public static boolean checkFieldValueNull(Object bean) {
+		boolean result = false;
+		if (bean == null) {
+			return true;
+		}
+		Class<?> cls = bean.getClass();
+		Method[] methods = cls.getDeclaredMethods();
+		TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
+
+		if (tableInfo == null) {
+			logger.warn("Error: Could not find @TableId.");
+			return false;
+		}
+		List<TableFieldInfo> fieldList = tableInfo.getFieldList();
+		for (TableFieldInfo tableFieldInfo : fieldList) {
+			String fieldGetName = StringUtils.concatCapitalize("get", tableFieldInfo.getProperty());
+			if (!checkMethod(methods, fieldGetName)) {
+				continue;
+			}
+			try {
+				Method method = cls.getMethod(fieldGetName);
+				Object obj = method.invoke(bean);
+				if (null != obj) {
+					result = true;
+					break;
+				}
+			} catch (Exception e) {
+				logger.warn("Unexpected exception on checkFieldValueNull.  Cause:" + e);
+			}
+
+		}
+		return result;
+	}
+
+	/**
+	 * 判断是否存在某属性的 get方法
+	 *
+	 * @param methods
+	 *            对象所有方法
+	 * @param method
+	 *            当前检查的方法
+	 * @return boolean true存在,false不存在
+	 */
+	public static boolean checkMethod(Method[] methods, String method) {
+		for (Method met : methods) {
+			if (method.equals(met.getName())) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+}

+ 17 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/StringUtils.java

@@ -164,12 +164,16 @@ public class StringUtils {
 	}
 
 	/**
-	 * 字符串第一个字母大写
-	 * 
+	 * 拼接字符串第二个字符串第一个字母大写
+	 *
+	 * @param concatStr
 	 * @param str
 	 * @return
 	 */
-	public static String capitalize(final String str) {
+	public static String concatCapitalize(String concatStr, final String str) {
+		if (isEmpty(concatStr)){
+			concatStr = EMPTY_STRING;
+		}
 		int strLen;
 		if (str == null || (strLen = str.length()) == 0) {
 			return str;
@@ -181,7 +185,16 @@ public class StringUtils {
 			return str;
 		}
 
-		return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
+		return new StringBuilder(strLen).append(concatStr).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
+	}
+	/**
+	 * 字符串第一个字母大写
+	 *
+	 * @param str
+	 * @return
+	 */
+	public static String capitalize(final String str) {
+		return concatCapitalize(null,str);
 	}
 
 }

+ 10 - 5
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/EntityWrapperTest.java

@@ -15,14 +15,14 @@
  */
 package com.baomidou.mybatisplus.test;
 
-import java.util.ArrayList;
-import java.util.List;
-
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
+import com.baomidou.mybatisplus.test.mysql.entity.User;
+import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 import org.junit.Assert;
 import org.junit.Test;
 
-import com.baomidou.mybatisplus.mapper.EntityWrapper;
-import com.baomidou.mybatisplus.test.mysql.entity.User;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * <p>
@@ -39,6 +39,11 @@ public class EntityWrapperTest {
      */
     private EntityWrapper<User> ew = new EntityWrapper<User>();
 
+    // 初始化
+    static {
+        TableInfoHelper.initTableInfo(User.class);
+    }
+
     @Test
     public void test() {
         /*