Explorar o código

Merge pull request !4 from DevYang/master

青苗 %!s(int64=9) %!d(string=hai) anos
pai
achega
db6bb60cbb

+ 2 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -33,6 +33,7 @@ import java.util.Map;
 
 import com.baomidou.mybatisplus.annotations.IdType;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 /**
  * <p>
@@ -141,7 +142,7 @@ public class AutoGenerator {
 	 * @return
 	 */
 	protected static String getPathFromPackageName(String packageName) {
-		if (null == packageName || "".equals(packageName)) {
+		if (StringUtils.isEmpty(packageName)) {
 			return "";
 		}
 		return packageName.replace(".", File.separator);

+ 5 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/ConfigGenerator.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.generator;
 
 import com.baomidou.mybatisplus.annotations.IdType;
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 
 /**
@@ -141,7 +142,7 @@ public class ConfigGenerator {
 	}
 
 	public String getSuperService() {
-		if (superService == null || "".equals(superService)) {
+		if (StringUtils.isEmpty(superService)) {
 			if (this.getConfigIdType() == ConfigIdType.STRING) {
 				return "com.baomidou.framework.service.ICommonService";
 			} else {
@@ -156,7 +157,7 @@ public class ConfigGenerator {
 	}
 
 	public String getSuperServiceImpl() {
-		if (superServiceImpl == null || "".equals(superServiceImpl)) {
+		if (StringUtils.isEmpty(superServiceImpl)) {
 			if (this.getConfigIdType() == ConfigIdType.STRING) {
 				return "com.baomidou.framework.service.impl.CommonServiceImpl";
 			} else {
@@ -277,7 +278,7 @@ public class ConfigGenerator {
 	}
 
 	public String getXmlPackage() {
-		if (null == xmlPackage || "".equals(xmlPackage)) {
+		if (StringUtils.isEmpty(xmlPackage)) {
 			xmlPackage = mapperPackage + ".xml";
 		}
 		return xmlPackage;
@@ -304,7 +305,7 @@ public class ConfigGenerator {
 	}
 
 	public String getServiceImplPackage() {
-		if (null == serviceImplPackage || "".equals(serviceImplPackage)) {
+		if (StringUtils.isEmpty(serviceImplPackage)) {
 			serviceImplPackage = servicePackage + ".impl";
 		}
 		return serviceImplPackage;

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

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
+import com.baomidou.mybatisplus.toolkit.StringUtils;
+
 /**
  * <p>
  * Entity 对象封装操作类
@@ -68,20 +70,20 @@ public class EntityWrapper<T> {
 	}
 
 	public String getSqlSelect() {
-		if (sqlSelect == null || "".equals(sqlSelect)) {
+		if (StringUtils.isEmpty(sqlSelect)) {
 			return null;
 		}
 		return stripSqlInjection(sqlSelect);
 	}
 
 	public void setSqlSelect(String sqlSelect) {
-		if (sqlSelect != null && !"".equals(sqlSelect)) {
+		if (StringUtils.isNotEmpty(sqlSelect)) {
 			this.sqlSelect = sqlSelect;
 		}
 	}
 
 	public String getSqlSegment() {
-		if (null == sqlSegment || "".equals(sqlSegment)) {
+		if (StringUtils.isEmpty(sqlSegment)) {
 			return null;
 		}
 		StringBuffer andOr = new StringBuffer();
@@ -95,7 +97,7 @@ public class EntityWrapper<T> {
 	}
 
 	public void setSqlSegment(String sqlSegment) {
-		if (sqlSegment != null && !"".equals(sqlSegment)) {
+		if (StringUtils.isNotEmpty(sqlSegment)) {
 			this.sqlSegment = sqlSegment;
 		}
 	}

+ 2 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/Page.java

@@ -19,6 +19,7 @@ import java.util.Collections;
 import java.util.List;
 
 import com.baomidou.mybatisplus.plugins.pagination.Pagination;
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 /**
  * <p>
@@ -79,7 +80,7 @@ public class Page<T> extends Pagination {
 	}
 
 	public void setOrderByField(String orderByField) {
-		if (!"".equals(orderByField)) {
+		if (StringUtils.isNotEmpty(orderByField)) {
 			this.orderByField = orderByField;
 		}
 	}

+ 5 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/PaginationInterceptor.java

@@ -21,6 +21,7 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Properties;
 
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 import org.apache.ibatis.executor.parameter.ParameterHandler;
 import org.apache.ibatis.executor.statement.StatementHandler;
 import org.apache.ibatis.mapping.BoundSql;
@@ -71,10 +72,10 @@ public class PaginationInterceptor implements Interceptor {
 			
 			/* 定义数据库方言 */
 			IDialect dialect = null;
-			if (dialectType != null && !"".equals(dialectType)) {
+			if (StringUtils.isNotEmpty(dialectType)) {
 				dialect = DialectFactory.getDialectByDbtype(dialectType);
 			} else {
-				if (dialectClazz != null && !"".equals(dialectClazz)) {
+				if (StringUtils.isNotEmpty(dialectClazz)) {
 					try {
 						Class<?> clazz = Class.forName(dialectClazz);
 						if (IDialect.class.isAssignableFrom(clazz)) {
@@ -205,10 +206,10 @@ public class PaginationInterceptor implements Interceptor {
 	public void setProperties(Properties prop) {
 		String dialectType = prop.getProperty("dialectType");
 		String dialectClazz = prop.getProperty("dialectClazz");
-		if (dialectType != null && !"".equals(dialectType)) {
+		if (StringUtils.isNotEmpty(dialectType)) {
 			this.dialectType = dialectType;
 		}
-		if (dialectClazz != null && !"".equals(dialectClazz)) {
+		if (StringUtils.isNotEmpty(dialectClazz)) {
 			this.dialectClazz = dialectClazz;
 		}
 	}

+ 98 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/StringUtils.java

@@ -0,0 +1,98 @@
+/**
+ * Copyright (c) 2011-2014, 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;
+
+/**
+ * <p>
+ * String工具类
+ * </p>
+ *
+ * @author D.Yang
+ * @Date 2016-08-18
+ */
+public class StringUtils {
+
+    /**
+     * 判断字符串是否为空
+     *
+     * @param str 需要判断字符串
+     * @return 判断结果
+     */
+    public static boolean isEmpty(String str) {
+        return str == null || "".equals(str.trim());
+    }
+
+    /**
+     * 判断字符串是否不为空
+     *
+     * @param str 需要判断字符串
+     * @return 判断结果
+     */
+    public static boolean isNotEmpty(String str) {
+        return (str != null) && !"".equals(str.trim());
+    }
+
+    private static final char UNDERLINE = '_';
+
+    /**
+     * 字符串驼峰转下划线格式
+     *
+     * @param param 需要转换的字符串
+     * @return 转换好的字符串
+     */
+    public static String camelToUnderline(String param) {
+        if (isEmpty(param)) {
+            return "";
+        }
+        int len = param.length();
+        StringBuilder sb = new StringBuilder(len);
+        for (int i = 0; i < len; i++) {
+            char c = param.charAt(i);
+            if (Character.isUpperCase(c)) {
+                sb.append(UNDERLINE);
+                sb.append(Character.toLowerCase(c));
+            } else {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * 字符串下划线转驼峰格式
+     *
+     * @param param 需要转换的字符串
+     * @return 转换好的字符串
+     */
+    public static String underlineToCamel(String param) {
+        if (isEmpty(param)) {
+            return "";
+        }
+        int len = param.length();
+        StringBuilder sb = new StringBuilder(len);
+        for (int i = 0; i < len; i++) {
+            char c = param.charAt(i);
+            if (c == UNDERLINE) {
+                if (++i < len) {
+                    sb.append(Character.toUpperCase(param.charAt(i)));
+                }
+            } else {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+}

+ 3 - 3
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java

@@ -78,7 +78,7 @@ public class TableInfoHelper {
 			if (tableId != null) {
 				if (tableInfo.getKeyColumn() == null) {
 					tableInfo.setIdType(tableId.type());
-					if(tableId.value() != null && !"".equals(tableId.value())) {
+					if(StringUtils.isNotEmpty(tableId.value())) {
 						/* 自定义字段 */
 						tableInfo.setKeyColumn(tableId.value());
 						tableInfo.setKeyRelated(true);
@@ -98,7 +98,7 @@ public class TableInfoHelper {
 
 			/* 获取注解属性,自定义字段 */
 			TableField tableField = field.getAnnotation(TableField.class);
-			if (tableField != null && tableField.value() != null && !"".equals(tableField.value())) {
+			if (tableField != null && StringUtils.isNotEmpty(tableField.value())) {
 				fieldList.add(new TableFieldInfo(true, tableField.value(), field.getName()));
 				continue;
 			}
@@ -130,7 +130,7 @@ public class TableInfoHelper {
 	 * 驼峰转下划线
 	 */
 	private static String camelToUnderline(String param) {
-		if (param == null || "".equals(param.trim())) {
+		if (StringUtils.isEmpty(param)) {
 			return "";
 		}
 		int len = param.length();