当需要的时候到数据库中查询,
1)首先在总配置文件中加
<settings>
<!-- 启用延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
2) 在相应的配置文件中
<resultMap type="userbean" id="many2oneMap">
<id column="uuid" property="userid"/>
<result column="username" property="username"/>
<association property="dep" javaType="cn.hd.po.Dept" column="did" select="findDeptByDid">
<id column="did" property="did"/>
<result column="depname" property="dname"/>
</association>
</resultMap>
<select id="findDeptByDid" resultType="dept" parameterType="int">
select * from t_dept where did=#{did}
</select>
<select id="queryAll" resultMap="many2oneMap" >
select userid as uuid,username,did
from t_user t1
</select>
优点:需要再到数据库中查询,所以某个角度来说是可以提高效率
缺点:会产生N+1问题