关于MyBatis二级缓存问题:org.apache.ibatis.cache.CacheException: Error serializing object.

本文介绍如何在MyBatis中正确配置和使用二级缓存。包括在映射文件中使用<cache>元素开启二级缓存的方法,以及在mybatis-config.xml中全局开启二级缓存的步骤。

错误描述:

在使用二级缓存时,报的错误,如图所示:

错误解决:

mapper在映射文件中,使用<cache>元素开启当前 mapper 的namespace 下的二级缓存,如下所示:

<cache eviction="LRU" flushInterval="30000" size="512" readOnly="true"/>

这样,mapper.xml下的SQL语句执行结束后,会将结果存储到它的二级缓存中。<cache>元素配置在<mapper>元素内,<cache>元素的属性含义如下:

属性描述
flushInterval 属性表示刷新间隔,可以设置为任意正整数,单位是毫秒。默认不设置,表示没有刷新间隔,仅仅调用语句时刷新缓存。
size 属性表示引用数目,可以被设置为任意正整数,默认值是1024。
readOnly 属性设置是否只读,true 表示只读,false 表示可读写。只读的缓存会给所有调用者返回缓存对象的相同实例,因此这些对象不能被修改,这提供了重要的性能优势。可读写的缓存会返回缓存对象的拷贝,这会慢一些,但是安全,因此默认值是false。
eviction 属性表示收回策略,有 LRU、 FIFO 、SOFT 、WEAK 等策略,默认值为LRU。LRU 表示最近最少使用策略,即移除最近最少使用的对象。FIFO表示先进先出策略,按对象进入缓存的顺序来移除。SOFT 表示软引用策略,移除基于垃圾回收器状态和软引用规则的对象。WEAK 表示弱引用策略,更积极地移除基于垃圾收集器状态和弱引用的规则的对象。

同时也要具备一个大前提,MyBatis 默认是没有开启二级缓存,需要在 MyBatis 的配置文件 mybatis-config.xml 中开启二级缓存,配置如下:

<!-- 开启二级缓存 -->
<settings>
    <setting name="cacheEnabled" value="true"/>
</settings>

需要注意的是,settings 元素要放在 properties 元素之后,typeAliases 元素之前,否则配置文件会报错。

再次执行测试类:

问题解决!

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 的版本兼容,以避免因版本不匹配导致的问题。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

We Never say die

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

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

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

打赏作者

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

抵扣说明:

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

余额充值