前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Mybatis】常见面试题:处理表与表之间的关系:多对一,一对多

【Mybatis】常见面试题:处理表与表之间的关系:多对一,一对多

作者头像
小尘要自信
发布2023-10-10 15:26:40
1360
发布2023-10-10 15:26:40
举报
文章被收录于专栏:CSDN小尘要自信

表的员工与部门有对应关系,实体类之间也有对应的关系

多对一

在员工实体类中加入实体类部门属性 Dept dept;

查询员工信息以及员工所对应的部门信息

方式一:级联方式处理映射关系

代码语言:javascript
复制
<resultMap id="empAndDeptResultMapOne" type="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>
        <result property="dept.did" column="did"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>
    <!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
    <select id="getEmpAndDeptByEid" resultMap="empAndDeptResultMapOne">
        select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
    </select>

方式二:使用association处理映射关系

association专门处理多对一的映射关系 * property:表示需要处理的多对一关系的属性名 * javaType:表示该属性的类型

代码语言:javascript
复制
<resultMap id="empAndDeptResultMapTwo" type="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>
        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>
    <!--Emp getEmpAndDept(@Param("eid") int eid);-->
    <select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
        select * from t_emp left join t_dept on t_emp.eid=t_dept.did where t_emp.eid=#{eid}
    </select>

方式三:分步查询

通过分步查询(常用):必须在核心配置文件中配置核心配置信息(将下划线映射为驼峰的那个) * 好处: * 可以实现延迟加载,在mybatis中默认是不加载的

核心配置信息:

代码语言:javascript
复制
<settings>
        <!-- 将下划线自动映射为驼峰emp_name对应empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

分为两步

第一步:查询员工信息

select:设置分布查询的sql的唯一标识(namespacesqlID或mapper接口的全类名.方法名 column:设置分步查询的条件 property:处理的实体中的多对一的属性

代码语言:javascript
复制
<resultMap id="empAndDeptByStepResultMap" type="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>
        <!--
        select:设置分布查询的sql的唯一标识(namespacesqlID或mapper接口的全类名.方法名
        column:设置分步查询的条件
        property:处理的实体中的多对一的属性
         -->
        <association property="dept"
                     select="com.li.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"></association>
    </resultMap>
   <!-- Emp getEmpAndDept(@Param("eid") Integer eid);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid=#{eid}
    </select>

第二步:查询部门信息

代码语言:javascript
复制
<!--   Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);  -->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did =#{did}
    </select>

一对多

在部门实体类中加入员工类构成的集合

代码语言:javascript
复制
private List<Emp> emps;

方式一:collection

collection:用来处理一对多的映射关系 property:处理一对多关系的属性 ofType:表示该属性对应的集合中存储的数据的类型

代码语言:javascript
复制
<resultMap id="deptAndEmpResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <!--
         collection:用来处理一对多的映射关系
         property:处理一对多关系的属性
         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>

方式二:分步查询

第一步:查询部门信息

代码语言:javascript
复制
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps"
                    select="com.li.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did">

        </collection>
    </resultMap>
<!--  Dept getDeptAndEmpByStepOne(@Param("did") Integer di);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did=#{did}
    </select>

第二步:查询员工信息

代码语言:javascript
复制
<!-- List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did); -->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where did =#{did}
    </select>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 多对一
    • 方式一:级联方式处理映射关系
      • 方式二:使用association处理映射关系
        • 方式三:分步查询
        • 一对多
          • 方式一:collection
            • 方式二:分步查询
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档