yulichang / mybatis-plus-join

支持连表查询的mybatis-plus,mybatis-plus风格的连表操作提供wrapper.leftJoin(),wrapper.rightJoin()等操作
https://yulichang.github.io/mybatis-plus-join-doc/
Apache License 2.0
971 stars 113 forks source link

定义两个Mapper,使用selectJoinList时,报错:mapper not find by class <XXXX> add mapper and extends BaseMapper<T> or MPJBaseMapper<T> #102

Closed bineanfrank closed 2 months ago

bineanfrank commented 4 months ago

当前使用版本

mybatis-plus-join-boot-starter 1.4.7

该问题是如何引起的?

1、定义两个Mapper继承MPJBaseMapper、两个实体类,分别对应两张表 2、将以上定义个mapper打包作为一个starter组件,并使用@Configuration配置,使用@MapperScan扫描 3、该starter组件配置使用spring.factories 4、使用该组件的地方,定义一个类,实现InitializingBean接口,@Autowired引入其中一个mapper,在afterPropertiesSet 中使用该mapper,调用selectJoinList方法

重现步骤

根据上面四个步骤代码如下: 1、定义mapper、实体类

public interface AMapper extends MPJBaseMapper<AEntity> {
    @DS("xxxx")
    @Override
    <DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz,
                                   @Param(Constants.WRAPPER) MPJBaseJoin<AEntity> wrapper);
}
@Mapper
public interface BMapper extends MPJBaseMapper<BEntity> {}

@TableName("table1")
public class AEntity{}

@TableName("table2")
public class BEntity{}

2、定义配置类

@ConditionalOnProperty(prefix = "xxx.xxx", name = "enabled", havingValue = "true", matchIfMissing = true)
@MapperScan(basePackages = {"com.xxx.xxx.mapper"})
public class XXXAutoConfiguration implements Ordered {}

3、spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.xxx.xxx.XXXAutoConfiguration

4、引入该组件后定义类

@Component
public class XTest implements InitializingBean {
    @Autowired
    private AMapper amapper;

    // 此处如果同时引入BMapper就不会报错
    // @Autowired
    // private BMapper bmapper;

    @Override
    public void afterPropertiesSet() throws Exception {
        final List<XXXVO> vos =
                amapper.selectJoinList(XXXVO.class, wrapper);
    }
}

报错信息

Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: mapper not find by class , add mapper and extends BaseMapper or MPJBaseMapper at com.github.yulichang.toolkit.Asserts.hasTable(Asserts.java:14) ~[mybatis-plus-join-core-1.4.7.jar:na] at com.github.yulichang.toolkit.support.ColumnCache.lambda$getListField$1(ColumnCache.java:31) ~[mybatis-plus-join-core-1.4.7.jar:na] at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_202] at com.github.yulichang.toolkit.support.ColumnCache.getListField(ColumnCache.java:29) ~[mybatis-plus-join-core-1.4.7.jar:na] at com.github.yulichang.wrapper.interfaces.Query.selectAll(Query.java:164) ~[mybatis-plus-join-core-1.4.7.jar:na] at com.github.yulichang.wrapper.MPJLambdaWrapper.selectAll(MPJLambdaWrapper.java:194) ~[mybatis-plus-join-core-1.4.7.jar:na]

补充说明

1、调试发现,当调用selectJoinList时,会调用到TableInfoHelper.getTableInfo(Class<?> clazz) 但是其内部缓存 TABLE_INFO_CACHE只有AMapper,并没有BMapper,而从Ioc容器中,BMapper是已经在容器中的。

2、如果不是在afterPropertiesSet使用,而是其他普通不在springboot生命周期函数中,就不会报这个问题,例如在CommandLineRunner接口中

yulichang commented 4 months ago

用1.4.9试一下

bineanfrank commented 4 months ago

用1.4.9试一下

还是不行,依旧会有这个问题