resultMap自定义映射(多对一)

自定义resultMap,处理复杂的表关系,实现高级结果集映射
  1) id :用于完成主键值的映射
  2) result :用于完成普通列的映射
  3) association :一个复杂的类型关联;许多结果将包成这种类型
  4) collection : 复杂类型的集

1、多对一的查询,员工与员工部门:

  1)直接通过resultMap进行处理:

复制代码
    <!-- 
        <resultMap>:自定义映射,处理复杂的表关系
        <id column="eid" property="eid"/>
        <id>:设置主键的映射关系,column设置字段名,property设置属性名
        <result column="ename" property="ename"/>
        <result>:设置非主键的映射关系,column设置字段名,property设置属性名
     -->
    <resultMap type="Emp" id="empMap">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="did" property="dept.did"/>
        <result column="dname" property="dept.dname"/>
    </resultMap>
    

    <!-- List<Emp> getAllEmp(); -->
    <select id="getAllEmp" resultMap="empMap">
        select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did = d.did
    </select>     
复制代码

  2)POJO中的属性可能会是一个对象,我们可以使用联合查询,并以级联属性的方式封装对象.使用association标签定义对象的封装规则

复制代码
public class Dept {
    private Integer did;
    private String dname;
    // 省略 get/set方法
}
public class Emp {
    private Integer eid;
    private String ename;
    private Integer age;
    private String sex;
    private Dept dept;
    // 省略 get/set方法
}
复制代码

查询某一员工的所有信息,包括其的部门信息,涉及到两表联查:

复制代码
   <resultMap type="Emp" id="empMap">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <association property="dept" javaType="Dept">
            <id column="did" property="did"/>
            <result column="dname" property="dname"/>
        </association>
    </resultMap>
    
    <!-- List<Emp> getAllEmp(); -->
    <select id="getAllEmp" resultMap="empMap">
        select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did = d.did
    </select>
复制代码

  3)association 分步查询
    实际的开发中,对于每个实体类都应该有具体的增删改查方法,也就是DAO层, 因此对于查询员工信息并且将对应的部门信息也查询出来的需求,就可以通过分步的方式完成查询。
    ① 先通过员工的id查询员工信息
    ② 再通过查询出来的员工信息中的外键(部门id)查询对应的部门信息.

复制代码
<!-- 
        <resultMap>:自定义映射,处理复杂的表关系
     -->
    <resultMap type="Emp" id="empMapStep">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <!-- 
            select:分步查询的SQL的id,即接口的全限定名.方法名或namespace.SQL的id
            column:分步查询的条件,注意:此条件必须是从数据库查询过得
         -->
        <association property="dept" select="com.atguigu.mapper.DeptMapper.getDeptByDid" column="did"/>
    </resultMap>
    
    <!-- Emp getEmpStep(String eid); -->
    <select id="getEmpStep" resultMap="empMapStep">
        select eid,ename,age,sex,did from emp where eid = #{eid}
    </select>
复制代码
<mapper namespace="com.atguigu.mapper.DeptMapper">
    
    <!-- Dept getDeptByDid(String did); -->
    <select id="getDeptByDid" resultType="Dept">
        select did,dname from dept where did = #{did}
    </select>
    
</mapper>

    ③association 分步查询使用延迟加载:在分步查询的基础上,可以使用延迟加载来提升查询的效率,只需要在全局的Settings中进行如下的配置:

    <settings>
        <!-- 开启延迟加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 是否查询所有数据 -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

 

posted @   kkzhang  阅读(783)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示