Browse Source

程序优化

jobob 9 years ago
parent
commit
8706a6acf4

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

@@ -15,21 +15,22 @@
  */
  */
 package com.baomidou.framework.service.impl;
 package com.baomidou.framework.service.impl;
 
 
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+
 import com.baomidou.framework.service.IService;
 import com.baomidou.framework.service.IService;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
 import com.baomidou.mybatisplus.plugins.Page;
-import com.baomidou.mybatisplus.toolkit.StringUtils;
+import com.baomidou.mybatisplus.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.toolkit.TableInfo;
 import com.baomidou.mybatisplus.toolkit.TableInfo;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.Map;
 
 
 /**
 /**
  * <p>
  * <p>
@@ -73,7 +74,7 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 			TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
 			TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
 			if (null != tableInfo) {
 			if (null != tableInfo) {
 				try {
 				try {
-					Method m = cls.getMethod(StringUtils.concatCapitalize("get",tableInfo.getKeyProperty()));
+					Method m = cls.getMethod(ReflectionKit.getMethodCapitalize(tableInfo.getKeyProperty()));
 					Object idVal = m.invoke(entity);
 					Object idVal = m.invoke(entity);
 					if (null != idVal) {
 					if (null != idVal) {
 						return isSelective ? updateSelectiveById(entity) : updateById(entity);
 						return isSelective ? updateSelectiveById(entity) : updateById(entity);

+ 14 - 13
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/EntityWrapper.java

@@ -85,24 +85,25 @@ public class EntityWrapper<T> implements Serializable {
     /**
     /**
      * SQL 片段
      * SQL 片段
      */
      */
-    public String getSqlSegment() {
-        /*
-         * 无条件
+	public String getSqlSegment() {
+		/*
+		 * 无条件
 		 */
 		 */
-        String sqlWhere = sql.toString();
-        if (StringUtils.isEmpty(sqlWhere)) {
-            return null;
-        }
+		String sqlWhere = sql.toString();
+		if (StringUtils.isEmpty(sqlWhere)) {
+			return null;
+		}
 
 
-        // 根据当前实体判断是否需要将WHERE替换成AND 增加实体不为空但所有属性为空的情况
-        sqlWhere = ((null != entity) && ReflectionKit.checkFieldValueNull(entity)) ? sqlWhere.replaceFirst("WHERE", "AND")
-                : sqlWhere;
+		/*
+		 * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
+		 */
+		sqlWhere = ReflectionKit.checkFieldValueNotNull(entity) ? sqlWhere.replaceFirst("WHERE", "AND") : sqlWhere;
 
 
 		/*
 		/*
-         * 使用防SQL注入处理后返回
+		 * 使用防SQL注入处理后返回
 		 */
 		 */
-        return stripSqlInjection(sqlWhere);
-    }
+		return stripSqlInjection(sqlWhere);
+	}
 
 
     /**
     /**
      * <p>
      * <p>

+ 28 - 8
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/ReflectionKit.java

@@ -22,34 +22,54 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
 /**
 /**
- * 反射工具类.
+ * <p>
+ * 反射工具类
+ * </p>
+ *
+ * @author Caratacus
+ * @Date 2016-09-22
  */
  */
 public class ReflectionKit {
 public class ReflectionKit {
 
 
 	private static Logger logger = LoggerFactory.getLogger(ReflectionKit.class);
 	private static Logger logger = LoggerFactory.getLogger(ReflectionKit.class);
 
 
+	/**
+	 * <p>
+	 * 反射 method 方法名,例如 getId
+	 * </p>
+	 *
+	 * @param str
+	 *            属性字符串内容
+	 * @return
+	 */
+	public static String getMethodCapitalize(final String str) {
+		return StringUtils.concatCapitalize("get", str);
+	}
+
 	/**
 	/**
 	 * 调用对象的get方法检查对象所有属性是否为null
 	 * 调用对象的get方法检查对象所有属性是否为null
 	 * 
 	 * 
 	 * @param bean
 	 * @param bean
+	 *            检查对象
 	 * @return boolean true对象所有属性不为null,false对象所有属性为null
 	 * @return boolean true对象所有属性不为null,false对象所有属性为null
 	 */
 	 */
-	public static boolean checkFieldValueNull(Object bean) {
-		boolean result = false;
-		if (bean == null) {
-			return true;
+	public static boolean checkFieldValueNotNull(Object bean) {
+		if (null == bean) {
+			return false;
 		}
 		}
+
 		Class<?> cls = bean.getClass();
 		Class<?> cls = bean.getClass();
 		Method[] methods = cls.getDeclaredMethods();
 		Method[] methods = cls.getDeclaredMethods();
 		TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
 		TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
-
-		if (tableInfo == null) {
+		if (null == tableInfo) {
 			logger.warn("Warn: Could not find @TableId.");
 			logger.warn("Warn: Could not find @TableId.");
 			return false;
 			return false;
 		}
 		}
+
+		boolean result = false;
 		List<TableFieldInfo> fieldList = tableInfo.getFieldList();
 		List<TableFieldInfo> fieldList = tableInfo.getFieldList();
 		for (TableFieldInfo tableFieldInfo : fieldList) {
 		for (TableFieldInfo tableFieldInfo : fieldList) {
-			String fieldGetName = StringUtils.concatCapitalize("get", tableFieldInfo.getProperty());
+			String fieldGetName = getMethodCapitalize(tableFieldInfo.getProperty());
 			if (!checkMethod(methods, fieldGetName)) {
 			if (!checkMethod(methods, fieldGetName)) {
 				continue;
 				continue;
 			}
 			}

+ 13 - 7
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/StringUtils.java

@@ -32,6 +32,7 @@ public class StringUtils {
 	 * 下划线字符
 	 * 下划线字符
 	 */
 	 */
 	public static final char UNDERLINE = '_';
 	public static final char UNDERLINE = '_';
+
 	/**
 	/**
 	 * 空字符串
 	 * 空字符串
 	 */
 	 */
@@ -47,7 +48,7 @@ public class StringUtils {
 	 * @return 判断结果
 	 * @return 判断结果
 	 */
 	 */
 	public static boolean isEmpty(String str) {
 	public static boolean isEmpty(String str) {
-		return str == null || "".equals(str.trim());
+		return str == null || EMPTY_STRING.equals(str.trim());
 	}
 	}
 
 
 	/**
 	/**
@@ -60,7 +61,7 @@ public class StringUtils {
 	 * @return 判断结果
 	 * @return 判断结果
 	 */
 	 */
 	public static boolean isNotEmpty(String str) {
 	public static boolean isNotEmpty(String str) {
-		return (str != null) && !"".equals(str.trim());
+		return (str != null) && !EMPTY_STRING.equals(str.trim());
 	}
 	}
 
 
 	/**
 	/**
@@ -74,7 +75,7 @@ public class StringUtils {
 	 */
 	 */
 	public static String camelToUnderline(String param) {
 	public static String camelToUnderline(String param) {
 		if (isEmpty(param)) {
 		if (isEmpty(param)) {
-			return "";
+			return EMPTY_STRING;
 		}
 		}
 		int len = param.length();
 		int len = param.length();
 		StringBuilder sb = new StringBuilder(len);
 		StringBuilder sb = new StringBuilder(len);
@@ -99,7 +100,7 @@ public class StringUtils {
 	 */
 	 */
 	public static String underlineToCamel(String param) {
 	public static String underlineToCamel(String param) {
 		if (isEmpty(param)) {
 		if (isEmpty(param)) {
-			return "";
+			return EMPTY_STRING;
 		}
 		}
 		int len = param.length();
 		int len = param.length();
 		StringBuilder sb = new StringBuilder(len);
 		StringBuilder sb = new StringBuilder(len);
@@ -164,6 +165,7 @@ public class StringUtils {
 	}
 	}
 
 
 	/**
 	/**
+	 * <p>
 	 * 拼接字符串第二个字符串第一个字母大写
 	 * 拼接字符串第二个字符串第一个字母大写
 	 *
 	 *
 	 * @param concatStr
 	 * @param concatStr
@@ -171,7 +173,7 @@ public class StringUtils {
 	 * @return
 	 * @return
 	 */
 	 */
 	public static String concatCapitalize(String concatStr, final String str) {
 	public static String concatCapitalize(String concatStr, final String str) {
-		if (isEmpty(concatStr)){
+		if (isEmpty(concatStr)) {
 			concatStr = EMPTY_STRING;
 			concatStr = EMPTY_STRING;
 		}
 		}
 		int strLen;
 		int strLen;
@@ -185,16 +187,20 @@ public class StringUtils {
 			return str;
 			return str;
 		}
 		}
 
 
-		return new StringBuilder(strLen).append(concatStr).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
+		return new StringBuilder(strLen).append(concatStr).append(Character.toTitleCase(firstChar))
+				.append(str.substring(1)).toString();
 	}
 	}
+
 	/**
 	/**
+	 * <p>
 	 * 字符串第一个字母大写
 	 * 字符串第一个字母大写
+	 * </p>
 	 *
 	 *
 	 * @param str
 	 * @param str
 	 * @return
 	 * @return
 	 */
 	 */
 	public static String capitalize(final String str) {
 	public static String capitalize(final String str) {
-		return concatCapitalize(null,str);
+		return concatCapitalize(null, str);
 	}
 	}
 
 
 }
 }