diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 07ba1f08..cd0206ce 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -20,12 +20,16 @@ import com.mybatisflex.core.keygen.MultiEntityKeyGenerator; import com.mybatisflex.core.keygen.MultiRowKeyGenerator; import com.mybatisflex.core.keygen.MybatisKeyGeneratorUtil; import com.mybatisflex.core.keygen.RowKeyGenerator; +import com.mybatisflex.core.mybatis.executor.FlexBatchExecutor; +import com.mybatisflex.core.mybatis.executor.FlexReuseExecutor; +import com.mybatisflex.core.mybatis.executor.FlexSimpleExecutor; import com.mybatisflex.core.row.RowMapper; import com.mybatisflex.core.table.EntityWrapperFactory; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.StringUtil; +import org.apache.ibatis.executor.CachingExecutor; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.keygen.KeyGenerator; import org.apache.ibatis.executor.keygen.NoKeyGenerator; @@ -36,10 +40,8 @@ import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ResultMap; -import org.apache.ibatis.session.Configuration; -import org.apache.ibatis.session.ResultHandler; -import org.apache.ibatis.session.RowBounds; -import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.*; +import org.apache.ibatis.transaction.Transaction; import java.lang.reflect.Proxy; import java.util.Map; @@ -100,6 +102,29 @@ public class FlexConfiguration extends Configuration { } + /** + * 替换为 Flex 的 Executor,主要用于重建 CacheKey + * 默认情况下,Mybatis 的 CacheKey 构建是必须有 ParameterMapping,而 Flex 的 select 是不带有 ParameterMapping 的 + */ + @Override + public Executor newExecutor(Transaction transaction, ExecutorType executorType) { + executorType = executorType == null ? defaultExecutorType : executorType; + Executor executor; + if (ExecutorType.BATCH == executorType) { + executor = new FlexBatchExecutor(this, transaction); + } else if (ExecutorType.REUSE == executorType) { + executor = new FlexReuseExecutor(this, transaction); + } else { + executor = new FlexSimpleExecutor(this, transaction); + } + if (cacheEnabled) { + executor = new CachingExecutor(executor); + } + executor = (Executor) interceptorChain.pluginAll(executor); + return executor; + } + + @Override public void addMappedStatement(MappedStatement ms) { //替换 RowMapper.insert 的主键生成器 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/CacheKeyBuilder.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/CacheKeyBuilder.java new file mode 100644 index 00000000..c3757a2a --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/CacheKeyBuilder.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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.mybatisflex.core.mybatis.executor; + +import com.mybatisflex.core.FlexConsts; +import org.apache.ibatis.cache.CacheKey; + +import java.util.Arrays; +import java.util.Map; + +public interface CacheKeyBuilder { + + default CacheKey buildCacheKey(CacheKey cacheKey, Object parameterObject){ + if (parameterObject instanceof Map && ((Map) parameterObject).containsKey(FlexConsts.SQL_ARGS)){ + cacheKey.update(Arrays.toString((Object[]) ((Map) parameterObject).get(FlexConsts.SQL_ARGS))); + } + return cacheKey; + } +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexBatchExecutor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexBatchExecutor.java new file mode 100644 index 00000000..f85b6854 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexBatchExecutor.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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.mybatisflex.core.mybatis.executor; + +import org.apache.ibatis.cache.CacheKey; +import org.apache.ibatis.executor.BatchExecutor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.transaction.Transaction; + +public class FlexBatchExecutor extends BatchExecutor implements CacheKeyBuilder { + + public FlexBatchExecutor(Configuration configuration, Transaction transaction) { + super(configuration, transaction); + } + + @Override + public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) { + return buildCacheKey(super.createCacheKey(ms, parameterObject, rowBounds, boundSql),parameterObject); + } + + +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexReuseExecutor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexReuseExecutor.java new file mode 100644 index 00000000..474ef692 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexReuseExecutor.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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.mybatisflex.core.mybatis.executor; + +import org.apache.ibatis.cache.CacheKey; +import org.apache.ibatis.executor.ReuseExecutor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.transaction.Transaction; + +public class FlexReuseExecutor extends ReuseExecutor implements CacheKeyBuilder { + + public FlexReuseExecutor(Configuration configuration, Transaction transaction) { + super(configuration, transaction); + } + + @Override + public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) { + return buildCacheKey(super.createCacheKey(ms, parameterObject, rowBounds, boundSql),parameterObject); + } +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexSimpleExecutor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexSimpleExecutor.java new file mode 100644 index 00000000..8942230d --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/executor/FlexSimpleExecutor.java @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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.mybatisflex.core.mybatis.executor; + +import org.apache.ibatis.cache.CacheKey; +import org.apache.ibatis.executor.SimpleExecutor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.transaction.Transaction; + +public class FlexSimpleExecutor extends SimpleExecutor implements CacheKeyBuilder { + + public FlexSimpleExecutor(Configuration configuration, Transaction transaction) { + super(configuration, transaction); + } + + @Override + public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) { + return buildCacheKey(super.createCacheKey(ms, parameterObject, rowBounds, boundSql),parameterObject); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index 7ad7b89c..cf5196da 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -50,16 +50,19 @@ public class AccountController { @Transactional public Account selectOne(@PathVariable("id") Long id) { - Account account = new Account(); - account.setId(1L); - account.setUserName("heihei"); - accountMapper.update(account); +// Account account = new Account(); +// account.setId(1L); +// account.setUserName("heihei"); +// accountMapper.update(account); +// +// +// accountService.update2(); + Account account1 = accountMapper.selectOneById(1L); + Account account2 = accountMapper.selectOneById(2L); - accountService.update2(); - -// update2(); - + System.out.println("selectOne >>>> " + account1); + System.out.println("selectOne >>>> " + account2); return accountMapper.selectOneById(id); }