Sfoglia il codice sorgente

先给咩咩改一点点吧,等下说我没搬砖。

nieqiuqiu 5 anni fa
parent
commit
1eb799b20a

+ 0 - 25
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java

@@ -16,22 +16,15 @@
 package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
-import com.baomidou.mybatisplus.core.executor.MybatisBatchExecutor;
-import com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor;
-import com.baomidou.mybatisplus.core.executor.MybatisReuseExecutor;
-import com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import org.apache.ibatis.binding.MapperRegistry;
-import org.apache.ibatis.executor.Executor;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
 import org.apache.ibatis.mapping.Environment;
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.scripting.LanguageDriver;
 import org.apache.ibatis.session.Configuration;
-import org.apache.ibatis.session.ExecutorType;
 import org.apache.ibatis.session.SqlSession;
-import org.apache.ibatis.transaction.Transaction;
 
 /**
  * replace default Configuration class
@@ -161,22 +154,4 @@ public class MybatisConfiguration extends Configuration {
         getLanguageRegistry().setDefaultDriverClass(driver);
     }
 
-    @Override
-    public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
-        executorType = executorType == null ? defaultExecutorType : executorType;
-        executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
-        Executor executor;
-        if (ExecutorType.BATCH == executorType) {
-            executor = new MybatisBatchExecutor(this, transaction);
-        } else if (ExecutorType.REUSE == executorType) {
-            executor = new MybatisReuseExecutor(this, transaction);
-        } else {
-            executor = new MybatisSimpleExecutor(this, transaction);
-        }
-        if (cacheEnabled) {
-            executor = new MybatisCachingExecutor(executor);
-        }
-        executor = (Executor) interceptorChain.pluginAll(executor);
-        return executor;
-    }
 }

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/AbstractBaseExecutor.java

@@ -38,7 +38,9 @@ import java.util.Optional;
  * 重写执行器 {@link org.apache.ibatis.executor.BaseExecutor}
  *
  * @author nieqiurong 2019/4/22.
+ * @deprecated 3.3.3
  */
+@Deprecated
 public abstract class AbstractBaseExecutor extends BaseExecutor {
 
     protected AbstractBaseExecutor(Configuration configuration, Transaction transaction) {

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisBatchExecutor.java

@@ -41,7 +41,9 @@ import java.util.List;
  * 重写执行器 {@link org.apache.ibatis.executor.BatchExecutor}
  *
  * @author nieqiurong 2019/4/14.
+ * @deprecated 3.3.3
  */
+@Deprecated
 public class MybatisBatchExecutor extends AbstractBaseExecutor {
     public static final int BATCH_UPDATE_RETURN_VALUE = Integer.MIN_VALUE + 1002;
 

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisCachingExecutor.java

@@ -42,7 +42,9 @@ import java.util.Optional;
  * copy org.apache.ibatis.executor.CachingExecutor 主要修改了分页缓存逻辑
  *
  * @author nieqiuqiu
+ * @deprecated 3.3.3
  */
+@Deprecated
 public class MybatisCachingExecutor implements Executor {
 
     private final Executor delegate;

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisReuseExecutor.java

@@ -38,7 +38,9 @@ import java.util.Map;
  * 重写执行器 {@link org.apache.ibatis.executor.ReuseExecutor}
  *
  * @author nieqiurong 2019/4/14.
+ * @deprecated 3.3.3
  */
+@Deprecated
 public class MybatisReuseExecutor extends AbstractBaseExecutor {
 
     private final Map<String, Statement> statementMap = new HashMap<>();

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisSimpleExecutor.java

@@ -36,7 +36,9 @@ import java.util.List;
  * 重写执行器 {@link org.apache.ibatis.executor.SimpleExecutor}
  *
  * @author nieqiurong 2019/4/14.
+ * @deprecated 3.3.3
  */
+@Deprecated
 public class MybatisSimpleExecutor extends AbstractBaseExecutor {
 
     public MybatisSimpleExecutor(Configuration configuration, Transaction transaction) {

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/IPage.java

@@ -135,6 +135,7 @@ public interface IPage<T> extends Serializable {
      * @return 是否命中count缓存
      * @since 3.3.1
      */
+    @Deprecated
     default boolean isHitCount() {
         return false;
     }

+ 3 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/MybatisPlusInterceptor.java

@@ -1,6 +1,7 @@
 package com.baomidou.mybatisplus.extension.plugins;
 
 import com.baomidou.mybatisplus.extension.plugins.chain.BeforeQuery;
+import com.baomidou.mybatisplus.extension.plugins.chain.PageBeforeQuery;
 import org.apache.ibatis.cache.CacheKey;
 import org.apache.ibatis.executor.Executor;
 import org.apache.ibatis.mapping.BoundSql;
@@ -9,9 +10,7 @@ import org.apache.ibatis.plugin.*;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.RowBounds;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Properties;
+import java.util.*;
 
 /**
  * @author miemie
@@ -26,7 +25,7 @@ import java.util.Properties;
 )
 public class MybatisPlusInterceptor implements Interceptor {
 
-    private final List<BeforeQuery> beforeQueries = new ArrayList<>();
+    private final List<BeforeQuery> beforeQueries = Collections.singletonList(new PageBeforeQuery());
 
     @Override
     public Object intercept(Invocation invocation) throws Throwable {

+ 5 - 4
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/chain/PageBeforeQuery.java

@@ -85,11 +85,12 @@ public class PageBeforeQuery implements BeforeQuery {
             handlerLimit(page);
         }
         String originalSql = boundSql.getSql();
-        if (page.isSearchCount() && !page.isHitCount()) {
+        if (page.isSearchCount()) {
             SqlInfo sqlInfo = SqlParserUtils.getOptimizeCountSql(page.optimizeCountSql(), countSqlParser, originalSql, null);
-            ms = buildCountMappedStatement(ms);
-            CacheKey cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
-            long count = (long) executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql).get(0);
+            MappedStatement countMappedStatement = buildCountMappedStatement(ms);
+            BoundSql countSql = new BoundSql(countMappedStatement.getConfiguration(), sqlInfo.getSql(), boundSql.getParameterMappings(), parameter);
+            CacheKey cacheKey = executor.createCacheKey(countMappedStatement, parameter, rowBounds, countMappedStatement.getBoundSql(parameter));
+            long count = (long) executor.query(countMappedStatement, parameter, rowBounds, resultHandler, cacheKey, countSql).get(0);
             page.setTotal(count);
             if (!this.continueLimit(page)) {
                 return boundSql;

+ 3 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/cache/CacheConfig.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.test.h2.cache;
 
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
 import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
 import org.apache.ibatis.session.ExecutorType;
@@ -43,7 +44,8 @@ public class CacheConfig {
         configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
         configuration.setCacheEnabled(true);
         sqlSessionFactory.setConfiguration(configuration);
-        PaginationInterceptor pagination = new PaginationInterceptor();
+        MybatisPlusInterceptor pagination = new MybatisPlusInterceptor();
+
         sqlSessionFactory.setPlugins(pagination);
         return sqlSessionFactory.getObject();
     }

+ 0 - 11
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/cache/CacheTest.java

@@ -34,48 +34,37 @@ class CacheTest {
     void testPageCache() {
         Cache cache = getCache();
         IPage<CacheModel> cacheModelIPage1 = cacheService.page(new Page<>(1, 3), new QueryWrapper<>());
-        Assertions.assertFalse(cacheModelIPage1.isHitCount());
         IPage<CacheModel> cacheModelIPage2 = cacheService.page(new Page<>(1, 3), new QueryWrapper<>());
-        Assertions.assertTrue(cacheModelIPage2.isHitCount());
         Assertions.assertEquals(cache.getSize(), 2);
         Assertions.assertEquals(cacheModelIPage1.getTotal(), cacheModelIPage2.getTotal());
         Assertions.assertEquals(cacheModelIPage1.getRecords().size(), cacheModelIPage2.getRecords().size());
         IPage<CacheModel> cacheModelIPage3 = cacheService.page(new Page<>(2, 3), new QueryWrapper<>());
-        Assertions.assertTrue(cacheModelIPage3.isHitCount());
         Assertions.assertEquals(cacheModelIPage1.getTotal(), cacheModelIPage3.getTotal());
         Assertions.assertEquals(cacheModelIPage3.getRecords().size(), 2);
         Assertions.assertEquals(cache.getSize(), 3);
         IPage<CacheModel> cacheModelIPage4 = cacheService.page(new Page<>(2, 3, false), new QueryWrapper<>());
-        Assertions.assertFalse(cacheModelIPage4.isHitCount());
         Assertions.assertEquals(cacheModelIPage4.getTotal(), 0L);
         Assertions.assertEquals(cacheModelIPage4.getRecords().size(), 2);
         Assertions.assertEquals(cache.getSize(), 3);
         IPage<CacheModel> cacheModelIPage5 = cacheService.page(new Page<>(2, 3, true), new QueryWrapper<>());
-        Assertions.assertTrue(cacheModelIPage5.isHitCount());
         Assertions.assertEquals(cacheModelIPage5.getTotal(), cacheModelIPage3.getTotal());
         Assertions.assertEquals(cacheModelIPage5.getRecords().size(), 2);
         IPage<CacheModel> cacheModelIPage6 = cacheService.page(new Page<>(1, 3, true), new QueryWrapper<CacheModel>().ge("id", 2L));
-        Assertions.assertFalse(cacheModelIPage6.isHitCount());
         Assertions.assertEquals(cacheModelIPage6.getTotal(), 4);
         Assertions.assertEquals(cacheModelIPage6.getRecords().size(), 3);
         IPage<CacheModel> cacheModelIPage7 = cacheService.page(new Page<>(1, 3, false), new QueryWrapper<CacheModel>().ge("id", 2L));
-        Assertions.assertFalse(cacheModelIPage7.isHitCount());
         Assertions.assertEquals(cacheModelIPage7.getTotal(), 0L);
         Assertions.assertEquals(cacheModelIPage7.getRecords().size(), 3);
         IPage<CacheModel> cacheModelIPage8 = cacheService.page(new Page<>(1, 3, false), new QueryWrapper<CacheModel>().ge("id", 3L));
-        Assertions.assertFalse(cacheModelIPage8.isHitCount());
         Assertions.assertEquals(cacheModelIPage8.getTotal(), 0L);
         Assertions.assertEquals(cacheModelIPage8.getRecords().size(), 3);
         cacheModelIPage8 = cacheService.page(new Page<>(1, 3, false), new QueryWrapper<CacheModel>().ge("id", 3L));
-        Assertions.assertFalse(cacheModelIPage8.isHitCount());
         Assertions.assertEquals(cacheModelIPage8.getTotal(), 0L);
         Assertions.assertEquals(cacheModelIPage8.getRecords().size(), 3);
         IPage<CacheModel> cacheModelIPage9 = cacheService.page(new Page<>(1, 3, true), new QueryWrapper<CacheModel>().ge("id", 3L));
-        Assertions.assertFalse(cacheModelIPage9.isHitCount());
         Assertions.assertEquals(cacheModelIPage9.getTotal(), 3L);
         Assertions.assertEquals(cacheModelIPage9.getRecords().size(), 3);
         cacheModelIPage9 = cacheService.page(new Page<>(1, 3, true), new QueryWrapper<CacheModel>().ge("id", 3L));
-        Assertions.assertTrue(cacheModelIPage9.isHitCount());
         Assertions.assertEquals(cacheModelIPage9.getTotal(), 3L);
         Assertions.assertEquals(cacheModelIPage9.getRecords().size(), 3);
     }

+ 2 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/config/MybatisPlusConfig.java

@@ -26,6 +26,7 @@ import com.baomidou.mybatisplus.core.parser.ISqlParser;
 import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
 import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
 import com.baomidou.mybatisplus.extension.injector.methods.LogicDeleteByIdWithFill;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
@@ -75,7 +76,7 @@ public class MybatisPlusConfig {
         configuration.setDefaultExecutorType(ExecutorType.REUSE);
         configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);  //默认枚举处理
         sqlSessionFactory.setConfiguration(configuration);
-        PaginationInterceptor pagination = new PaginationInterceptor();
+        MybatisPlusInterceptor pagination = new MybatisPlusInterceptor();
         SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
         List<ISqlParser> sqlParserList = new ArrayList<>();
         sqlParserList.add(new AbstractJsqlParser() {