浏览代码

增加Map工具类.

nieqiuqiu 5 年之前
父节点
当前提交
0a81b365e3

+ 1 - 0
build.gradle

@@ -134,6 +134,7 @@ subprojects {
         exclude "**/test/**/*.java"
         exclude "**/*Test.java"
         exclude "**/TableNameParser.java"
+        exclude "**/Maps.java"
         mapping "java", "SLASHSTAR_STYLE"
         mapping "kt", "SLASHSTAR_STYLE"
         ignoreFailures = true

+ 5 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -22,7 +22,6 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
 
 import java.lang.ref.WeakReference;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
@@ -98,11 +97,15 @@ public final class LambdaUtils {
      * @return 缓存 map
      */
     private static Map<String, ColumnCache> createColumnCacheMap(TableInfo info) {
-        Map<String, ColumnCache> map = new HashMap<>();
 
         String kp = info.getKeyProperty();
+        Map<String, ColumnCache> map;
+
         if (StringUtils.isNotBlank(kp)) {
+            map = Maps.newHashMapWithExpectedSize(info.getFieldList().size() + 1);
             map.put(formatKey(kp), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect()));
+        } else {
+            map = Maps.newHashMapWithExpectedSize(info.getFieldList().size());
         }
 
         info.getFieldList().forEach(i ->

+ 52 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Maps.java

@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2007 The Guava Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.baomidou.mybatisplus.core.toolkit;
+
+import java.util.HashMap;
+
+/**
+ * com.google.common.collect.Maps
+ */
+public final class Maps {
+
+    private static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);
+
+
+    public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) {
+        return new HashMap<>(capacity(expectedSize));
+    }
+
+    /**
+     * Returns a capacity that is sufficient to keep the map from being resized as
+     * long as it grows no larger than expectedSize and the load factor is >= its
+     * default (0.75).
+     */
+    private static int capacity(int expectedSize) {
+        if (expectedSize < 3) {
+            if (expectedSize < 0) {
+                throw new IllegalArgumentException("expectedSize cannot be negative but was: " + expectedSize);
+            }
+            return expectedSize + 1;
+        }
+        if (expectedSize < MAX_POWER_OF_TWO) {
+            // This is the calculation used in JDK8 to resize when a putAll
+            // happens; it seems to be the most conservative calculation we
+            // can make.  0.75 is the default load factor.
+            return (int) ((float) expectedSize / 0.75F + 1.0F);
+        }
+        return Integer.MAX_VALUE; // any large value
+    }
+}

+ 6 - 7
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java

@@ -28,7 +28,6 @@ import org.apache.ibatis.session.SqlSession;
 import org.mybatis.spring.SqlSessionUtils;
 
 import java.io.Serializable;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -97,7 +96,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param queryWrapper 实体对象封装操作类(可以为 null)
      */
     public boolean delete(Wrapper<T> queryWrapper) {
-        Map<String, Object> map = new HashMap<>(1);
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(1);
         map.put(Constants.WRAPPER, queryWrapper);
         SqlSession sqlSession = sqlSession();
         try {
@@ -113,7 +112,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
     public boolean updateById() {
         Assert.isFalse(StringUtils.checkValNull(pkVal()), "updateById primaryKey is null.");
         // updateById
-        Map<String, Object> map = new HashMap<>(1);
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(1);
         map.put(Constants.ENTITY, this);
         SqlSession sqlSession = sqlSession();
         try {
@@ -129,7 +128,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
      */
     public boolean update(Wrapper<T> updateWrapper) {
-        Map<String, Object> map = new HashMap<>(2);
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
         map.put(Constants.ENTITY, this);
         map.put(Constants.WRAPPER, updateWrapper);
         // update
@@ -181,7 +180,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param queryWrapper 实体对象封装操作类(可以为 null)
      */
     public List<T> selectList(Wrapper<T> queryWrapper) {
-        Map<String, Object> map = new HashMap<>(1);
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(1);
         map.put(Constants.WRAPPER, queryWrapper);
         SqlSession sqlSession = sqlSession();
         try {
@@ -207,7 +206,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param queryWrapper 实体对象封装操作类(可以为 null)
      */
     public <E extends IPage<T>> E selectPage(E page, Wrapper<T> queryWrapper) {
-        Map<String, Object> map = new HashMap<>(2);
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
         map.put(Constants.WRAPPER, queryWrapper);
         map.put("page", page);
         SqlSession sqlSession = sqlSession();
@@ -225,7 +224,7 @@ public abstract class Model<T extends Model<?>> implements Serializable {
      * @param queryWrapper 实体对象封装操作类(可以为 null)
      */
     public Integer selectCount(Wrapper<T> queryWrapper) {
-        Map<String, Object> map = new HashMap<>(1);
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(1);
         map.put(Constants.WRAPPER, queryWrapper);
         SqlSession sqlSession = sqlSession();
         try {

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

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.extension.toolkit;
 import com.baomidou.mybatisplus.core.assist.ISqlRunner;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.core.toolkit.Maps;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
@@ -26,7 +27,6 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionUtils;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -108,7 +108,7 @@ public class SqlRunner implements ISqlRunner {
      * @return ignore
      */
     private Map<String, String> sqlMap(String sql, Object... args) {
-        Map<String, String> sqlMap = new HashMap<>();
+        Map<String, String> sqlMap = Maps.newHashMapWithExpectedSize(1);
         sqlMap.put(SQL, StringUtils.sqlArgsFill(sql, args));
         return sqlMap;
     }
@@ -122,7 +122,7 @@ public class SqlRunner implements ISqlRunner {
      * @return ignore
      */
     private Map<String, Object> sqlMap(String sql, IPage page, Object... args) {
-        Map<String, Object> sqlMap = new HashMap<>();
+        Map<String, Object> sqlMap = Maps.newHashMapWithExpectedSize(2);
         sqlMap.put(PAGE, page);
         sqlMap.put(SQL, StringUtils.sqlArgsFill(sql, args));
         return sqlMap;

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

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.generator.config.builder;
 
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Maps;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.generator.InjectionConfig;
@@ -34,7 +35,6 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -238,7 +238,7 @@ public class ConfigBuilder {
      */
     private void handlerPackage(TemplateConfig template, String outputDir, PackageConfig config) {
         // 包信息
-        packageInfo = new HashMap<>(10);
+        packageInfo = Maps.newHashMapWithExpectedSize(7);
         packageInfo.put(ConstVal.MODULE_NAME, config.getModuleName());
         packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), config.getEntity()));
         packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), config.getMapper()));
@@ -253,7 +253,7 @@ public class ConfigBuilder {
             pathInfo = configPathInfo;
         } else {
             // 生成路径信息
-            pathInfo = new HashMap<>(10);
+            pathInfo = Maps.newHashMapWithExpectedSize(6);
             setPathInfo(pathInfo, template.getEntity(getGlobalConfig().isKotlin()), outputDir, ConstVal.ENTITY_PATH, ConstVal.ENTITY);
             setPathInfo(pathInfo, template.getMapper(), outputDir, ConstVal.MAPPER_PATH, ConstVal.MAPPER);
             setPathInfo(pathInfo, template.getXml(), outputDir, ConstVal.XML_PATH, ConstVal.XML);
@@ -647,7 +647,7 @@ public class ConfigBuilder {
                     // 自定义字段查询
                     String[] fcs = dbQuery.fieldCustom();
                     if (null != fcs) {
-                        Map<String, Object> customMap = new HashMap<>(fcs.length);
+                        Map<String, Object> customMap = Maps.newHashMapWithExpectedSize(fcs.length);
                         for (String fc : fcs) {
                             customMap.put(fc, results.getObject(fc));
                         }

+ 5 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.generator.engine;
 
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Maps;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.generator.InjectionConfig;
@@ -192,11 +193,14 @@ public abstract class AbstractTemplateEngine {
      * @return ignore
      */
     public Map<String, Object> getObjectMap(TableInfo tableInfo) {
-        Map<String, Object> objectMap = new HashMap<>(30);
+        Map<String, Object> objectMap;
         ConfigBuilder config = getConfigBuilder();
         if (config.getStrategyConfig().isControllerMappingHyphenStyle()) {
+            objectMap = Maps.newHashMapWithExpectedSize(33);
             objectMap.put("controllerMappingHyphenStyle", config.getStrategyConfig().isControllerMappingHyphenStyle());
             objectMap.put("controllerMappingHyphen", StringUtils.camelToHyphen(tableInfo.getEntityPath()));
+        } else {
+            objectMap = Maps.newHashMapWithExpectedSize(31);
         }
         objectMap.put("restControllerStyle", config.getStrategyConfig().isRestControllerStyle());
         objectMap.put("config", config);