处理一对多的映射关系

一对多关系,比如说根据id查询一个部门的部门信息及部门下的员工信息

在Dept类中先添加List emps属性
image.png

1、collection
DeptMapper.xml文件中

<resultMap id="deptAndEmpResultMap" type="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>

        <!--
            collection:处理一对多的映射关系
            ofType: 标识该属性所对应的集合中存储数据的类型
         -->
            <collection property="emps" ofType="Emp">
                <id property="eid" column="eid"></id>
                <result property="empName" column="emp_name"></result>
                <result property="age" column="age"></result>
                <result property="sex" column="sex"></result>
                <result property="email" column="email"></result>
            </collection>
    </resultMap>
    <!-- Dept getDeptAndEmp(@Param("did") Integer did);-->
    <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
       select * from t_dept left join t_emp on t_dept.did=t_emp.did where t_dept.did=#{did}
    </select>

2、分布查询
DeptMapper.xml文件中:

 <resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps"
                    select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did"
                    fetchType="eager"></collection>
    </resultMap>
    <!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did=#{did}
    </select>

EmpMapper.xml文件中:

 <!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where did=#{did}
    </select>

相关推荐

  1. MyBatis 自定义映射 ResultMap:映射关系处理

    2024-06-06 16:44:06       30 阅读
  2. MyBatis笔记——映射问题解决

    2024-06-06 16:44:06       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 16:44:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 16:44:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 16:44:06       82 阅读
  4. Python语言-面向对象

    2024-06-06 16:44:06       91 阅读

热门阅读

  1. 创建邮件系统的一个思路

    2024-06-06 16:44:06       30 阅读
  2. js 数组过滤删除空对象

    2024-06-06 16:44:06       25 阅读
  3. 基于R语言的糖尿病检测模型准确率97%

    2024-06-06 16:44:06       28 阅读
  4. 【杂记-IDS入侵检测系统、IPS入侵防御系统】

    2024-06-06 16:44:06       34 阅读
  5. Android 架构组件面试问答

    2024-06-06 16:44:06       19 阅读
  6. ubuntu22.04 安装mongodb的管理工具

    2024-06-06 16:44:06       30 阅读
  7. 2024-05-29 架构-程序设计-思考

    2024-06-06 16:44:06       32 阅读
  8. 2024/6/6随笔

    2024-06-06 16:44:06       26 阅读