diff --git a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 09c7d92..8225017 100644 --- a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -1,6 +1,7 @@ package com.github.yulichang.interceptor; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.github.yulichang.method.MPJResultType; import com.github.yulichang.toolkit.Constant; import org.apache.ibatis.executor.Executor; @@ -43,11 +44,10 @@ public class MPJInterceptor implements Interceptor { Object[] args = invocation.getArgs(); if (args[0] instanceof MappedStatement) { MappedStatement ms = (MappedStatement) args[0]; - Object parameter = args[1]; - if (parameter instanceof Map) { - Map map = (Map) parameter; + if (args[1] instanceof Map) { + Map map = (Map) args[1]; if (CollectionUtils.isNotEmpty(map)) { - try { + if (map.containsKey(Constant.CLAZZ)) { Class clazz = (Class) map.get(Constant.CLAZZ); if (Objects.nonNull(clazz)) { List 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 */ 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); if (Objects.nonNull(statement)) { return statement; } - MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, ms.getSqlSource(), ms.getSqlCommandType()); - builder.resource(ms.getResource()) + MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), id, ms.getSqlSource(), ms.getSqlCommandType()) + .resource(ms.getResource()) .fetchSize(ms.getFetchSize()) .statementType(ms.getStatementType()) - .keyGenerator(ms.getKeyGenerator()); - if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { - StringBuilder keyProperties = new StringBuilder(); - 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 resultMaps = new ArrayList<>(); - ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), resultType, EMPTY_RESULT_MAPPING).build(); - resultMaps.add(resultMap); - builder.resultMaps(resultMaps) + .keyGenerator(ms.getKeyGenerator()) + .timeout(ms.getTimeout()) + .parameterMap(ms.getParameterMap()) .resultSetType(ms.getResultSetType()) .cache(ms.getCache()) .flushCacheRequired(ms.isFlushCacheRequired()) .useCache(ms.isUseCache()); + + if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { + builder.keyProperty(String.join(StringPool.COMMA, ms.getKeyProperties())); + } + + List resultMaps = new ArrayList<>(); + resultMaps.add(new ResultMap.Builder(ms.getConfiguration(), ms.getId(), resultType, EMPTY_RESULT_MAPPING).build()); + builder.resultMaps(resultMaps); + MappedStatement mappedStatement = builder.build(); MS_CACHE.put(id, mappedStatement); return mappedStatement;