mybatis-config.xml设置
<!--开启全局延迟加载开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--指定哪些方法触发延迟加载,默认:equals,clone,hashCode,toString-->
<setting name="lazyLoadTriggerMethods" value=""/>
Mapper.xml文件
<?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.harbin.dao.UserMapper">
<resultMap id="UserResultMap" type="user">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<!--一对多关系:column是slect中方法查找条件的参数-->
<collection property="cars" ofType="car" column="id" select="com.harbin.dao.CarMapper.findByUId"></collection>
</resultMap>
<!--List<User> find()-->
<select id="find" resultMap="UserResultMap">
select * from user
</select>
</mapper>
<?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.harbin.dao.CarMapper">
<!--List<Car> findByUId(Integer uId)-->
<select id="findByUId" resultType="car">
select * from car where uId = #{uId}
</select>
</mapper>
结果
二级缓存