java.lang.RuntimeException: Cannot instantiate object of type tk.mybatis.mapper.generator.MapperPlug

本文介绍了解决MyBatis Plus在Spring Boot项目中出现的配置错误的方法,通过在pom.xml中添加特定依赖解决了运行时报错的问题,并列出了项目中使用的其他关键Jar包版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、出现这个错误时

二、解决方法如下

在pom.xml中添加如下配制即可解决

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.0.3</version>
        </dependency>

在次运行不在报错

三、我项目中使用的其他几个jar包的版本

<!-- spring boot集成mybatis start -->
        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!--mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
        <!--pagehelper 分页 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

### Spring BootMyBatis Plus SqlSessionFactory 创建失败的原因分析 在 Spring Boot 集成 MyBatis Plus 的场景下,`SqlSessionFactory` Bean 创建失败通常是由 `ProductDocMapper.xml` 文件解析错误引起的。这种问题的根本原因可能涉及以下几个方面: #### 1. **XML 文件语法错误** 如果 `ProductDocMapper.xml` 文件存在语法错误(例如未闭合的标签、非法字符等),MyBatis 将无法正确解析该文件并抛出异常。此类问题可以通过检查 XML 文件的具体位置来定位。例如,在引用中提到的一个典型问题是第 31 行、第 14 列处的内容不符合 XML 格式标准[^1]。 #### 2. **重复定义的结果映射 (Result Map)** 当同一个 `Result Map` ID 在多个地方被重复定义时,也会引发类似的错误。这种情况可能是由于手动编辑 XML 或者使用工具(如 MyBatis Generator)多次生成相同内容所致[^4]。具体表现为如下错误信息: ```plaintext Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for ... ``` 解决方案是对 `ProductDocMapper.xml` 进行清理,移除多余的 `<resultMap>` 定义或者调整其唯一性标识符。 #### 3. **路径配置不匹配** 如果 `mybatis-config.xml` 或其他全局配置文件中的资源扫描路径设置不当,则可能导致某些 Mapper 文件未能成功加载或解析。这会进一步影响到 `SqlSessionFactory` 的初始化过程。因此需确认以下几点: - 是否已在 `application.properties` 或 `application.yml` 中指定了正确的 mapper 扫描目录; - 如果采用的是基于注解的方式开发,则应确保对应的包名已加入组件扫描范围。 #### 4. **依赖冲突** 有时第三方库版本之间的差异也可能造成兼容性问题,进而干扰正常运行流程。比如不同版本间的 API 变更可能会破坏原有逻辑结构。故建议核查当前项目的 Maven/Gradle 构建脚本是否存在潜在矛盾之处,并尝试统一各模块所使用的框架版本号。 以下是针对此情况的一段代码片段用于验证和修正上述可能性之一——即处理因重复 resultMap 导致的问题: ```java // 清理多余 resultMaps 方法示例 public void cleanDuplicateResultMaps(String filePath){ try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File(filePath)); NodeList resultList = doc.getElementsByTagName("resultMap"); Set<String> uniqueIds = new HashSet<>(); List<Node> nodesToRemove = new ArrayList<>(); for(int i=0;i<resultList.getLength();i++) { Node node = resultList.item(i); String idAttrValue = ((Element)node).getAttribute("id"); if(!uniqueIds.add(idAttrValue)){ nodesToRemove.add(node); // 记录需要删除的节点 } } for(Node n : nodesToRemove){ n.getParentNode().removeChild(n); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filePath)); transformer.transform(source, result); } catch(Exception e){ throw new RuntimeException(e.getMessage(),e); } } ``` 通过执行以上函数可以自动去除掉那些不必要的重复项从而改善整体性能表现同时减少发生意外状况的概率。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值