baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.3k stars 4.3k forks source link

Mybatis-Plus3.5.5 版本在扩展DefaultSqlInjector时,无法调用扩展的Mapper方法 #6008

Closed chaobingliu closed 6 months ago

chaobingliu commented 6 months ago

当前使用版本(必填,否则不予处理)

Mybatis-Plus3.5.5

该问题是如何引起的?(确定最新版也有问题再提!!!)

Mybatis-Plus3.5.5 版本在扩展DefaultSqlInjector时,无法调用扩展的Mapper方法

重现步骤(如果有就写完整)

1、定义扩展类 @Component public class InsertBatchSqlInjector extends DefaultSqlInjector {

@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
    // super.getMethodList() 保留 Mybatis Plus 自带的方法
    List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
    // 添加自定义方法:批量插入,方法名为 insertBatchSomeColumn
    methodList.add(new InsertBatchSomeColumn());
    return methodList;
}

}

2、定义Mapper public interface MyBaseMapper extends BaseMapper {

// 批量插入
int insertBatchSomeColumn(Collection<T> batchList);

}

3、继承MyBaseMapper public interface DemoMapper extends MyBaseMapper {

}

4、设置sqlInjector @Override public void run(String... args) { Configuration configuration = sessionFactory.getConfiguration(); GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration); globalConfig.setSqlInjector(insertBatchSqlInjector); }

5、调用insertBatchSomeColumn方法 demoMapper.insertBatchSomeColumn(list))

报错信息

在调用demoMapper.insertBatchSomeColumn(list))时,报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):demo.mapper.DemoMapper.insertBatchSomeColumn

at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:229) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.(MybatisMapperMethod.java:50) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:111) at com.baomidou.mybatisplus.core.toolkit.CollectionUtils.computeIfAbsent(CollectionUtils.java:143) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:98) at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) at com.sun.proxy.$Proxy332.insertBatchSomeColumn(Unknown Source)

miemieYaho commented 6 months ago

你那个步骤4是个啥?画蛇添足是吧

chaobingliu commented 6 months ago

老师,这是因为步骤一定义的组件没有生效。 我是手工将自定义的SqlInjector设置到globalConfig中。但依然没有生效。

miemieYaho commented 6 months ago

https://gitee.com/baomidou/mybatis-plus-samples/tree/master/mybatis-plus-sample-sql-injector