使用mybatisplus执行批量保存时出现错误:Error: Cannot execute table Method, ClassGenricType not found

一、描述

异常 com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: Cannot execute table Method, ClassGenricType not found 表明 MyBatis-Plus 在尝试执行某个表操作时未能找到泛型类型。这通常是由于以下几个原因之一引起的:

注:一般来说都是泛型类型未正确指定、实体类与Mapper接口不匹配。也要注意调用方式,Mybatis-Plus必须依赖于spring运行,不能直接在main方法中运行

  1. 泛型类型未正确指定:在您的服务实现类中,如果泛型类型没有正确指定,Mybatis-Plus可能无法找到对应的实体类,导致ClassGenricType not found错误。请检查您的服务实现类是否正确继承了ServiceImpl或实现了 IService 并指定了正确的泛型参数。如果没有正确指定泛型类型,会导致此类错误。
  2. 类路径问题:确保所有相关的类和依赖项都已正确添加到类路径中。
  3. 代码逻辑问题:可能存在某些代码逻辑错误,导致 MyBatis-Plus 无法正确解析泛型类型。
  4. 实体类与Mapper接口不匹配:确保您的Mapper接口和实体类是匹配的,并且都指定了正确的泛型类型。如果Mapper接口或服务类没有正确继承或实现,可能会导致这个问题。

  5. Mybatis-Plus配置问题:如果您的项目中使用了多个数据源,但没有使用Mybatis-Plus的多数据源配置,可能会导致这个问题。请检查您的配置是否正确。

  6. TableInfo缓存问题:在某些情况下,如果TableInfoHelper没有正确缓存对应的表信息,也可能会导致这个错误。您可以尝试调试TableInfoHelper#initTableInfo方法,查看是否正确初始化了表信息。

  7. 版本兼容性问题:如果您最近升级了Mybatis-Plus的版本,可能会引入一些兼容性问题。特别是在3.4.0版本之后,saveOrUpdate方法有所修改,可能会导致问题。请检查您的版本是否与Mybatis-Plus的文档兼容。

  8. SQL语句或权限问题:检查是否有SQL语句错误或权限问题导致无法执行操作

  9. 启动方式,调用方式有问题。Mybatis-Plus必须依赖于spring运行,不能直接在main方法中运行

二、解决步骤

1. 检查泛型类型

确保您的 ServiceImplIService 接口正确指定了泛型类型。例如:

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chinaunicom.medical.business.cdm.entity.ResidentRecordDetail;
import com.chinaunicom.medical.business.cdm.mapper.ResidentRecordDetailMapper;

public interface ResidentRecordDetailService extends IService<ResidentRecordDetail> {
}

@Service
public class ResidentRecordDetailServiceImpl extends ServiceImpl<ResidentRecordDetailMapper, ResidentRecordDetail> implements ResidentRecordDetailService {
}
2. 检查实体类

确保您的实体类 ResidentRecordDetail 已经正确定义,并且没有拼写错误或其他问题。

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("resident_record_detail")
public class ResidentRecordDetail {
    @TableId
    private Long id;
    // 其他字段
}
3. 检查 Mapper 接口

确保您的 Mapper 接口也正确指定了泛型类型。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaunicom.medical.business.cdm.entity.ResidentRecordDetail;

public interface ResidentRecordDetailMapper extends BaseMapper<ResidentRecordDetail> {
}
4. 检查依赖项

确保您的项目中已经正确引入了 MyBatis-Plus 的依赖项。在 pom.xml 中添加以下依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version> <!-- 请使用最新版本 -->
</dependency>
5. 检查代码逻辑

确保在调用 saveBatch 方法时传递的参数是正确的。例如:

import com.baomidou.mybatisplus.extension.service.IService;
import com.chinaunicom.medical.business.cdm.entity.ResidentRecordDetail;
import com.chinaunicom.medical.business.cdm.service.ResidentRecordDetailService;

@Service
public class ResidentRecordDetailExcelAnalysis {

    @Autowired
    private ResidentRecordDetailService residentRecordDetailService;

    public void analysis() {
        List<ResidentRecordDetail> details = new ArrayList<>();
        // 填充 details 列表
        residentRecordDetailService.saveBatch(details);
    }
}

三、调试建议

  1. 日志输出:增加日志输出,查看在调用 saveBatch 方法前后的日志信息,确保传递的参数是正确的。
  2. 单元测试:编写单元测试,单独测试 ResidentRecordDetailService 的 saveBatch 方法,确保其能够正常工作。
  3. 示例代码

以下是一个完整的示例,展示了如何正确配置和使用 MyBatis-Plus:

// 实体类
@TableName("resident_record_detail")
public class ResidentRecordDetail {
    @TableId
    private Long id;
    // 其他字段
}

// Mapper 接口
public interface ResidentRecordDetailMapper extends BaseMapper<ResidentRecordDetail> {
}

// Service 接口
public interface ResidentRecordDetailService extends IService<ResidentRecordDetail> {
}

// Service 实现类
@Service
public class ResidentRecordDetailServiceImpl extends ServiceImpl<ResidentRecordDetailMapper, ResidentRecordDetail> implements ResidentRecordDetailService {
}

// 分析类
@Service
public class ResidentRecordDetailExcelAnalysis {

    @Autowired
    private ResidentRecordDetailService residentRecordDetailService;

    public void analysis() {
        List<ResidentRecordDetail> details = new ArrayList<>();
        // 填充 details 列表
        residentRecordDetailService.saveBatch(details);
    }
}

猜你喜欢

转载自blog.csdn.net/ZhShH0413/article/details/143472258