This commit is contained in:
admin 2021-03-31 16:09:46 +08:00
parent 1c969190d9
commit 624f9a8896

View File

@ -1,6 +1,7 @@
package com.github.yulichang.interceptor; package com.github.yulichang.interceptor;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.github.yulichang.method.MPJResultType; import com.github.yulichang.method.MPJResultType;
import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.Constant;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
@ -43,11 +44,10 @@ public class MPJInterceptor implements Interceptor {
Object[] args = invocation.getArgs(); Object[] args = invocation.getArgs();
if (args[0] instanceof MappedStatement) { if (args[0] instanceof MappedStatement) {
MappedStatement ms = (MappedStatement) args[0]; MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1]; if (args[1] instanceof Map) {
if (parameter instanceof Map) { Map<String, ?> map = (Map<String, ?>) args[1];
Map<String, ?> map = (Map<String, ?>) parameter;
if (CollectionUtils.isNotEmpty(map)) { if (CollectionUtils.isNotEmpty(map)) {
try { if (map.containsKey(Constant.CLAZZ)) {
Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ); Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ);
if (Objects.nonNull(clazz)) { if (Objects.nonNull(clazz)) {
List<ResultMap> list = ms.getResultMaps(); List<ResultMap> list = ms.getResultMaps();
@ -58,8 +58,6 @@ public class MPJInterceptor implements Interceptor {
} }
} }
} }
} catch (Exception e) {
//通常是MapperMethod内部类HashMap的子类ParamMap重写了了get方法抛出的BindingException
} }
} }
} }
@ -71,35 +69,31 @@ public class MPJInterceptor implements Interceptor {
* 构建新的MappedStatement * 构建新的MappedStatement
*/ */
public MappedStatement newMappedStatement(MappedStatement ms, Class<?> resultType) { public MappedStatement newMappedStatement(MappedStatement ms, Class<?> resultType) {
String id = ms.getId() + "_" + resultType.getName(); String id = ms.getId() + StringPool.UNDERSCORE + resultType.getName();
MappedStatement statement = MS_CACHE.get(id); MappedStatement statement = MS_CACHE.get(id);
if (Objects.nonNull(statement)) { if (Objects.nonNull(statement)) {
return statement; return statement;
} }
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, ms.getSqlSource(), ms.getSqlCommandType()); MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, ms.getSqlSource(), ms.getSqlCommandType())
builder.resource(ms.getResource()) .resource(ms.getResource())
.fetchSize(ms.getFetchSize()) .fetchSize(ms.getFetchSize())
.statementType(ms.getStatementType()) .statementType(ms.getStatementType())
.keyGenerator(ms.getKeyGenerator()); .keyGenerator(ms.getKeyGenerator())
if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { .timeout(ms.getTimeout())
StringBuilder keyProperties = new StringBuilder(); .parameterMap(ms.getParameterMap())
for (String keyProperty : ms.getKeyProperties()) {
keyProperties.append(keyProperty).append(",");
}
keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
builder.keyProperty(keyProperties.toString());
}
builder.timeout(ms.getTimeout())
.parameterMap(ms.getParameterMap());
//count查询返回值int
List<ResultMap> resultMaps = new ArrayList<>();
ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), resultType, EMPTY_RESULT_MAPPING).build();
resultMaps.add(resultMap);
builder.resultMaps(resultMaps)
.resultSetType(ms.getResultSetType()) .resultSetType(ms.getResultSetType())
.cache(ms.getCache()) .cache(ms.getCache())
.flushCacheRequired(ms.isFlushCacheRequired()) .flushCacheRequired(ms.isFlushCacheRequired())
.useCache(ms.isUseCache()); .useCache(ms.isUseCache());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
builder.keyProperty(String.join(StringPool.COMMA, ms.getKeyProperties()));
}
List<ResultMap> resultMaps = new ArrayList<>();
resultMaps.add(new ResultMap.Builder(ms.getConfiguration(), ms.getId(), resultType, EMPTY_RESULT_MAPPING).build());
builder.resultMaps(resultMaps);
MappedStatement mappedStatement = builder.build(); MappedStatement mappedStatement = builder.build();
MS_CACHE.put(id, mappedStatement); MS_CACHE.put(id, mappedStatement);
return mappedStatement; return mappedStatement;