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

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

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

@TOC

多对一

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

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

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

<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:表示该属性的类型
<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中默认是不加载的

核心配置信息:

<settings>
        <!-- 将下划线自动映射为驼峰emp_name对应empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

分为两步

第一步:查询员工信息
>

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

<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>

第二步:查询部门信息

<!--   Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);  -->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did =#{did}
    </select>

一对多

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

private List<Emp> emps;

方式一:collection

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

<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>

方式二:分步查询

第一步:查询部门信息

<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>

第二步:查询员工信息


<!-- List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did); -->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where did =#{
       did}
    </select>
相关文章
|
7天前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
17 0
|
1月前
|
SQL 缓存 Java
Mybatis面试题
Mybatis面试题
|
3天前
|
Java 数据库连接 mybatis
mybatis的一对多
mybatis的一对多
|
1月前
|
存储 XML Java
mybatis使用内部类处理一对多类型数据2
mybatis使用内部类处理一对多类型数据2
13 0
|
1月前
|
SQL Java 数据库连接
MyBatis常见面试题总结2
MyBatis常见面试题总结
23 0
|
1月前
|
SQL Java 数据库连接
MyBatis常见面试题总结1
MyBatis常见面试题总结
26 0
|
2月前
|
SQL Java 数据库连接
答案很详细的MyBatis面试题(含示例代码)
MyBatis是一种优秀的持久层框架,它是一个轻量级的、优化的、功能强大的Java持久层框架,它的设计理念是贴近SQL、便于使用、高效并且功能丰富。通过MyBatis的使用,开发者能够更加专注于业务逻辑的实现,而不用过多关注底层的数据库操作。MyBatis通过XML或注解的方式配置SQL映射文件,并将Java的POJO(Plain Old Java Object,普通的Java对象)与数据库中的记录进行映射,使得开发人员能够以面向对象的方式来操作数据库,同时兼顾了SQL的灵活性和效率。灵活的SQL映射。
122 0
|
2月前
|
XML SQL Java
mybatis的一对多,多对一,以及多对对的配置和使用
mybatis的一对多,多对一,以及多对对的配置和使用
17 2
|
2月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
78 1
|
12天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
31 4