问题:在mybatis里面,我们可以使用延迟加载来提高性能,同时减少代码量(只用写查询延时属性的statement语句的id可以),但是这有可能造成循环。比如学生(对应实体类Student)和成绩(对应实体类Score),一个学生有多个成绩,那么在学生实体类Student里面有一个scoreSet的集合,然后在成绩实体类Score里面有一个学生对象Student。
学生类:
public class Student {
//省略了其他属性和方法
private String id;
private String name;
private Set<Score> scoreSet=new HashSet<Score>();
}
成绩类:
public class Score {
//省略了其他属性和方法
private Integer id;//id,唯一标识
private Float score1;//一考成绩
private Student student;//学生类
}
然后在StudentMapper.xml里面写一个StudentResultMap
<resultMap type="com.zxs666.model.Student" id="studentResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!-- 延迟加载scoreSet 这个select属性就是延迟该学生对应的scoreSet的statementId-->
<collection property="scoreSet" ofType="com.zxs666.model.Score" column="id"
select="com.zxs666.mapper.ScoreMapper.queryByStudentId" />
</resultMap>
在ScoreMapper.xml里面写一个ScoreResultMap
<resultMap type="com.zxs666.model.Score" id="scoreResultMap">
<id column="id" property="id"/>
<result column="score1" property="score1"/>
<!--延迟加载student 这个select属性就是延迟加载student属性的statementId-->
<association property="student" column="studentId"
javaType="com.zxs666.model.Student"
select="com.zxs666.mapper.StudentMapper.queryById" ></association>
</resultMap>
贴上两个查询语句的Id:
StudentMapper.xml根据id唯一查询学生:
<select id="queryById" parameterType="string" resultMap="studentResultMap">
ScoreMapper.xml根据学生的id查询他的所有成绩:
<select id="queryByStudentId" parameterType="string" resultMap="scoreResultMap">
现在有一个情况:假设我先查询一个学生(第一次查询),通过StudentMapper.xml里面的queryById这个statement查询,这时候到数据库查询出来的数据,里面的scoreSet是没有值的,当我需要用到scoreSet的时候才会延迟去加载它,问题来了:如果我延迟加载了scoreSet(第二次查询),然后我再去访问scoreSet里面的每一个元素(Score类对象)的student属性,那么这时候将会调用scoreResultMap里面的select属性作为statementId去查询(第三次查询),注意:这里第一次和第三次查询语句的statementId是相同的,所以其实查询的是同一个对象,这个时候mybatis的一级缓存就会返回第一次查询的那个学生对象,那么这样就出现了一个有点像循环的东西,我查询一个学生,然后通过他的属性延迟加载去查询又查到了他本身,这时候就抛出了了一个异常
Exception in thread "main" java.lang.ClassCastException: org.apache.ibatis.executor.ExecutionPlaceholder cannot be cast to java.util.List
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:152)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.loader.ResultLoader.selectList(ResultLoader.java:81)
at org.apache.ibatis.executor.loader.ResultLoader.loadResult(ResultLoader.java:70)
at org.apache.ibatis.executor.loader.ResultLoaderMap$LoadPair.load(ResultLoaderMap.java:219)
at org.apache.ibatis.executor.loader.ResultLoaderMap$LoadPair.load(ResultLoaderMap.java:184)
at org.apache.ibatis.executor.loader.ResultLoaderMap.load(ResultLoaderMap.java:81)
at org.apache.ibatis.executor.loader.ResultLoaderMap.loadAll(ResultLoaderMap.java:95)
at org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl.invoke(JavassistProxyFactory.java:151)
解决办法:在查询学生的那个statement上加一个属性,flushCache=“true”,执行之后,刷新缓存,这样第三次查询的时候缓存里面没有数据,就会重新去数据库查询一个新的student对象。
<select id="queryById" parameterType="string" resultMap="studentResultMap" flushCache="true">