فهرست منبع

fixed conflicts with local

Chris 7 سال پیش
والد
کامیت
5badf9b21b

+ 18 - 24
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java

@@ -15,18 +15,17 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
-import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
-import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
-
 import java.sql.Blob;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
+
 /**
  * <p>
  * String 工具类
@@ -60,16 +59,11 @@ public class StringUtils {
      * 占位符
      */
     public static final String PLACE_HOLDER = "{%s}";
-    private static final Pattern PATTERN_ONE = Pattern.compile(".*[A-Z]+.*");
-    private static final Pattern PATTERN_TWO = Pattern.compile(".*[/_]+.*");
-    private static boolean separatorBeforeDigit = false;
-    private static boolean separatorAfterDigit = true;
-
 
     private StringUtils() {
+        // to do nothing
     }
 
-
     /**
      * <p>
      * Blob 转为 String 格式
@@ -220,7 +214,7 @@ public class StringUtils {
      * @return
      */
     public static boolean isUpperCase(String str) {
-        return match("^[A-Z]+$", str);
+        return matches("^[A-Z]+$", str);
     }
 
     /**
@@ -229,13 +223,14 @@ public class StringUtils {
      * </p>
      *
      * @param regex 正则表达式字符串
-     * @param str   要匹配的字符串
-     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
+     * @param input 要匹配的字符串
+     * @return 如果 input 符合 regex 正则表达式格式, 返回true, 否则返回 false;
      */
-    public static boolean match(String regex, String str) {
-        Pattern pattern = Pattern.compile(regex);
-        Matcher matcher = pattern.matcher(str);
-        return matcher.matches();
+    public static boolean matches(String regex, String input) {
+        if (null == regex || null == input) {
+            return false;
+        }
+        return Pattern.matches(regex, input);
     }
 
     /**
@@ -418,7 +413,7 @@ public class StringUtils {
      * @return
      */
     public static boolean isMixedMode(String word) {
-        return PATTERN_ONE.matcher(word).matches() && PATTERN_TWO.matcher(word).matches();
+        return matches(".*[A-Z]+.*", word) && matches(".*[/_]+.*", word);
     }
 
     /**
@@ -776,17 +771,16 @@ public class StringUtils {
             boolean previousIsWhitespace = Character.isWhitespace(previousChar);
             boolean lastOneIsNotUnderscore = (buf.length() > 0) && (buf.charAt(buf.length() - 1) != '_');
             boolean isNotUnderscore = c != '_';
-            if ((lastOneIsNotUnderscore) && ((isUpperCaseAndPreviousIsLowerCase) || (previousIsWhitespace) || ((betweenUpperCases)
-                && (containsLowerCase) && (isUpperCaseAndPreviousIsUpperCase)))) {
+            if (lastOneIsNotUnderscore && (isUpperCaseAndPreviousIsLowerCase || previousIsWhitespace
+                || (betweenUpperCases && containsLowerCase && isUpperCaseAndPreviousIsUpperCase))) {
                 buf.append("_");
-            } else if (((separatorAfterDigit) && (Character.isDigit(previousChar))
-                && (Character.isLetter(c))) || ((separatorBeforeDigit) && (Character
-                .isDigit(c)) && (Character.isLetter(previousChar)))) {
+            } else if ((Character.isDigit(previousChar) && Character.isLetter(c))
+                || (false && Character.isDigit(c) && Character.isLetter(previousChar))) {
                 buf.append('_');
             }
             if ((shouldReplace(c)) && (lastOneIsNotUnderscore)) {
                 buf.append('_');
-            } else if ((!Character.isWhitespace(c)) && ((isNotUnderscore) || (lastOneIsNotUnderscore))) {
+            } else if (!Character.isWhitespace(c) && (isNotUnderscore || lastOneIsNotUnderscore)) {
                 buf.append(Character.toUpperCase(c));
             }
             previousChar = c;

+ 28 - 24
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/OptimisticLockerInterceptor.java

@@ -1,5 +1,24 @@
 package com.baomidou.mybatisplus.extension.plugins;
 
+import java.lang.reflect.Field;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlCommandType;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.plugin.Intercepts;
+import org.apache.ibatis.plugin.Invocation;
+import org.apache.ibatis.plugin.Plugin;
+import org.apache.ibatis.plugin.Signature;
+
 import com.baomidou.mybatisplus.annotation.Version;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
@@ -7,17 +26,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
 import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
-import org.apache.ibatis.binding.MapperMethod;
-import org.apache.ibatis.executor.Executor;
-import org.apache.ibatis.mapping.MappedStatement;
-import org.apache.ibatis.mapping.SqlCommandType;
-import org.apache.ibatis.plugin.*;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.sql.Timestamp;
-import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * <p>
@@ -76,8 +84,8 @@ public class OptimisticLockerInterceptor implements Interceptor {
         Wrapper ew = null;
         // entity = et
         Object et = null;
-        if (param instanceof MapperMethod.ParamMap) {
-            MapperMethod.ParamMap map = (MapperMethod.ParamMap) param;
+        if (param instanceof Map) {
+            Map map = (Map) param;
             if (map.containsKey(NAME_ENTITY_WRAPPER)) {
                 // mapper.update(updEntity, QueryWrapper<>(whereEntity);
                 ew = (Wrapper) map.get(NAME_ENTITY_WRAPPER);
@@ -92,7 +100,7 @@ public class OptimisticLockerInterceptor implements Interceptor {
             if (map.containsKey(NAME_ENTITY)) {
                 et = map.get(NAME_ENTITY);
             }
-            if (ew != null) { // entityWrapper
+            if (ew != null) { // entityWrapper. baseMapper.update(et,ew);
                 Object entity = ew.getEntity();
                 if (entity != null) {
                     entityClass = ClassUtils.getUserClass(entity.getClass());
@@ -113,6 +121,7 @@ public class OptimisticLockerInterceptor implements Interceptor {
                     // update(entityClass, null) -->> update all. ignore version
                     return invocation.proceed();
                 }
+                //invoke: baseMapper.updateById()
                 entityClass = ClassUtils.getUserClass(et.getClass());
                 EntityField entityField = this.getVersionField(entityClass);
                 versionField = entityField == null ? null : entityField.getField();
@@ -159,16 +168,11 @@ public class OptimisticLockerInterceptor implements Interceptor {
         }
 
         Object resultObj = invocation.proceed();
-        if (Objects.equals(1, resultObj)) {
-            // setVersion, Long.class
-            String _setterMethodName = ReflectionKit.setMethodCapitalize(versionField, versionColumnName);
-            Class<?> _fieldType = versionField.getType();
-            if (ew != null) {
-                Method md = ew.getClass().getMethod(_setterMethodName, _fieldType);
-                Object o = md.invoke(ew, updatedVersionVal);
-            } else if (et != null) {
-                Method md = et.getClass().getMethod(_setterMethodName, _fieldType);
-                Object o = md.invoke(et, updatedVersionVal);
+        if (resultObj instanceof Integer) {
+            Integer effRow = (Integer) resultObj;
+            if (effRow != 0 && et != null && versionField != null && updatedVersionVal != null) {
+                //updated version value set to entity.
+                versionField.set(et, updatedVersionVal);
             }
         }
         return resultObj;

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

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.generator;
 import java.io.Serializable;
 import java.util.List;
 
+import com.baomidou.mybatisplus.extension.activerecord.Model;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -135,8 +136,7 @@ public class AutoGenerator {
             /* ---------- 添加导入包 ---------- */
             if (config.getGlobalConfig().isActiveRecord()) {
                 // 开启 ActiveRecord 模式
-                // todo
-              //  tableInfo.setImportPackages(Model.class.getCanonicalName());
+                tableInfo.setImportPackages(Model.class.getCanonicalName());
             }
             if (tableInfo.isConvert()) {
                 // 表注解

+ 2 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -97,12 +97,12 @@ public class StrategyConfig {
     private String superControllerClass;
 
     /**
-     * 需要包含的表名(与exclude二选一配置)
+     * 需要包含的表名,允许正则表达式(与exclude二选一配置)
      */
     private String[] include = null;
 
     /**
-     * 需要排除的表名
+     * 需要排除的表名,允许正则表达式
      */
     private String[] exclude = null;
     /**

+ 23 - 7
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -460,19 +460,21 @@ public class ConfigBuilder {
                     tableInfo.setName(tableName);
                     tableInfo.setComment(tableComment);
                     if (isInclude) {
-                        for (String includeTab : config.getInclude()) {
-                            if (includeTab.equalsIgnoreCase(tableName)) {
+                        for (String includeTable : config.getInclude()) {
+                            // 忽略大小写等于 或 正则 true
+                            if (tableNameMatches(includeTable, tableName)) {
                                 includeTableList.add(tableInfo);
                             } else {
-                                notExistTables.add(includeTab);
+                                notExistTables.add(includeTable);
                             }
                         }
                     } else if (isExclude) {
-                        for (String excludeTab : config.getExclude()) {
-                            if (excludeTab.equalsIgnoreCase(tableName)) {
+                        for (String excludeTable : config.getExclude()) {
+                            // 忽略大小写等于 或 正则 true
+                            if (tableNameMatches(excludeTable, tableName)) {
                                 excludeTableList.add(tableInfo);
                             } else {
-                                notExistTables.add(excludeTab);
+                                notExistTables.add(excludeTable);
                             }
                         }
                     }
@@ -481,11 +483,11 @@ public class ConfigBuilder {
                     System.err.println("当前数据库为空!!!");
                 }
             }
+
             // 将已经存在的表移除,获取配置中数据库不存在的表
             for (TableInfo tabInfo : tableList) {
                 notExistTables.remove(tabInfo.getName());
             }
-
             if (notExistTables.size() > 0) {
                 System.err.println("表 " + notExistTables + " 在数据库中不存在!!!");
             }
@@ -521,6 +523,20 @@ public class ConfigBuilder {
     }
 
 
+    /**
+     * <p>
+     * 表名匹配
+     * </p>
+     *
+     * @param setTableName 设置表名
+     * @param dbTableName  数据库表单
+     * @return
+     */
+    private boolean tableNameMatches(String setTableName, String dbTableName) {
+        return setTableName.equalsIgnoreCase(dbTableName)
+            || StringUtils.matches(setTableName, dbTableName);
+    }
+
     /**
      * <p>
      * 将字段信息与表信息关联

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/controller.java.ftl

@@ -14,7 +14,7 @@ import ${superControllerClassPackage};
 
 /**
  * <p>
- * ${table.comment} 前端控制器
+ * ${table.comment!} 前端控制器
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/entity.java.ftl

@@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
 
 /**
  * <p>
- * ${table.comment}
+ * ${table.comment!}
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/mapper.java.ftl

@@ -5,7 +5,7 @@ import ${superMapperClassPackage};
 
 /**
  * <p>
- * ${table.comment} Mapper 接口
+ * ${table.comment!} Mapper 接口
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/service.java.ftl

@@ -5,7 +5,7 @@ import ${superServiceClassPackage};
 
 /**
  * <p>
- * ${table.comment} 服务类
+ * ${table.comment!} 服务类
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/serviceImpl.java.ftl

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Service;
 
 /**
  * <p>
- * ${table.comment} 服务实现类
+ * ${table.comment!} 服务实现类
  * </p>
  *
  * @author ${author}

+ 2 - 2
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/test/generator/PostgreSQLGenerator.java

@@ -81,8 +81,8 @@ public class PostgreSQLGenerator extends GeneratorTest {
         strategy.setFieldPrefix(new String[]{"A_"});
         strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
         strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 允许字段策略独立设置,默认为 naming 策略
-        // strategy.setInclude(new String[] { "user" }); // 需要生成的表
-        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
+        strategy.setInclude("sys_user", "^mp.*", "ok"); // 需要生成的表,支持正则表达式
+        // strategy.setExclude("test"); // 排除生成的表,支持正则表达式
         // 自定义实体父类
         // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
         // 自定义实体,公共字段

+ 2 - 2
mybatis-plus-generator/src/test/resources/templates/dto.java.ftl

@@ -3,8 +3,8 @@ package ${package.Entity};
 
 /**
  * <p>
-    * 测试自定义DTO模板 ${table.comment}
-    * </p>
+ * 测试自定义DTO模板 ${table.comment!}
+ * </p>
  *
  * 测试注入 ${cfg.abc}
  */

+ 46 - 6
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -4,6 +4,13 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.test.h2.config.H2Db;
 import com.baomidou.mybatisplus.test.h2.entity.persistent.H2User;
 import com.baomidou.mybatisplus.test.h2.service.IH2UserService;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -12,12 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * <p>
  * Mybatis Plus H2 Junit Test
@@ -116,4 +117,43 @@ public class H2UserTest extends BaseTest {
         Assert.assertNotEquals(0, count);
     }
 
+    @Test
+    public void testUpdateByIdWitiOptLock(){
+        Long id = 991L;
+        H2User user = new H2User();
+        user.setTestId(id);
+        user.setName("991");
+        user.setAge(91);
+        user.setPrice(BigDecimal.TEN);
+        user.setDesc("asdf");
+        user.setTestType(1);
+        user.setVersion(1);
+        userService.save(user);
+
+        H2User userDB = userService.getById(id);
+        Assert.assertEquals(1, userDB.getVersion().intValue());
+
+        userDB.setName("992");
+        userService.updateById(userDB);
+        Assert.assertEquals("updated version value should be updated to entity",2, userDB.getVersion().intValue());
+
+        userDB = userService.getById(id);
+        Assert.assertEquals(2, userDB.getVersion().intValue());
+        Assert.assertEquals("992", userDB.getName());
+    }
+
+    @Test
+    public void testUpdateByEwWithOptLock(){
+        QueryWrapper<H2User> ew = new QueryWrapper<>();
+        ew.gt("age",13);
+        for(H2User u: userService.list(ew)){
+            System.out.println(u.getName()+","+u.getAge()+","+u.getVersion());
+        }
+        userService.update(new H2User().setPrice(BigDecimal.TEN), ew);
+        for(H2User u: userService.list(ew)){
+            System.out.println(u.getName()+","+u.getAge()+","+u.getVersion());
+        }
+    }
+
+
 }