mybatis------处理多对一映射关系的两种方式、多对一处理映射关系的两种方式

员工类:

package com.gothic.sunset.pojo;

public class Emp {
    private Integer empId;//员工号

    private String empName;//员工姓名

    private Integer age;//员工年龄

    private String gender;//员工性别

    private Dept dept;//部门

    public Emp() {
    }

    public Emp(Integer empId, String empName, Integer age, String gender) {
        this.empId = empId;
        this.empName = empName;
        this.age = age;
        this.gender = gender;
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", dept=" + dept +
                '}';
    }
}


部门类

package com.gothic.sunset.pojo;

import java.util.List;

public class Dept {
    private Integer deptId;//部门id号

    private String deptName;//部门名称

    private Emp emps;//每个部门中的所有员工

    public Dept() {
    }

    public Dept(Integer deptId, String deptName) {
        this.deptId = deptId;
        this.deptName = deptName;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public List<Emp> getEmps() {
        return emps;
    }

    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptId=" + deptId +
                ", deptName='" + deptName + '\'' +
                ", emps=" + emps +
                '}';
    }
}


处理多对一映射关系的两种方式

1、级联属性赋值方式—resultMap 标签

需求:根据员工id,查询员工的所有信息(包括他的基本信息和对应的部门信息)。
首先,在mapper接口中编写对应的方法:
/**
* 根据id查询员工信息
* @param empId
* @return
*/
Emp getEmpByEmpId(@Param(“empId”) Integer empId);

映射文件xml中写sql语句:

<resultMap id="getEmpAllByEmpId" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <result column="dept_id" property="dept.deptId"></result>
    <result column="dept_name" property="dept.deptName"></result>
</resultMap>
<!-- Emp getEmpByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpByEmpId" resultMap="getEmpAllByEmpId">
    select
        t_emp.*,t_dept.*
    from t_emp
             inner join t_dept
                       on t_emp.dept_id = t_dept.dept_id
    where t_emp.emp_id = #{empId}
</select>
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>

首先通过resultMap标签中的id主键标签将emp实体类中的属性id和emp表中的字段保持一致,其次用result普通标签将emp实体类属性和emp表字段保持一致,因为emp实体类中涉及到demp部门表的对应关系,也要进行处理emp实体类中private Dept dept这个属性字段的转化,从而获取到dept表中相应的字段,利用联机
方式,先dept.deptId得到dept实体类中的属性字段deptId然后利用标签转化将dept实体类中deptId属性和dept表中dept_id字段保持一致即可,

使用resultMap中的association标签

association:处理多对一的映射关系。
association标签的结构:

property:需要处理多对一的映射关系的属性名
javaType:该属性的类型

 <association property="" javaType="" ......>
            <id property="" column=""></id>
            <result property="" column=""></result>
        </association>

映射xml中对应sql:

	<resultMap id="getEmpAllByEmpIdTwo" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <association property="dept" javaType="Dept" >
            <id  column="dept_id" property="deptId"></id>
            <result  column="dept_name" property="deptName"></result>
        </association>
    </resultMap>
    <!--Emp getEmpByEmpIdTwo(("empId") Integer empId);-->
    <select id="getEmpByEmpIdTwo" resultMap="getEmpAllByEmpIdTwo">
        select
            t_emp.*,t_dept.*
        from t_emp
                 inner join t_dept
                            on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>


emp实体类中有private Dept dept;部门属性,通过association标签中property属性名找到该属性的javaType类型:property=“dept” javaType=“Dept”,然后利用 :

<id  column="dept_id" property="deptId"></id>
<result  column="dept_name" property="deptName"></result>

主键标签和普通标签将javaType类型的属性和表的字段保持一致即可,

多对一处理映射关系的两种方式

collection 标签

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<!--namespace:名称空间:接口的具体位置-->
<mapper namespace="com.itheim.mapper.DeptMapper">
    <!-- Dept getDeptAndEmpById(@Param("bumenTableid") Integer bumenTableid);-->
    <!--左外连接和右外连接的区别:
    第一:关键字不同
    第二:左外连接以左边为主表,右表为辅助表
    第三:右外连接是以右表为主表左表为辅助表-->
    <resultMap id="getDeptAndEmpById" type="Dept">
        <id property="bumenTableid" column=""></id>
        <result property="bumenTablename" column="bumenTablename"></result>
        <result property="bumenTableaddress" column="bumenTableaddress"></result>
        <!--oftype:集合中的类型-->
        <!--一对多关系利用collection来处理-->
        <collection property="emps" ofType="Emp">
            <id property="id" column="id"></id>
            <result property="yuangongname" column="yuangongname"></result>
            <result property="word" column="word"></result>
            <result property="lineManagerId" column="lineManagerId"></result>
            <result property="entryTime" column="entryTime"></result>
            <result property="wage" column="wage"></result>
            <result property="bonus" column="bonus"></result>
            <result property="bumenTableId" column="bumenTableId"></result>

        </collection>

    </resultMap>

    <select id="getDeptAndEmpById" resultMap="getDeptAndEmpById">

select *  from   dept d LEFT JOIN emp e
on d.bumenTableid=e.bumenTableId
 where d.bumenTableid=#{bumenTableid}; </select>

</mapper>

<collection property="emps" ofType="Emp"></collection> 处理多对一的映射关系