Tag Archives: org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableExc

[Solved] Mybatis Error: org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableExc

org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableException: com.baizhi.entity.User
at org.apache.ibatis.cache.decorators.SerializedCache.serialize(SerializedCache.java:100)
atorg.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.io.NotSerializableException: com.baizhi.entity.User
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
org.apache.ibatis.cache.decorators.SerializedCache.serialize(SerializedCache.java:96)
… 47 more

Reason: when using the L2 cache in mybatis, the entity class must be serialized. Implements serializable. My mapper file uses tags and the L2 cache provided by mybatis, so it must be serialized in my entity class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.baizhi.dao.UserDao">
     <!-- The L2 cache is used here The entity class must implement the serialization interface -->
    <cache/>  
    <select id="selectAllUsers" resultType="User">
        select id,
               phone,
               password,
                name,
        from c_user
    </select>
</mapper>