程序员的资源宝库

网站首页 > gitee 正文

【问题记录】【Mybatis-Plus】saveBatch MybatisPlusException: Error: Cannot execute table Method, ClassGenricType not found

sanyeah 2024-04-02 19:56:29 gitee 5 ℃ 0 评论

1  问题描述

MyBatis-Plus版本

<mybatis-plus-boot-starter.version>3.3.0</mybatis-plus-boot-starter.version>

由于Mybatis-Plus本身已经有增删改查以及批量保存的基本接口了,所以我们没必要新建一张表就重复写一套自己的增删改查,我们在公共层抽一个BaseService接口(继承MyBatis-Plus的IService)以及BaseServiceImpl实现类(实现ServiceImpl类)。

/**
 * @description
 * 基础接口层
 */
public interface BaseService<P> extends IService<P> {
}
/**
 * @description
 * 基础接口实现层
 */
public class BaseServiceImpl<M extends BaseMapper<P>, P> extends ServiceImpl<M, P> implements BaseService<P> {

    public BaseServiceImpl() {
    }

    public BaseServiceImpl(M baseMapper) {
        this.baseMapper = baseMapper;
    }
}

这样我基础层就可以这样引用进来,全部都调上边的基础实现即可。

/**
 * @description
 */
@Component
public class DeptRepositoryImpl implements DeptRepository {

    private BaseService<DeptPo> deptPoBaseService;
    private DeptMapper deptMapper;
    public DeptRepositoryImpl(DeptMapper deptMapper) {
        this.deptPoBaseService = new BaseServiceImpl<>(deptMapper);
        this.deptMapper = deptMapper;
    }
    @Override
    public DeptPo addOne(DeptPo po) {
        deptPoBaseService.saveBatch(Lists.newArrayList(po));
        return po;
    }
}

但是批量新增的方法报错,saveBatch报错,

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: Cannot execute table Method, ClassGenricType not found .
    at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
    at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38)
    at com.baomidou.mybatisplus.core.toolkit.Assert.notNull(Assert.java:72)

大概看了看别人的解决办法,有的说的Mapper没有继承BaseMapper有的说是ServiceImpl没有写Mapper,可是我的都有还是报错。

2  解决办法

(1)换版本,我换成了3.3.1的版本,这个版本ServiceImpl中多了一个entityClass,这样MP就不会调用自己的方法去找类了。

 (2)改一下自己的基础实现类的入参

(3)改一下使用的地方,完事

 

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表