MyBatis多表联查

举一个例子:员工与部门(员工-->部门==多对一)

相关实体类

员工(Emp)

public class Emp {
    private Integer id;
    private String eName;
    private Integer age;
    private Dept dept;
}

部门(Dept)

public class Dept {
    private Integer dId;
    private String deptName;
}

Emp.xml文件编写自定义SQL

方式一:级联属性

 <!--处理多对一映射关系方式一:级联属性赋值-->
    <!--Emp;员工表    Dept:部门表-->
    <!--员工对应部门:多对一 -->
    <resultMap id="empAndDeptResultMap" type="Emp">
        <!--id为查询的实体类主键id column为数据表id-->
        <id property="eid" column="eid"></id>
        <!--result为:除主键外,其他属性。-->
        <result property="eName" column="e_name"></result>
        <result property="age" column="age"></result>
        <result property="dept.id" column="did"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>
    <select id="getEmpAndDept" resultMap="empAndDeptResultMap">
        select * from tbl_employee e inner join tbl_deparment d on e.eid =d.did where e.eid=#{eid}
    </select>

方式二: 通过association标签实现

    <resultMap id="empAndDeptResultMapTwo" type="com.cc.entity.Emp">
        <!--id为查询的实体类主键id column为数据表id-->
        <id property="eid" column="eid"></id>
        <!--result为:除主键外,其他属性。-->
        <result property="eName" column="e_name"></result>
        <result property="age" column="age"></result>
    <!--association:处理多对一的映射关系
        property:需要处理多对一的映射关系的属性
        javaType:该属性的类型-->
        <association property="dept" javaType="com.cc.entity.Dept">
            <id property="dId" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>
    <select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
        select * from tbl_employee e inner join tbl_deparment d on e.eid =d.did where e.eid=#{eid}
    </select>

方式三:分步查询,先查询员工信息,再根据查询出的员工信息所对应的部门id进行部门信息查询

Emp.xml

<!--    分步查询-->
    <!--1.查询员工信息-->
    <resultMap id="empAndDeptByStepResultMap" type="com.cc.entity.Emp">
        <id property="eid" column="eid"></id>
        <!--result为:除主键外,其他属性。-->
        <result property="eName" column="e_name"></result>
        <result property="age" column="age"></result>
        <!--
        association:处理多对一的映射关系
        property:需要处理多对一的映射关系的属性
        select:namespace+id唯一标识
        column="did":由前一个查询结果提供的员工的部门id进行查询
        -->
        <association property="dept" select="namespace+id" column="did">
            <id property="dId" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

    <select id="getEmpAndDeptByStep" resultMap="empAndDeptResultMapTwo">
        select * from tbl_employee where eid=#{eid}
    </select>

Dept.xml

<select id="getEmpAndDeptByStepTow" resultMap="com.cc.entity.Dept">
        select * from tbl_dept where did=#{did}
    </select>

一对多(部门对应员工)

相关实体类

员工(Emp)

public class Emp {
    private Integer id;
    private String eName;
    private Integer age;
}

部门(Dept)

public class Dept {
    private Integer dId;
    private String deptName;
    private List<Emp> emps;

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

collection

Dept.xml

    <!-- 一对多 -->
    <!-- collection:用来处理一对多的映射关系 -->
    <!-- ofType:表示该属性所对应的集合中存储数据的类型 -->
    <resultMap id="deptAndEmpResultMap" type="com.cc.entity.Dept">
        <id property="dId" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps" ofType="com.cc.entity.Emp">
            <id property="id" column="eid"></id>
            <result property="eName" column="emp_name"></result>
            <result property="age" column="age"></result>
        </collection>
    </resultMap>

    <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
        select *
        from tbl_dept d left join tbl_emp e on d.did=e.did where d.did=#{did};
    </select>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CC_Waiting

我,大学未工作,感谢您的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值