add CacheKeyBuilder.java interface

This commit is contained in:
开源海哥 2023-04-16 19:37:42 +08:00
parent acf1106bee
commit c33d83edfe
6 changed files with 182 additions and 12 deletions

View File

@ -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 的主键生成器

View File

@ -0,0 +1,32 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}
}

View File

@ -0,0 +1,38 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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);
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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);
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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);
}
}

View File

@ -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);
}