一对一
关联的嵌套结果映射(将多个表所有需要的值一次性查出)
public class SysUser{
private SysRole role
}
标签用于表示一个内嵌复合对象属性
<select id="selectUserAndRole" resultType="SysUser">
select u.id, u.user_name userName, u.user_password userPassword,
r.id "role.id", r.role_name "role.roleName"
from sys_user u
inner join sys_user_role ur on u.id = ur.user_id
inner join sys_role r on ur.role_id = r.id
where u.id = #{id}
</select>
映射方式
- 自动映射使用resultType,role对象映射的列需要添加“role.”前缀
- 使用resultMap,定制化属性映射,
- 使用resultMap, 同事使用association标签
嵌套查询
<association select="selectid">
<select id="selectid">
aggressiveLazyLoading为true时,延迟加载fetchType=lazy失效
lazyLoadTriggerMethods,配置主对象调用相关的方法则加载延迟加载的对象(调用user.equals()时是加载Role对象)
一对多
resultMap标签中,标签表明是否同一个对象。
<resultMap>
<id property="" column=""/>
</resultMap>