From a7833d270d0d395a586992a20e575622f6fa916a Mon Sep 17 00:00:00 2001 From: miemie Date: Sat, 7 Nov 2020 20:15:06 +0800 Subject: [PATCH] fix: https://github.com/baomidou/mybatis-plus/issues/3038 --- .../mybatisplus/core/InjectorResolver.java | 39 ++++++++++++++++ .../core/MybatisMapperAnnotationBuilder.java | 13 +++++- .../core/MybatisMethodResolver.java | 5 +- .../core/injector/AbstractMethod.java | 1 + .../core/injector/methods/Insert.java | 2 +- .../MybatisMapperAnnotationBuilderTest.java | 46 +++++++++++++++++++ 6 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/InjectorResolver.java create mode 100644 mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilderTest.java diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/InjectorResolver.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/InjectorResolver.java new file mode 100644 index 0000000000..1cc23a9e13 --- /dev/null +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/InjectorResolver.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2011-2020, baomidou (jobob@qq.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 + *

+ * https://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.baomidou.mybatisplus.core; + +import org.apache.ibatis.builder.annotation.MethodResolver; + +/** + * 继承 {@link MethodResolver} + * + * @author miemie + * @since 2019-01-05 + */ +public class InjectorResolver extends MethodResolver { + + private final MybatisMapperAnnotationBuilder annotationBuilder; + + public InjectorResolver(MybatisMapperAnnotationBuilder annotationBuilder) { + super(annotationBuilder, null); + this.annotationBuilder = annotationBuilder; + } + + @Override + public void resolve() { + annotationBuilder.parserInjector(); + } +} diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilder.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilder.java index 6b2cb60cfd..27654ac7b4 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilder.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilder.java @@ -117,13 +117,22 @@ public void parse() { } } // TODO 注入 CURD 动态 SQL , 放在在最后, because 可能会有人会用注解重写sql - if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) { - GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type); + try { + // https://github.com/baomidou/mybatis-plus/issues/3038 + if (GlobalConfigUtils.isSupperMapperChildren(configuration, type)) { + parserInjector(); + } + } catch (IncompleteElementException e) { + configuration.addIncompleteMethod(new InjectorResolver(this)); } } parsePendingMethods(); } + void parserInjector() { + GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type); + } + private boolean canHaveStatement(Method method) { // issue #237 return !method.isBridge() && !method.isDefault(); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMethodResolver.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMethodResolver.java index b2047dd609..047dd4d3b1 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMethodResolver.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMethodResolver.java @@ -15,7 +15,6 @@ */ package com.baomidou.mybatisplus.core; -import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder; import org.apache.ibatis.builder.annotation.MethodResolver; import java.lang.reflect.Method; @@ -31,9 +30,9 @@ public class MybatisMethodResolver extends MethodResolver { private final MybatisMapperAnnotationBuilder annotationBuilder; private final Method method; - public MybatisMethodResolver(MapperAnnotationBuilder annotationBuilder, Method method) { + public MybatisMethodResolver(MybatisMapperAnnotationBuilder annotationBuilder, Method method) { super(annotationBuilder, method); - this.annotationBuilder = (MybatisMapperAnnotationBuilder) annotationBuilder; + this.annotationBuilder = annotationBuilder; this.method = method; } diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java index 29ff78f5c2..ffdfd7b074 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java @@ -45,6 +45,7 @@ * @author hubin * @since 2018-04-06 */ +@SuppressWarnings("serial") public abstract class AbstractMethod implements Constants { protected static final Log logger = LogFactory.getLog(AbstractMethod.class); diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java index 304510c4d3..c440ee2130 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/Insert.java @@ -34,7 +34,7 @@ * @author hubin * @since 2018-04-06 */ -@SuppressWarnings("all") +@SuppressWarnings("serial") public class Insert extends AbstractMethod { @Override diff --git a/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilderTest.java b/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilderTest.java new file mode 100644 index 0000000000..3e5c670506 --- /dev/null +++ b/mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilderTest.java @@ -0,0 +1,46 @@ +package com.baomidou.mybatisplus.core; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.apache.ibatis.annotations.CacheNamespace; +import org.apache.ibatis.annotations.CacheNamespaceRef; +import org.junit.jupiter.api.Test; + +/** + * @author miemie + * @since 2020-11-07 + */ +class MybatisMapperAnnotationBuilderTest { + + @Test + void parse() { + MybatisConfiguration configuration = new MybatisConfiguration(); + MybatisMapperAnnotationBuilder a = new MybatisMapperAnnotationBuilder(configuration, AMapper.class); + a.parse(); + MybatisMapperAnnotationBuilder b = new MybatisMapperAnnotationBuilder(configuration, BMapper.class); + b.parse(); + configuration.getMappedStatement(AMapper.class.getName() + ".insert"); + } + + @CacheNamespaceRef(BMapper.class) + interface AMapper extends BaseMapper { + + } + + @CacheNamespace + interface BMapper extends BaseMapper { + + } + + @Data + private static class A { + private Long id; + } + + @Data + @EqualsAndHashCode(callSuper = true) + private static class B extends A { + + } +}