diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java index 38d456b1c..e3f0c3bae 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java @@ -164,12 +164,12 @@ public void removeMapper(Class type) { // 清空 Mapper 方法 mappedStatement 缓存信息 String typeKey = type.getName() + StringPool.DOT; String simpleName = type.getSimpleName(); - mappedStatements.keySet().stream().filter(ms -> ms.startsWith(typeKey) || ms.equals(simpleName)).collect(Collectors.toSet()).forEach(mappedStatements::remove); - resultMaps.keySet().stream().filter(r -> r.startsWith(typeKey)).collect(Collectors.toSet()).forEach(resultMaps::remove); - parameterMaps.keySet().stream().filter(p -> p.startsWith(typeKey)).collect(Collectors.toSet()).forEach(parameterMaps::remove); - keyGenerators.keySet().stream().filter(k -> k.startsWith(typeKey)).collect(Collectors.toSet()).forEach(keyGenerators::remove); - sqlFragments.keySet().stream().filter(s -> s.startsWith(typeKey)).collect(Collectors.toSet()).forEach(sqlFragments::remove); - caches.keySet().stream().filter(p -> p.equals(type.getName()) || p.equals(simpleName)).collect(Collectors.toSet()).forEach(caches::remove); + mappedStatements.keySet().stream().filter(ms -> ms.startsWith(typeKey) || ms.equals(simpleName)).forEach(mappedStatements::remove); + resultMaps.keySet().stream().filter(r -> r.startsWith(typeKey)).forEach(resultMaps::remove); + parameterMaps.keySet().stream().filter(p -> p.startsWith(typeKey)).forEach(parameterMaps::remove); + keyGenerators.keySet().stream().filter(k -> k.startsWith(typeKey)).forEach(keyGenerators::remove); + sqlFragments.keySet().stream().filter(s -> s.startsWith(typeKey)).forEach(sqlFragments::remove); + caches.keySet().stream().filter(p -> p.equals(type.getName()) || p.equals(simpleName)).forEach(caches::remove); } } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperRegistry.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperRegistry.java index b2c60884e..1b0d95fdc 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperRegistry.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperRegistry.java @@ -49,9 +49,10 @@ public T getMapper(Class type, SqlSession sqlSession) { // fix https://github.com/baomidou/mybatis-plus/issues/4247 MybatisMapperProxyFactory mapperProxyFactory = (MybatisMapperProxyFactory) knownMappers.get(type); if (mapperProxyFactory == null) { - mapperProxyFactory = (MybatisMapperProxyFactory) knownMappers.entrySet().stream() - .filter(t -> t.getKey().getName().equals(type.getName())).findFirst().map(Map.Entry::getValue) - .orElseThrow(() -> new BindingException("Type " + type + " is not known to the MybatisPlusMapperRegistry.")); + mapperProxyFactory = findByName(type); + if (mapperProxyFactory == null) { + throw new BindingException("Type " + type + " is not known to the MybatisPlusMapperRegistry."); + } } try { return mapperProxyFactory.newInstance(sqlSession); @@ -60,6 +61,15 @@ public T getMapper(Class type, SqlSession sqlSession) { } } + private MybatisMapperProxyFactory findByName(Class type) { + for (Map.Entry, MybatisMapperProxyFactory> entry : knownMappers.entrySet()) { + if (entry.getKey().getName().equals(type.getName())) { + return (MybatisMapperProxyFactory) entry.getValue(); + } + } + return null; + } + @Override public boolean hasMapper(Class type) { return knownMappers.containsKey(type); @@ -69,8 +79,9 @@ public boolean hasMapper(Class type) { * 清空 Mapper 缓存信息 */ protected void removeMapper(Class type) { - knownMappers.entrySet().stream().filter(t -> t.getKey().getName().equals(type.getName())) - .findFirst().ifPresent(t -> knownMappers.remove(t.getKey())); + if (findByName(type) != null) { + knownMappers.remove(type); + } } @Override diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java index 9dc171094..1e78f54cb 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisParameterHandler.java @@ -205,12 +205,12 @@ private Collection extractParameters(Object parameterObject) { Collection parameters = new ArrayList<>(); Map parameterMap = (Map) parameterObject; Set objectSet = new HashSet<>(); - parameterMap.forEach((k, v) -> { + for (Object v : parameterMap.values()) { if (objectSet.add(v)) { Collection collection = toCollection(v); parameters.addAll(collection); } - }); + } return parameters; } else { return Collections.singleton(parameterObject); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java index 31316b098..be3370f5d 100755 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java @@ -110,15 +110,13 @@ public Children setEntityClass(Class entityClass) { @Override public Children allEq(boolean condition, Map params, boolean null2IsNull) { if (condition && CollectionUtils.isNotEmpty(params)) { - params.forEach((k, v) -> { - if (StringUtils.checkValNotNull(v)) { - eq(k, v); - } else { - if (null2IsNull) { - isNull(k); - } + for (Map.Entry entry : params.entrySet()) { + if (StringUtils.checkValNotNull(entry.getValue())) { + eq(entry.getKey(), entry.getValue()); + } else if (null2IsNull) { + isNull(entry.getKey()); } - }); + } } return typedThis; } @@ -126,17 +124,17 @@ public Children allEq(boolean condition, Map params, boolean null2IsNu @Override public Children allEq(boolean condition, BiPredicate filter, Map params, boolean null2IsNull) { if (condition && CollectionUtils.isNotEmpty(params)) { - params.forEach((k, v) -> { + for (Map.Entry entry : params.entrySet()) { + R k = entry.getKey(); + V v = entry.getValue(); if (filter.test(k, v)) { if (StringUtils.checkValNotNull(v)) { eq(k, v); - } else { - if (null2IsNull) { - isNull(k); - } + } else if (null2IsNull) { + isNull(k); } } - }); + } } return typedThis; } @@ -374,7 +372,6 @@ public Children doGroupBy(boolean condition, R column, List columns) { }); } - public Children doOrderBy(boolean condition, boolean isAsc, R column, List columns) { return maybeDo(condition, () -> { final SqlKeyword mode = isAsc ? ASC : DESC; @@ -382,8 +379,9 @@ public Children doOrderBy(boolean condition, boolean isAsc, R column, List co appendSqlSegments(ORDER_BY, columnToSqlSegment(column), mode); } if (CollectionUtils.isNotEmpty(columns)) { - columns.forEach(c -> appendSqlSegments(ORDER_BY, - columnToSqlSegment(c), mode)); + for (R c : columns) { + appendSqlSegments(ORDER_BY, columnToSqlSegment(c), mode); + } } }); } @@ -416,8 +414,11 @@ public Children orderBy(boolean condition, boolean isAsc, R column) { @Override public Children orderBy(boolean condition, boolean isAsc, List columns) { - return maybeDo(condition, () -> columns.forEach(c -> appendSqlSegments(ORDER_BY, - columnToSqlSegment(c), isAsc ? ASC : DESC))); + return maybeDo(condition, () -> { + for (R c : columns) { + appendSqlSegments(ORDER_BY, columnToSqlSegment(c), isAsc ? ASC : DESC); + } + }); } @Override @@ -578,7 +579,7 @@ protected ISqlSegment inExpression(Object[] values) { */ protected void initNeed() { paramNameSeq = new AtomicInteger(0); - paramNameValuePairs = new HashMap<>(16); + paramNameValuePairs = new HashMap<>(); expression = new MergeSegments(); lastSql = SharedString.emptyString(); sqlComment = SharedString.emptyString(); @@ -707,5 +708,7 @@ public Children clone() { public interface DoSomething { void doIt(); + } + } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java index dc59a5d29..34fd46261 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java @@ -15,6 +15,9 @@ */ package com.baomidou.mybatisplus.core.enums; +import java.util.ArrayList; +import java.util.List; + /** * MybatisPlus 支持 SQL 方法 * @@ -101,6 +104,8 @@ public enum SqlMethod { private final String method; private final String desc; private final String sql; + /** [ null, [s1From, s1To], null, null, [s2From, s2To] ] */ + private transient int[][] compiledPosPairs; SqlMethod(String method, String desc, String sql) { this.method = method; @@ -119,4 +124,43 @@ public String getDesc() { public String getSql() { return sql; } + + private static int[][] compile(final String sqlTemplate) { + final List list = new ArrayList<>(6); + final String placeholder = "%s"; + int begin = 0, pos = sqlTemplate.indexOf(placeholder, begin); + while (pos != -1) { + if (begin < pos) { + list.add(new int[] { begin, pos }); + } + list.add(null); // null is placeholder + begin = pos + placeholder.length(); + pos = sqlTemplate.indexOf(placeholder, begin); + } + final int length = sqlTemplate.length(); + if (begin < length) { + list.add(new int[] { begin, length }); + } + return list.toArray(new int[list.size()][]); + } + + public String format(Object... args) { + // return String.format(getSql(), args); + int[][] pairs = compiledPosPairs; + final String sql = getSql(); + if (pairs == null) { + compiledPosPairs = pairs = compile(sql); + } + final StringBuilder sb = new StringBuilder(sql.length() + 64); + int i = 0; + for (final int[] pair : pairs) { + if (pair == null) { + sb.append(args[i++]); + } else { + sb.append(sql, pair[0], pair[1]); + } + } + return sb.toString(); + } + } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java index c5c449c15..e2fb2dbb8 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java @@ -15,8 +15,7 @@ */ package com.baomidou.mybatisplus.core.handlers; -import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.baomidou.mybatisplus.core.metadata.*; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.reflection.MetaObject; @@ -194,14 +193,17 @@ default MetaObjectHandler strictUpdateFill(TableInfo tableInfo, MetaObject metaO */ default MetaObjectHandler strictFill(boolean insertFill, TableInfo tableInfo, MetaObject metaObject, List> strictFills) { if ((insertFill && tableInfo.isWithInsertFill()) || (!insertFill && tableInfo.isWithUpdateFill())) { - strictFills.forEach(i -> { + for (StrictFill i : strictFills) { final String fieldName = i.getFieldName(); final Class fieldType = i.getFieldType(); - tableInfo.getFieldList().stream() - .filter(j -> j.getProperty().equals(fieldName) && fieldType.equals(j.getPropertyType()) && - ((insertFill && j.isWithInsertFill()) || (!insertFill && j.isWithUpdateFill()))).findFirst() - .ifPresent(j -> strictFillStrategy(metaObject, fieldName, i.getFieldVal())); - }); + for (TableFieldInfo j : tableInfo.getFieldList()) { + if (j.getProperty().equals(fieldName) && fieldType.equals(j.getPropertyType()) && + ((insertFill && j.isWithInsertFill()) || (!insertFill && j.isWithUpdateFill()))) { + strictFillStrategy(metaObject, fieldName, i.getFieldVal()); + break; + } + } + } } return this; } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractSqlInjector.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractSqlInjector.java index 678856373..98af47be7 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractSqlInjector.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractSqlInjector.java @@ -55,7 +55,9 @@ public void inspectInject(MapperBuilderAssistant builderAssistant, Class mapp } if (CollectionUtils.isNotEmpty(methodList)) { // 循环注入自定义方法 - methodList.forEach(m -> m.inject(builderAssistant, mapperClass, modelClass, tableInfo)); + for (AbstractMethod m : methodList) { + m.inject(builderAssistant, mapperClass, modelClass, tableInfo); + } } else { logger.debug(className + ", No effective injection method was found."); } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Delete.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Delete.java index 9114d4749..716a8a566 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Delete.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Delete.java @@ -46,14 +46,14 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode String sql; SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE; if (tableInfo.isWithLogicDelete()) { - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), + sql = sqlMethod.format(tableInfo.getTableName(), sqlLogicSet(tableInfo), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource); } else { sqlMethod = SqlMethod.DELETE; - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), + sql = sqlMethod.format(tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteById.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteById.java index e4426d6d8..ee49cc523 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteById.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteById.java @@ -63,10 +63,10 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode String sqlSet = "SET " + SqlScriptUtils.convertIf(fieldInfos.stream() .map(i -> i.getSqlSet(EMPTY)).collect(joining(EMPTY)), "!@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(_parameter.getClass())", true) + tableInfo.getLogicDeleteSql(false, false); - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), + sql = sqlMethod.format(tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true)); } else { - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), + sql = sqlMethod.format(tableInfo.getTableName(), sqlLogicSet(tableInfo), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true)); } @@ -74,7 +74,7 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource); } else { sqlMethod = SqlMethod.DELETE_BY_ID; - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), + sql = sqlMethod.format(tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty()); return this.addDeleteMappedStatement(mapperClass, methodName, super.createSqlSource(configuration, sql, Object.class)); } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByIds.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByIds.java index d16c01028..6e9217611 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByIds.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByIds.java @@ -62,7 +62,7 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource); } else { sqlMethod = SqlMethod.DELETE_BY_IDS; - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), + sql = sqlMethod.format(tableInfo.getTableName(), tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach( SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())", "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"), @@ -88,7 +88,7 @@ public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) { .map(i -> i.getSqlSet(Constants.MP_FILL_ET + StringPool.DOT)).collect(joining(EMPTY)), String.format("%s != null", Constants.MP_FILL_ET), true); } sqlSet += StringPool.EMPTY + tableInfo.getLogicDeleteSql(false, false); - return String.format(sqlMethod.getSql(), tableInfo.getTableName(), + return sqlMethod.format(tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach( SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())", "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"), diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByMap.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByMap.java index 4a1a61ce2..7146e90a8 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByMap.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteByMap.java @@ -49,12 +49,12 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode String sql; SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_MAP; if (tableInfo.isWithLogicDelete()) { - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), sqlWhereByMap(tableInfo)); + sql = sqlMethod.format(tableInfo.getTableName(), sqlLogicSet(tableInfo), sqlWhereByMap(tableInfo)); SqlSource sqlSource = super.createSqlSource(configuration, sql, Map.class); return addUpdateMappedStatement(mapperClass, Map.class, methodName, sqlSource); } else { sqlMethod = SqlMethod.DELETE_BY_MAP; - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlWhereByMap(tableInfo)); + sql = sqlMethod.format(tableInfo.getTableName(), this.sqlWhereByMap(tableInfo)); SqlSource sqlSource = super.createSqlSource(configuration, sql, Map.class); return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource); } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java index 9985f0f58..cd7ed0bf8 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java @@ -100,7 +100,7 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode keyColumn = tableInfo.getKeyColumn(); } } - String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript); + String sql = sqlMethod.format(tableInfo.getTableName(), columnScript, valuesScript); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addInsertMappedStatement(mapperClass, modelClass, methodName, sqlSource, keyGenerator, keyProperty, keyColumn); } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java index bdd9b4f8b..a72b3daaa 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectById.java @@ -44,7 +44,7 @@ public SelectById(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_BY_ID; - SqlSource sqlSource = super.createSqlSource(configuration, String.format(sqlMethod.getSql(), + SqlSource sqlSource = super.createSqlSource(configuration, sqlMethod.format( sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true)), Object.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByIds.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByIds.java index b65b56b58..5ba2b3a5c 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByIds.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByIds.java @@ -45,7 +45,7 @@ public SelectByIds(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_BY_IDS; - SqlSource sqlSource = super.createSqlSource(configuration, String.format(sqlMethod.getSql(), + SqlSource sqlSource = super.createSqlSource(configuration, sqlMethod.format( sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach("#{item}", COLL, null, "item", COMMA), tableInfo.getLogicDeleteSql(true, true)), Object.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByMap.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByMap.java index 15bbae216..8d73b42d9 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByMap.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectByMap.java @@ -47,7 +47,7 @@ public SelectByMap(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_BY_MAP; - String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false), + String sql = sqlMethod.format(sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlWhereByMap(tableInfo)); SqlSource sqlSource = super.createSqlSource(configuration, sql, Map.class); return this.addSelectMappedStatementForTable(mapperClass, methodName, sqlSource, tableInfo); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java index 11b228c27..231f85151 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectCount.java @@ -44,7 +44,7 @@ public SelectCount(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_COUNT; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(), tableInfo.getTableName(), + String sql = sqlMethod.format(sqlFirst(), sqlCount(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, methodName, sqlSource, Long.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java index acadcec03..265932b1c 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java @@ -44,7 +44,7 @@ public SelectList(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_LIST; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), + String sql = sqlMethod.format(sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForTable(mapperClass, methodName, sqlSource, tableInfo); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMaps.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMaps.java index c3810ed85..9920f44de 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMaps.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMaps.java @@ -46,7 +46,7 @@ public SelectMaps(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_MAPS; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), + String sql = sqlMethod.format(sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),sqlOrderBy(tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, methodName, sqlSource, Map.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java index 745918d30..2c990c743 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java @@ -48,7 +48,7 @@ public SelectMapsPage(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_MAPS_PAGE; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), + String sql = sqlMethod.format(sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, methodName, sqlSource, Map.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java index 65bdf5360..e272b0c4b 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectObjs.java @@ -44,7 +44,7 @@ public SelectObjs(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_OBJS; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectObjsColumns(tableInfo), + String sql = sqlMethod.format(sqlFirst(), sqlSelectObjsColumns(tableInfo), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),sqlOrderBy(tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, methodName, sqlSource, Object.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java index db2a2dda6..fc8c87fcd 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java @@ -49,7 +49,7 @@ public SelectPage(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.SELECT_PAGE; - String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), + String sql = sqlMethod.format(sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForTable(mapperClass, methodName, sqlSource, tableInfo); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Update.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Update.java index 1febb3bf8..d7fd32c70 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Update.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Update.java @@ -44,7 +44,7 @@ public Update(String name) { @Override public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.UPDATE; - String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), + String sql = sqlMethod.format(tableInfo.getTableName(), sqlSet(true, true, tableInfo, true, ENTITY, ENTITY_DOT), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateById.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateById.java index 2be0ff4a7..49d73d1cf 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateById.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateById.java @@ -45,7 +45,7 @@ public UpdateById(String name) { public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID; final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true); - String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), + String sql = sqlMethod.format(tableInfo.getTableName(), sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, ENTITY, ENTITY_DOT), tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/OrderItem.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/OrderItem.java index 2359c9ec2..78c483b9e 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/OrderItem.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/OrderItem.java @@ -15,6 +15,7 @@ */ package com.baomidou.mybatisplus.core.metadata; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import lombok.Getter; import lombok.Setter; @@ -33,6 +34,7 @@ @Getter @Setter public class OrderItem implements Serializable { + private static final long serialVersionUID = 1L; /** @@ -53,11 +55,11 @@ public static OrderItem desc(String column) { } public static List ascs(String... columns) { - return Arrays.stream(columns).map(OrderItem::asc).collect(Collectors.toList()); + return CollectionUtils.toList(Arrays.asList(columns), OrderItem::asc); } public static List descs(String... columns) { - return Arrays.stream(columns).map(OrderItem::desc).collect(Collectors.toList()); + return CollectionUtils.toList(Arrays.asList(columns), OrderItem::desc); } private static OrderItem build(String column, boolean asc) { diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java index 123e86498..ca174b08b 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java @@ -498,7 +498,9 @@ void initResultMapIfNeed() { resultMappings.add(idMapping); } if (CollectionUtils.isNotEmpty(fieldList)) { - fieldList.forEach(i -> resultMappings.add(i.getResultMapping(configuration))); + for (TableFieldInfo i : fieldList) { + resultMappings.add(i.getResultMapping(configuration)); + } } ResultMap resultMap = new ResultMap.Builder(configuration, id, entityType, resultMappings).build(); configuration.addResultMap(resultMap); @@ -510,7 +512,7 @@ void setFieldList(List fieldList) { this.fieldList = fieldList; AtomicInteger logicDeleted = new AtomicInteger(); AtomicInteger version = new AtomicInteger(); - fieldList.forEach(i -> { + for (TableFieldInfo i : fieldList) { if (i.isLogicDelete()) { this.withLogicDelete = true; this.logicDeleteFieldInfo = i; @@ -527,7 +529,7 @@ void setFieldList(List fieldList) { this.versionFieldInfo = i; version.getAndAdd(1); } - }); + } /* 校验字段合法性 */ Assert.isTrue(logicDeleted.get() <= 1, "@TableLogic not support more than one in Class: \"%s\"", entityType.getName()); Assert.isTrue(version.get() <= 1, "@Version not support more than one in Class: \"%s\"", entityType.getName()); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelper.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelper.java index 6472dbbe1..8ad8b6eef 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelper.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelper.java @@ -232,7 +232,9 @@ private static Map chooseOthers(Map mapper, Ma keys.addAll(methodKeys); keys.addAll(mapperKeys); Map map = CollectionUtils.newHashMapWithExpectedSize(keys.size()); - methodKeys.forEach(k -> map.put(k, chooseBoolean(mapper.get(k), method.get(k)))); + for (String k : methodKeys) { + map.put(k, chooseBoolean(mapper.get(k), method.get(k))); + } return map; } } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/CollectionUtils.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/CollectionUtils.java index 9a351138a..d1d8dad42 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/CollectionUtils.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/CollectionUtils.java @@ -200,7 +200,12 @@ private static int capacity(int expectedSize) { public static List getCollection(Map map, Iterable keys) { List result = new ArrayList<>(); if (map != null && !map.isEmpty() && keys != null) { - keys.forEach(key -> Optional.ofNullable(map.get(key)).ifPresent(result::add)); + for (K key : keys) { + V v = map.get(key); + if (v != null) { + result.add(v); + } + } } return result; } @@ -218,7 +223,7 @@ public static List getCollection(Map map, Iterable keys) { public static List getCollection(Map map, Iterable keys, Comparator comparator) { Objects.requireNonNull(comparator); List result = getCollection(map, keys); - Collections.sort(result, comparator); + result.sort(comparator); return result; } @@ -235,27 +240,52 @@ public static List toList(T... t) { return Collections.emptyList(); } + public static List toList(Collection c, Function converter) { + if (c == null) { + return null; + } + final List list = new ArrayList<>(c.size()); + for (E t : c) { + R val = converter.apply(t); + list.add(val); + } + return list; + } + /** * 切割集合为多个集合 - * @param entityList 数据集合 + * + * @param c 数据集合 * @param batchSize 每批集合的大小 - * @return 切割后的多个集合 * @param 数据类型 + * @return 切割后的多个集合 */ - public static List> split(Collection entityList, int batchSize) { - if (isEmpty(entityList)) { + public static List> split(Collection c, int batchSize) { + if (isEmpty(c)) { return Collections.emptyList(); } Assert.isFalse(batchSize < 1, "batchSize must not be less than one"); - final Iterator iterator = entityList.iterator(); - final List> results = new ArrayList<>(entityList.size() / batchSize); - while (iterator.hasNext()) { - final List list = IntStream.range(0, batchSize).filter(x -> iterator.hasNext()) - .mapToObj(i -> iterator.next()).collect(Collectors.toList()); - if (!list.isEmpty()) { - results.add(list); + final int total = c.size(); + final List> results = new ArrayList<>(total / batchSize + 1); + if (c instanceof List) { + final List list = (List) c; + for (int i = 0; i < total; ) { + int end = Math.min(total, i + batchSize); + results.add(list.subList(i, end)); + i = end; + } + } else { + List subList = new ArrayList<>(Math.min(total, batchSize)); + results.add(subList); + int count = 0; + for (T t : c) { + subList.add(t); + if (++count % batchSize == 0 && count < total) { + results.add(subList = new ArrayList<>(Math.min(total - count, batchSize))); + } } } return results; } + } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java index f82f011b0..6b3ae9103 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java @@ -15,8 +15,7 @@ */ package com.baomidou.mybatisplus.core.toolkit; -import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.baomidou.mybatisplus.core.metadata.*; import com.baomidou.mybatisplus.core.toolkit.support.*; import java.lang.invoke.SerializedLambda; @@ -102,10 +101,9 @@ private static Map createColumnCacheMap(TableInfo info) { } else { map = CollectionUtils.newHashMapWithExpectedSize(info.getFieldList().size()); } - - info.getFieldList().forEach(i -> - map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(), i.getMapping())) - ); + for (TableFieldInfo i : info.getFieldList()) { + map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(), i.getMapping())); + } return map; } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/PluginUtils.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/PluginUtils.java index c9723a089..1aa3b0b8c 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/PluginUtils.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/PluginUtils.java @@ -81,7 +81,9 @@ public static MetaObject getMetaObject(Object object) { * @param additionalParameters additionalParameters */ public static void setAdditionalParameter(BoundSql boundSql, Map additionalParameters) { - additionalParameters.forEach(boundSql::setAdditionalParameter); + for (Map.Entry entry : additionalParameters.entrySet()) { + boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); + } } public static MPBoundSql mpBoundSql(BoundSql boundSql) { diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java index ec09d23ba..50bc6393c 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java @@ -168,8 +168,11 @@ public static Map excludeOverrideSuperField(Field[] fields, List< throw new IllegalStateException(String.format("Duplicate key %s", u)); }, LinkedHashMap::new)); - superFieldList.stream().filter(field -> !fieldMap.containsKey(field.getName())) - .forEach(f -> fieldMap.put(f.getName(), f)); + for (Field field : superFieldList) { + if (!fieldMap.containsKey(field.getName())) { + fieldMap.put(field.getName(), field); + } + } return fieldMap; } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java index d5f44e7ae..ebe9149c1 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java @@ -200,7 +200,7 @@ public static String firstToLowerCase(String param) { if (isBlank(param)) { return StringPool.EMPTY; } - return param.substring(0, 1).toLowerCase() + param.substring(1); + return new StringBuilder(param.length()).append(Character.toLowerCase(param.charAt(0))).append(param, 1, param.length()).toString(); } /** @@ -593,7 +593,7 @@ public static boolean equals(final CharSequence cs1, final CharSequence cs2) { */ public static String sqlInjectionReplaceBlank(String str) { if (SqlInjectionUtils.check(str)) { - /** + /* * 过滤sql黑名单字符,存在 SQL 注入,去除空白内容 */ str = replaceAllBlank(str); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlInjectionUtils.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlInjectionUtils.java index bfe02fd64..d18ee0524 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlInjectionUtils.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlInjectionUtils.java @@ -25,6 +25,7 @@ * @since 2021-08-15 */ public class SqlInjectionUtils { + /** * SQL语法检查正则:符合两个关键字(有先后顺序)才算匹配 */ @@ -36,7 +37,6 @@ public class SqlInjectionUtils { */ private static final Pattern SQL_COMMENT_PATTERN = Pattern.compile("'.*(or|union|--|#|/\\*|;)", Pattern.CASE_INSENSITIVE); - /** * 检查参数是否存在 SQL 注入 * @@ -55,7 +55,7 @@ public static boolean check(String value) { * @param text 待处理字段 */ public static String removeEscapeCharacter(String text) { - Objects.nonNull(text); - return text.replaceAll("\"", "").replaceAll("'", ""); + return text.replace("\"", "").replace("'", ""); } + } diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/AlwaysUpdateSomeColumnById.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/AlwaysUpdateSomeColumnById.java index 211191341..1e608c0c8 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/AlwaysUpdateSomeColumnById.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/AlwaysUpdateSomeColumnById.java @@ -80,7 +80,7 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode String sqlSet = this.filterTableFieldInfo(tableInfo.getFieldList(), getPredicate(), i -> i.getSqlSet(true, ENTITY_DOT), NEWLINE); sqlSet = SqlScriptUtils.convertSet(sqlSet); - String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, + String sql = sqlMethod.format(tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource); diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/InsertBatchSomeColumn.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/InsertBatchSomeColumn.java index c774b2df5..bc51cb441 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/InsertBatchSomeColumn.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/InsertBatchSomeColumn.java @@ -126,7 +126,7 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode } } } - String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript); + String sql = sqlMethod.format(tableInfo.getTableName(), columnScript, valuesScript); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addInsertMappedStatement(mapperClass, modelClass, methodName, sqlSource, keyGenerator, keyProperty, keyColumn); } diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteBatchByIds.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteBatchByIds.java index fa4c56cd1..c37963fb7 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteBatchByIds.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteBatchByIds.java @@ -70,7 +70,7 @@ public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) { .map(i -> i.getSqlSet(COLL + "[0].")).collect(joining(EMPTY)); String sqlSet = "SET " + SqlScriptUtils.convertIf(sqlScript, "!@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(_parameter.getClass())", true) + tableInfo.getLogicDeleteSql(false, false); - return String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), + return sqlMethod.format(tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach( SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())", "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"), diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteByIdWithFill.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteByIdWithFill.java index 7ab41b5ae..57c54aebe 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteByIdWithFill.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteByIdWithFill.java @@ -69,16 +69,16 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode if (CollectionUtils.isNotEmpty(fieldInfos)) { String sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(EMPTY)).collect(joining(EMPTY)) + tableInfo.getLogicDeleteSql(false, false); - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), + sql = sqlMethod.format(tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true)); } else { - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), + sql = sqlMethod.format(tableInfo.getTableName(), sqlLogicSet(tableInfo), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true)); } } else { sqlMethod = SqlMethod.DELETE_BY_ID; - sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), + sql = sqlMethod.format(tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty()); } SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/Upsert.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/Upsert.java index 749d06431..f3f0494c2 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/Upsert.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/Upsert.java @@ -64,7 +64,7 @@ public MappedStatement injectMappedStatement(Class mapperClass, Class mode keyColumn = tableInfo.getKeyColumn(); } } - String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), columnScript, valuesScript); + String sql = sqlMethod.format(tableInfo.getTableName(), columnScript, valuesScript); SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass); return this.addInsertMappedStatement(mapperClass, modelClass, methodName, sqlSource, keyGenerator, keyProperty, keyColumn); } diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/MybatisPlusInterceptor.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/MybatisPlusInterceptor.java index f2b4dd6fe..b0ae5fec9 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/MybatisPlusInterceptor.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/MybatisPlusInterceptor.java @@ -138,11 +138,11 @@ public List getInterceptors() { public void setProperties(Properties properties) { PropertyMapper pm = PropertyMapper.newInstance(properties); Map group = pm.group(StringPool.AT); - group.forEach((k, v) -> { - InnerInterceptor innerInterceptor = ClassUtils.newInstance(k); - innerInterceptor.setProperties(v); + for (Map.Entry entry : group.entrySet()) { + InnerInterceptor innerInterceptor = ClassUtils.newInstance(entry.getKey()); + innerInterceptor.setProperties(entry.getValue()); addInnerInterceptor(innerInterceptor); - }); + } } @Override diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Page.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Page.java index 6f1acdac1..9e527ad1f 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Page.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Page.java @@ -194,11 +194,11 @@ public Long maxLimit() { */ private String[] mapOrderToArray(Predicate filter) { List columns = new ArrayList<>(orders.size()); - orders.forEach(i -> { + for (OrderItem i : orders) { if (filter.test(i)) { columns.add(i.getColumn()); } - }); + } return columns.toArray(new String[0]); } diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/PropertyMapper.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/PropertyMapper.java index b47609d7e..576ee3b81 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/PropertyMapper.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/PropertyMapper.java @@ -73,13 +73,17 @@ public Map group(String group) { return Collections.emptyMap(); } Map map = CollectionUtils.newHashMap(); - inner.forEach(i -> { - Properties p = new Properties(); + for (String i : inner) { + Properties p = new Properties(); String key = i.substring(group.length()) + StringPool.COLON; int keyIndex = key.length(); - keys.stream().filter(j -> j.startsWith(key)).forEach(j -> p.setProperty(j.substring(keyIndex), delegate.getProperty(j))); + for (String j : keys) { + if (j.startsWith(key)) { + p.setProperty(j.substring(keyIndex), delegate.getProperty(j)); + } + } map.put(delegate.getProperty(i), p); - }); + } return map; } } diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/PathInfoHandler.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/PathInfoHandler.java index 3447d9077..f83f43333 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/PathInfoHandler.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/PathInfoHandler.java @@ -118,7 +118,7 @@ private String joinPath(String parentDir, String packageName) { if (!StringUtils.endsWith(parentDir, File.separator)) { parentDir += File.separator; } - packageName = packageName.replaceAll("\\.", StringPool.BACK_SLASH + File.separator); + packageName = packageName.replace(".", StringPool.BACK_SLASH + File.separator); return parentDir + packageName; } } diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java index 6978677c4..d548c955b 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java @@ -243,7 +243,7 @@ public void importPackage() { this.importPackages.add(IdType.class.getCanonicalName()); this.importPackages.add(TableId.class.getCanonicalName()); } - this.fields.forEach(field -> { + for (TableField field : this.fields) { IColumnType columnType = field.getColumnType(); if (null != columnType && null != columnType.getPkg()) { importPackages.add(columnType.getPkg()); @@ -273,7 +273,7 @@ public void importPackage() { if (field.isLogicDeleteField()) { this.importPackages.add(TableLogic.class.getCanonicalName()); } - }); + } } /** diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DbQueryDecorator.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DbQueryDecorator.java index 7c9fab03c..58533ac2c 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DbQueryDecorator.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DbQueryDecorator.java @@ -272,7 +272,7 @@ public String getTableComment() { * @return 格式化内容 */ public String formatComment(String comment) { - return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replaceAll("\r\n", "\t"); + return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replace("\r\n", "\t"); } /** diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java index c534bbb38..99b194234 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java @@ -19,7 +19,6 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.config.ConstVal; -import java.util.Arrays; import java.util.Set; /** @@ -61,15 +60,17 @@ public static String underlineToCamel(String name) { String[] camels = tempName.split(ConstVal.UNDERLINE); // 跳过原始字符串中开头、结尾的下换线或双重下划线 // 处理真正的驼峰片段 - Arrays.stream(camels).filter(camel -> !StringUtils.isBlank(camel)).forEach(camel -> { - if (result.length() == 0) { - // 第一个驼峰片段,首字母都小写 - result.append(StringUtils.firstToLowerCase(camel)); - } else { - // 其他的驼峰片段,首字母大写 - result.append(capitalFirst(camel)); + for (String camel : camels) { + if (!StringUtils.isBlank(camel)) { + if (result.length() == 0) { + // 第一个驼峰片段,首字母都小写 + result.append(StringUtils.firstToLowerCase(camel)); + } else { + // 其他的驼峰片段,首字母大写 + result.append(capitalFirst(camel)); + } } - }); + } return result.toString(); } diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java index 18d441044..78f00b3cc 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java @@ -68,15 +68,15 @@ public abstract class AbstractTemplateEngine { protected void outputCustomFile(@NotNull List customFiles, @NotNull TableInfo tableInfo, @NotNull Map objectMap) { String entityName = tableInfo.getEntityName(); String parentPath = getPathInfo(OutputFile.parent); - customFiles.forEach(file -> { + for (CustomFile file : customFiles) { String filePath = StringUtils.isNotBlank(file.getFilePath()) ? file.getFilePath() : parentPath; if (StringUtils.isNotBlank(file.getPackageName())) { - filePath = filePath + File.separator + file.getPackageName().replaceAll("\\.", StringPool.BACK_SLASH + File.separator); + filePath = filePath + File.separator + file.getPackageName().replace(".", StringPool.BACK_SLASH + File.separator); } Function formatNameFunction = file.getFormatNameFunction(); String fileName = filePath + File.separator + (null != formatNameFunction ? formatNameFunction.apply(tableInfo) : entityName) + file.getFileName(); outputFile(new File(fileName), objectMap, file.getTemplatePath(), file.isFileOverride()); - }); + } } /** @@ -231,14 +231,15 @@ public AbstractTemplateEngine batchOutput() { try { ConfigBuilder config = this.getConfigBuilder(); List tableInfoList = config.getTableInfoList(); - tableInfoList.forEach(tableInfo -> { + for (TableInfo tableInfo : tableInfoList) { Map objectMap = this.getObjectMap(config, tableInfo); - Optional.ofNullable(config.getInjectionConfig()).ifPresent(t -> { + InjectionConfig t = config.getInjectionConfig(); + if (t != null) { // 添加自定义属性 t.beforeOutputFile(tableInfo, objectMap); // 输出自定义文件 outputCustomFile(t.getCustomFiles(), tableInfo, objectMap); - }); + } // entity outputEntity(tableInfo, objectMap); // mapper and xml @@ -247,7 +248,7 @@ public AbstractTemplateEngine batchOutput() { outputService(tableInfo, objectMap); // controller outputController(tableInfo, objectMap); - }); + } } catch (Exception e) { throw new RuntimeException("无法创建文件,请检查配置信息!", e); } @@ -374,4 +375,5 @@ public AbstractTemplateEngine setConfigBuilder(@NotNull ConfigBuilder configBuil this.configBuilder = configBuilder; return this; } + } diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java index 98b16484a..df1b60217 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java @@ -138,7 +138,7 @@ private boolean isGeneratedOrAutoIncrementColumn(ResultSet resultSet, String col } public String formatComment(String comment) { - return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replaceAll("\r\n", "\t"); + return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replace("\r\n", "\t"); } public Table getTableInfo(String tableName) { diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/query/DefaultQuery.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/query/DefaultQuery.java index 4cf9aefae..5803272f5 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/query/DefaultQuery.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/query/DefaultQuery.java @@ -68,7 +68,7 @@ public DefaultQuery(@NotNull ConfigBuilder configBuilder) { //需要反向生成或排除的表信息 List includeTableList = new ArrayList<>(); List excludeTableList = new ArrayList<>(); - tables.forEach(table -> { + for (DatabaseMetaDataWrapper.Table table : tables) { String tableName = table.getName(); if (StringUtils.isNotBlank(tableName)) { TableInfo tableInfo = new TableInfo(this.configBuilder, tableName); @@ -80,7 +80,7 @@ public DefaultQuery(@NotNull ConfigBuilder configBuilder) { } tableList.add(tableInfo); } - }); + } filter(tableList, includeTableList, excludeTableList); // 性能优化,只处理需执行表字段 https://github.com/baomidou/mybatis-plus/issues/219 tableList.forEach(this::convertTableFields); @@ -106,7 +106,7 @@ protected void convertTableFields(@NotNull TableInfo tableInfo) { String tableName = tableInfo.getName(); Map columnsInfoMap = getColumnsInfo(tableName); Entity entity = strategyConfig.entity(); - columnsInfoMap.forEach((k, columnInfo) -> { + for (DatabaseMetaDataWrapper.Column columnInfo : columnsInfoMap.values()) { String columnName = columnInfo.getName(); TableField field = new TableField(this.configBuilder, columnName); // 处理ID @@ -131,11 +131,12 @@ protected void convertTableFields(@NotNull TableInfo tableInfo) { field.setPropertyName(propertyName, columnType); field.setMetaInfo(metaInfo); tableInfo.addField(field); - }); + } tableInfo.processTable(); } protected Map getColumnsInfo(String tableName) { return databaseMetaDataWrapper.getColumnsInfo(tableName, true); } + } diff --git a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java index cee4b900b..c32347359 100644 --- a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java +++ b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java @@ -60,7 +60,9 @@ protected void processSelectBody(Select selectBody, final String whereSegment) { SetOperationList operationList = (SetOperationList) selectBody; List selectBodyList = setOperationList.getSelects(); - selectBodyList.forEach(s -> this.setWhere((PlainSelect) s, (String) obj)); + for (Select s : selectBodyList) { + this.setWhere((PlainSelect) s, (String) obj); + } } } } @@ -172,4 +176,5 @@ public Expression buildTableExpression(final Table table, final Expression where final MultiDataPermissionHandler handler = (MultiDataPermissionHandler) dataPermissionHandler; return handler.getSqlSegment(table, where, whereSegment); } + } diff --git a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java index 86c5b578b..a36a18cae 100644 --- a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java +++ b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java @@ -90,7 +90,9 @@ protected void processSelect(Select select, int index, String sql, Object obj) { processSelectBody(select, whereSegment); List withItemsList = select.getWithItemsList(); if (!CollectionUtils.isEmpty(withItemsList)) { - withItemsList.forEach(withItem -> processSelectBody(withItem, whereSegment)); + for (WithItem withItem : withItemsList) { + processSelectBody(withItem, whereSegment); + } } } @@ -166,11 +168,13 @@ protected void processUpdate(Update update, int index, String sql, Object obj) { } List sets = update.getUpdateSets(); if (!CollectionUtils.isEmpty(sets)) { - sets.forEach(us -> us.getValues().forEach(ex -> { - if (ex instanceof Select) { - processSelectBody(((Select) ex), (String) obj); + for (UpdateSet us : sets) { + for (Expression ex : us.getValues()) { + if (ex instanceof Select) { + processSelectBody(((Select) ex), (String) obj); + } } - })); + } } update.setWhere(this.andExpression(table, update.getWhere(), (String) obj)); } @@ -195,7 +199,7 @@ protected void processDelete(Delete delete, int index, String sql, Object obj) { * @param selectBody SelectBody */ protected void processInsertSelect(Select selectBody, final String whereSegment) { - if(selectBody instanceof PlainSelect){ + if (selectBody instanceof PlainSelect) { PlainSelect plainSelect = (PlainSelect) selectBody; FromItem fromItem = plainSelect.getFromItem(); if (fromItem instanceof Table) { @@ -207,7 +211,7 @@ protected void processInsertSelect(Select selectBody, final String whereSegment) appendSelectItem(plainSelect.getSelectItems()); processInsertSelect(subSelect, whereSegment); } - } else if(selectBody instanceof ParenthesedSelect){ + } else if (selectBody instanceof ParenthesedSelect) { ParenthesedSelect parenthesedSelect = (ParenthesedSelect) selectBody; processInsertSelect(parenthesedSelect.getSelect(), whereSegment); @@ -272,4 +276,5 @@ public Expression buildTableExpression(final Table table, final Expression where } return new EqualsTo(getAliasColumn(table), tenantLineHandler.getTenantId()); } + } diff --git a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java index df05dbfab..fd6c661e1 100644 --- a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java +++ b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java @@ -61,7 +61,9 @@ protected void processSelectBody(Select selectBody, final String whereSegment) { SetOperationList operationList = (SetOperationList) selectBody; List selectBodyList = setOperationList.getSelects(); - selectBodyList.forEach(s -> this.setWhere((PlainSelect) s, (String) obj)); + for (Select s : selectBodyList) { + this.setWhere((PlainSelect) s, (String) obj); + } } } } diff --git a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java index 733f7a837..c6f4cbb29 100644 --- a/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java +++ b/mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java @@ -87,7 +87,9 @@ protected void processSelect(Select select, int index, String sql, Object obj) { processSelectBody(select, whereSegment); List withItemsList = select.getWithItemsList(); if (!CollectionUtils.isEmpty(withItemsList)) { - withItemsList.forEach(withItem -> processSelectBody(withItem, whereSegment)); + for (WithItem withItem : withItemsList) { + processSelectBody(withItem, whereSegment); + } } } @@ -156,11 +158,13 @@ protected void processUpdate(Update update, int index, String sql, Object obj) { } List sets = update.getUpdateSets(); if (!CollectionUtils.isEmpty(sets)) { - sets.forEach(us -> us.getValues().forEach(ex -> { - if (ex instanceof Select) { - processSelectBody(((Select) ex), (String) obj); + for (UpdateSet us : sets) { + for (Expression ex : us.getValues()) { + if (ex instanceof Select) { + processSelectBody(((Select) ex), (String) obj); + } } - })); + } } update.setWhere(this.andExpression(table, update.getWhere(), (String) obj)); } @@ -185,7 +189,7 @@ protected void processDelete(Delete delete, int index, String sql, Object obj) { * @param selectBody SelectBody */ protected void processInsertSelect(Select selectBody, final String whereSegment) { - if(selectBody instanceof PlainSelect){ + if (selectBody instanceof PlainSelect) { PlainSelect plainSelect = (PlainSelect) selectBody; FromItem fromItem = plainSelect.getFromItem(); if (fromItem instanceof Table) { @@ -197,7 +201,7 @@ protected void processInsertSelect(Select selectBody, final String whereSegment) appendSelectItem(plainSelect.getSelectItems()); processInsertSelect(subSelect, whereSegment); } - } else if(selectBody instanceof ParenthesedSelect){ + } else if (selectBody instanceof ParenthesedSelect) { ParenthesedSelect parenthesedSelect = (ParenthesedSelect) selectBody; processInsertSelect(parenthesedSelect.getSelect(), whereSegment); @@ -262,4 +266,5 @@ public Expression buildTableExpression(final Table table, final Expression where } return new EqualsTo(getAliasColumn(table), tenantLineHandler.getTenantId()); } + } diff --git a/mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java b/mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java index fd1f992dd..8af51eab0 100644 --- a/mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java +++ b/mybatis-plus-spring/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java @@ -101,7 +101,6 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean... typeHandlers) { this.typeHandlers = typeHandlers; } + /** * Set the default type handler class for enum. * @@ -290,6 +288,7 @@ public void setDefaultEnumTypeHandler( @SuppressWarnings("rawtypes") Class defaultEnumTypeHandler) { this.defaultEnumTypeHandler = defaultEnumTypeHandler; } + /** * List of type aliases to register. They can be annotated with {@code Alias} * @@ -430,6 +429,7 @@ public void setTransactionFactory(TransactionFactory transactionFactory) { public void setEnvironment(String environment) { this.environment = environment; } + /** * Set scripting language drivers. * @@ -543,7 +543,6 @@ public void afterPropertiesSet() throws Exception { this.sqlSessionFactory = buildSqlSessionFactory(); } - /** * Build a {@code SqlSessionFactory} instance. *

@@ -586,48 +585,53 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception { Optional.ofNullable(this.vfs).ifPresent(targetConfiguration::setVfsImpl); if (hasLength(this.typeAliasesPackage)) { - scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType).stream() - .filter(clazz -> !clazz.isAnonymousClass()).filter(clazz -> !clazz.isInterface()) - .filter(clazz -> !clazz.isMemberClass()).forEach(targetConfiguration.getTypeAliasRegistry()::registerAlias); + for (Class clazz : scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType)) { + if (!clazz.isAnonymousClass() && !clazz.isInterface() && !clazz.isMemberClass()) { + targetConfiguration.getTypeAliasRegistry().registerAlias(clazz); + } + } } if (!isEmpty(this.typeAliases)) { - Stream.of(this.typeAliases).forEach(typeAlias -> { + for (Class typeAlias : this.typeAliases) { targetConfiguration.getTypeAliasRegistry().registerAlias(typeAlias); LOGGER.debug(() -> "Registered type alias: '" + typeAlias + "'"); - }); + } } if (!isEmpty(this.plugins)) { - Stream.of(this.plugins).forEach(plugin -> { + for (Interceptor plugin : this.plugins) { targetConfiguration.addInterceptor(plugin); LOGGER.debug(() -> "Registered plugin: '" + plugin + "'"); - }); + } } if (hasLength(this.typeHandlersPackage)) { - scanClasses(this.typeHandlersPackage, TypeHandler.class).stream().filter(clazz -> !clazz.isAnonymousClass()) - .filter(clazz -> !clazz.isInterface()).filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())) - .forEach(targetConfiguration.getTypeHandlerRegistry()::register); + for (Class clazz : scanClasses(this.typeHandlersPackage, TypeHandler.class)) { + if (!clazz.isAnonymousClass() && !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) { + targetConfiguration.getTypeHandlerRegistry().register(clazz); + } + } } if (!isEmpty(this.typeHandlers)) { - Stream.of(this.typeHandlers).forEach(typeHandler -> { + for (TypeHandler typeHandler : this.typeHandlers) { targetConfiguration.getTypeHandlerRegistry().register(typeHandler); LOGGER.debug(() -> "Registered type handler: '" + typeHandler + "'"); - }); + } } targetConfiguration.setDefaultEnumTypeHandler(defaultEnumTypeHandler); if (!isEmpty(this.scriptingLanguageDrivers)) { - Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> { + for (LanguageDriver languageDriver : this.scriptingLanguageDrivers) { targetConfiguration.getLanguageRegistry().register(languageDriver); LOGGER.debug(() -> "Registered scripting language driver: '" + languageDriver + "'"); - }); + } + } + if (this.defaultScriptingLanguageDriver != null) { + targetConfiguration.setDefaultScriptingLanguage(this.defaultScriptingLanguageDriver); } - Optional.ofNullable(this.defaultScriptingLanguageDriver) - .ifPresent(targetConfiguration::setDefaultScriptingLanguage); if (this.databaseIdProvider != null) {// fix #64 set databaseId before parse mapper xmls try { @@ -752,4 +756,5 @@ private Set> scanClasses(String packagePatterns, Class assignableTyp } return classes; } + } diff --git a/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/DdlApplicationRunner.java b/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/DdlApplicationRunner.java index c8a719c30..684d5308d 100644 --- a/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/DdlApplicationRunner.java +++ b/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/DdlApplicationRunner.java @@ -43,8 +43,10 @@ public DdlApplicationRunner(List ddlList) { public void run(ApplicationArguments args) throws Exception { if (CollectionUtils.isNotEmpty(ddlList)) { log.debug(" ... DDL start create ... "); - ddlList.forEach(ddl -> ddl.runScript(dataSource -> DdlHelper.runScript(ddl.getDdlGenerator(), - dataSource, ddl.getSqlFiles(), true))); + for (IDdl ddl : ddlList) { + ddl.runScript(dataSource -> DdlHelper.runScript(ddl.getDdlGenerator(), + dataSource, ddl.getSqlFiles(), true)); + } log.debug(" ... DDL end create ... "); } } diff --git a/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java b/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java index a845afb46..ccb96dc52 100644 --- a/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java +++ b/spring-boot-starter/mybatis-plus-spring-boot-autoconfigure/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java @@ -145,7 +145,9 @@ public MybatisPlusAutoConfiguration(MybatisPlusProperties properties, @Override public void afterPropertiesSet() { if (!CollectionUtils.isEmpty(mybatisPlusPropertiesCustomizers)) { - mybatisPlusPropertiesCustomizers.forEach(i -> i.customize(properties)); + for (MybatisPlusPropertiesCustomizer i : mybatisPlusPropertiesCustomizers) { + i.customize(properties); + } } checkConfigFileExists(); } @@ -236,8 +238,7 @@ private void getBeanThen(Class clazz, Consumer consumer) { private void getBeansThen(Class clazz, Consumer> consumer) { if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) { final Map beansOfType = this.applicationContext.getBeansOfType(clazz); - List clazzList = new ArrayList<>(); - beansOfType.forEach((k, v) -> clazzList.add(v)); + List clazzList = new ArrayList<>(beansOfType.values()); consumer.accept(clazzList); } }