案例:
一的一方:user
多的一方:account
主配置文件的修改
<!--配置参数-->
<settings>
<!--开启Mybatis支持延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"></setting>
</settings>
IuserDao.xml 修改
<!-- 定义User的resultMap-->
<resultMap id="userAccountMapLazy" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- 配置user对象中accounts集合的映射 -->
<collection property="accounts" ofType="account" select="hellomybaits.dao.IAccountDao.findAccountByUid"
column="id"></collection>
</resultMap>
<!-- 查询所有 -->
<select id="findAllLazy" resultMap="userAccountMapLazy">
select * from user
</select>
<select id="findById" resultType="hellomybaits.domain.User">
select * from user where id = #{uid}
</select>
IAccountDao.xml
<resultMap id="accoutMapLazy" type="account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一的关系映射:配置封装user的内容
select属性指定的内容:查询用户的唯一标识:
column属性指定的内容:用户根据id查询时,所需要的参数的值
-->
<association property="user" column="uid" javaType="user"
select="hellomybaits.dao.IUserDao.findById"></association>
</resultMap>
<select id="findAccountByUid" resultType="hellomybaits.domain.Account">
select * from account where uid = #{uid}
</select>