mybatis: org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableExc _

created at 11-24-2021 views: 15

error

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 second-level cache in mybatis, you must serialize the entity class implements Serializable, my mapper file uses tags, and the second-level cache provided by mybatis is used, 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 second-level 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>
created at:11-24-2021
edited at: 11-24-2021: