miemie 6 years ago
parent
commit
e8fc6844cf

+ 16 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java

@@ -42,16 +42,18 @@ public class StringUtils {
      * 字符串 is
      */
     public static final String IS = "is";
-
     /**
      * 下划线字符
      */
     public static final char UNDERLINE = '_';
-
     /**
      * 占位符
      */
     public static final String PLACE_HOLDER = "{%s}";
+    /**
+     * 验证字符串是否是数据库字段
+     */
+    private static final Pattern P_IS_CLOMUN = Pattern.compile("^\\w\\S*[\\w\\d]*$");
 
     private StringUtils() {
         // to do nothing
@@ -110,6 +112,18 @@ public class StringUtils {
         return !isEmpty(cs);
     }
 
+    /**
+     * <p>
+     * 判断字符串是否符合数据库字段的命名
+     * </p>
+     *
+     * @param str 字符串
+     * @return 判断结果
+     */
+    public static boolean isColumnName(String str) {
+        return P_IS_CLOMUN.matcher(str).matches();
+    }
+
     /**
      * <p>
      * 字符串驼峰转下划线格式

+ 1 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -38,7 +38,6 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import java.lang.reflect.Field;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.regex.Pattern;
 
 import static java.util.stream.Collectors.toList;
 
@@ -61,10 +60,6 @@ public class TableInfoHelper {
      * 默认表主键
      */
     private static final String DEFAULT_ID_NAME = "id";
-    /**
-     * 验证字符串是否是数据库字段
-     */
-    private static final Pattern P = Pattern.compile("^\\w\\S*[\\w\\d]*$");
 
     /**
      * <p>
@@ -428,7 +423,7 @@ public class TableInfoHelper {
      * @return related
      */
     public static boolean checkRelated(boolean underCamel, String property, String column) {
-        if (!P.matcher(column).matches()) {
+        if (!StringUtils.isColumnName(column)) {
             //首尾有转义符
             column = column.substring(1, column.length() - 1);
         }