Mybatis 出现org.apache.ibatis.io不存在,org.apache.ibatis.session不存在等错误

解决IDEA项目构建错误
本文提供了一种解决在IntelliJ IDEA中遇到项目构建错误的方法。通过一系列步骤,包括重启后使用Terminal执行特定的Maven命令,如idea:idea、clean和test,通常可以解决构建问题。如果在执行命令过程中遇到错误,建议创建新项目并复制原有文件再次尝试。

这是我遇到的

解决方法:

第一步

在这里插入图片描述

第二步:点击蓝色的选项

在这里插入图片描述

第三步:重启后,打开下面的Terminal

  • 分别输入以下命令:
  • mvn idea:idea
  • mvn clean
  • mvn test
  • 执行完上述命令后一般就会解决问题
  • 注意:在上述命令中如果有某个命令在执行过程中,出现错误,请新建一个项目,将原有文件复制过去再执行上述命令
    在这里插入图片描述
    在这里插入图片描述
java.lang.ExceptionInInitializerError at com.hyh.test.JTest.setUpBeforeClass(JTest.java:41) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in com/hyh/dao/UserMapper.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/hyh/dao/UserMapper.xml'. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'. Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:82) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:66) at com.hyh.utils.MybatisUtil.<clinit>(MybatisUtil.java:19) ... 3 more Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/hyh/dao/UserMapper.xml'. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'. Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:133) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:110) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80) ... 5 more Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/hyh/dao/UserMapper.xml'. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'. Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:125) at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:98) at org.apache.ibatis.builder.xml.XMLConfigBuilder.mappersElement(XMLConfigBuilder.java:405) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:131) ... 7 more Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'org.mybatis.caches.ehcache.EhcacheCache'. Cause: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:128) at org.apache.ibatis.builder.xml.XMLMapperBuilder.cacheElement(XMLMapperBuilder.java:164) at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119) ... 10 more Caused by: java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:226) at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:103) at org.apache.ibatis.io.Resources.classForName(Resources.java:322) at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:124) ... 12 more 什么问题
最新发布
05-21
根据您提供的错误信息,问题的核心是 `java.lang.ClassNotFoundException: Cannot find class: org.mybatis.caches.ehcache.EhcacheCache`。这表明 MyBatis 在解析 `UserMapper.xml` 文件时,无法找到指定的缓存类 `org.mybatis.caches.ehcache.EhcacheCache`。 以下是详细的分析和解决方案。 --- ### 错误原因分析 1. **缺少依赖库**: - `org.mybatis.caches.ehcache.EhcacheCache` 是 MyBatis 提供的一个 Ehcache 缓存实现类。如果项目中没有引入 Ehcache 的相关依赖库,就会导致 `ClassNotFoundException`。 2. **MyBatis 配置文件问题**: - 在 MyBatis 的配置文件(如 `mybatis-config.xml` 或 `UserMapper.xml`)中,可能指定了使用 Ehcache 缓存,但未正确配置或引入相关依赖。 3. **版本匹配**: - 如果使用的 MyBatis 版本与 Ehcache 版本兼容,也可能导致此类问题。 --- ### 解决方案 #### 1. 引入 Ehcache 依赖 确保在项目的 `pom.xml` 文件中引入了 Ehcache 的依赖。例如: ```xml <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.9.4</version> <!-- 确保版本与 MyBatis 兼容 --> </dependency> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.2.1</version> <!-- 确保版本与 MyBatis 兼容 --> </dependency> ``` #### 2. 检查 MyBatis 配置文件 确保 MyBatis 的配置文件中正确配置了缓存。例如,在 `mybatis-config.xml` 中添加以下内容: ```xml <configuration> <settings> <setting name="cacheEnabled" value="true"/> <!-- 启用全局缓存 --> </settings> <typeAliases> <typeAlias type="org.mybatis.caches.ehcache.EhcacheCache" alias="EhcacheCache"/> </typeAliases> <caches> <cache type="EhcacheCache"/> </caches> <mappers> <mapper resource="com/hyh/dao/UserMapper.xml"/> </mappers> </configuration> ``` #### 3. 检查 Mapper 文件 确保 `UserMapper.xml` 文件中正确配置了缓存。例如: ```xml <mapper namespace="com.hyh.dao.UserMapper"> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> <select id="selectUser" resultType="User"> SELECT * FROM users WHERE id = #{id} </select> </mapper> ``` #### 4. 检查 MyBatis 和 Ehcache 的版本兼容性 确保 MyBatis 和 Ehcache 的版本兼容。以下是常见的兼容版本组合: - MyBatis 3.5.x + Ehcache 3.x - MyBatis 3.4.x + Ehcache 2.x 如果版本匹配,请调整依赖版本。 --- ### 示例代码 以下是一个完整的 MyBatis 配置示例: #### `pom.xml` ```xml <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.9.4</version> </dependency> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.2.1</version> </dependency> </dependencies> ``` #### `mybatis-config.xml` ```xml <configuration> <settings> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <typeAlias type="org.mybatis.caches.ehcache.EhcacheCache" alias="EhcacheCache"/> </typeAliases> <caches> <cache type="EhcacheCache"/> </caches> <mappers> <mapper resource="com/hyh/dao/UserMapper.xml"/> </mappers> </configuration> ``` #### `UserMapper.xml` ```xml <mapper namespace="com.hyh.dao.UserMapper"> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> <select id="selectUser" resultType="User"> SELECT * FROM users WHERE id = #{id} </select> </mapper> ``` --- ### 解释 1. **引入 Ehcache 依赖**: - `ehcache` 和 `mybatis-ehcache` 是实现 MyBatis 缓存所必需的依赖库。 2. **配置缓存**: - 在 `mybatis-config.xml` 中启用全局缓存,并注册 Ehcache 缓存类型。 - 在 `UserMapper.xml` 中为每个 Mapper 配置缓存。 3. **版本兼容性**: - 确保 MyBatis 和 Ehcache 的版本兼容,以避免因版本匹配导致的问题。 --- ###
评论 21
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Style_OvO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值