MyBatis(七)一对多和多对一之resultMap

本文详细讲解了在MyBatis中如何处理多对一和一对多的关系映射。通过数据库设计、实体类创建及XML映射文件配置,展示了使用子查询和结果嵌套处理这两种方式来实现关联查询。重点介绍了association和collection标签的应用,帮助理解如何将查询结果映射到对象或集合中。
摘要由CSDN通过智能技术生成

基于白嫖:【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂

1 多对一的情况

假设有老师和学生的两张表,一个老师可以对应多个学生,如何处理查询?

1.1 数据库设计

1.1.1 老师表

+---------+------------+--------+-------+-----------+---------+
| Field   | Type       | Null   | Key   |   Default | Extra   |
|---------+------------+--------+-------+-----------+---------|
| t_id    | int(5)     | NO     | PRI   |    <null> |         |
| t_name  | varchar(4) | YES    |       |    <null> |         |
+---------+------------+--------+-------+-----------+---------+

两个属性,idname,其中id是主键;

1.1.2 学生表

+---------+------------+--------+-------+-----------+---------+
| Field   | Type       | Null   | Key   |   Default | Extra   |
|---------+------------+--------+-------+-----------+---------|
| stu_id  | int(5)     | NO     | PRI   |    <null> |         |
| name    | varchar(4) | YES    |       |    <null> |         |
| city    | varchar(5) | YES    |       |    <null> |         |
| tea_id  | int(5)     | YES    | MUL   |    <null> |         |
+---------+------------+--------+-------+-----------+---------+

四个属性,其中tea_id是外键(是老师表的主键)

1.2 实体类设计

1.2.1 老师类

public class Teacher {
    private int tId;
    private String tName;
}

1.2.2 学生类

public class Stu {
    private int id;
    private String name;
    private String stuCity;
    private Teacher teacher;//重点,此处是一个老师对象
}

重点来了!数据库表中的是一个外键,但是在java实体中希望它是一个实体对象(关联关系),所以对象作为一个属性

1.3 子查询解决

<?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="dao.StuMapper">
    <!--老师结果集映射-->
    <resultMap id="TeacherMap" type="pojo.Teacher">
        <result column="t_id" property="tId"/>
        <result column="t_name" property="tName"/>
    </resultMap>

    <!--学生结果集映射-->
    <resultMap id="StuMap" type="pojo.Stu">
        <result column="stu_id" property="id"/>
        <result column="name" property="name"/>
        <result column="city" property="stuCity"/>
        <!--对于老师复杂属性需要单独处理-->
        <association column="tea_id" property="teacher" javaType="pojo.Teacher" select="getTeacher"/>
    </resultMap>

    <!--查询学生表-->
    <select id="getStu" resultType="pojo.Stu" resultMap="StuMap">
        select * from Student;
    </select>
    <!--子任务是查询老师表-->
    <select id="getTeacher" resultType="pojo.Teacher" resultMap="TeacherMap">
        select * from Teacher where t_id=#{aaa}<!--参数名字随意-->
    </select>

</mapper>

重点就在于学生结果集resultMap映射,前面三个类型的字段stu_idnamecity直接处理,第四个字段tea_id使用association标签要映射一个java对象,使用嵌套查询语句,子查询中根据外键的值匹配该表主键返回一个Teacher对象;

1.4 结果嵌套处理

<?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="dao.StuMapper">
    <!--查询全部学生-->
    <select id="getStu" resultType="pojo.Stu" resultMap="StuMap">
        SELECT stu_id,name,city,tea_id,t_name
        FROM `Student` s,`Teacher`t
        WHERE s.tea_id=t.t_id
    </select>

    <!--学生的结果映射-->
    <resultMap id="StuMap" type="pojo.Stu">
        <result column="stu_id" property="id"/>
        <result column="name" property="name"/>
        <result property="stuCity" column="city"/>
        <association property="teacher" javaType="pojo.Teacher">
            <result property="tId" column="tea_id"/>
            <result property="tName" column="t_name"/>
        </association>
    </resultMap>

</mapper>

使用resultMap正常映射,在映射对象的时候,要映射一个association包括的对象,然后用对应的值去map它的属性;

2 一对多的情况

场景一个老师教多个学生的情况。

2.1 实体类

2.1.1 学生类

public class Stu {
    private int id;
    private String name;
    private String stuCity;
}

2.1.2 老师类

public class Teacher {
    private int tId;
    private String tName;
    private List<Stu> student;
}

注意,此处的关注点在于老师,一个老师类的成员变量含有多个学生的list集合。

2.2 结果嵌套查询

<?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="dao.TeacherMapper">

    <!--结果集映射-->
    <resultMap id="TeacherMap" type="pojo.Teacher">
        <result property="tId" column="t_id" />
        <result property="tName" column="t_name" />
        <collection property="student" ofType="pojo.Stu">
            <result property="id" column="stu_id"/>
            <result property="name" column="name"/>
            <result property="stuCity" column="city"/>
        </collection>
    </resultMap>

    <select id="getTeacher" resultType="pojo.Teacher" parameterType="int" resultMap="TeacherMap">
        SELECT tea_id,t_name,stu_id,name,city
        FROM `Student` s,`Teacher` t
        WHERE s.tea_id=t.t_id and t.t_id =1
    </select>
</mapper>

使用sql语句连表查询,把查询结果进行resultMap结果集映射,普通字段直接映射,List字段用collection去映射,其中列表泛型里面的元素用ofType去表示,里面的元素正常用result映射就可以。

3 总结

推荐使用结果集映射的方式处理,理解就是把查询到的字段放到对象或者集合里面去,是很好理解的匹配关系。association映射对象,collection映射集合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值