10、多对一处理 11、一对多处理

![image.png](https://img-blog.csdnimg.cn/img_convert/1900a9e1b609a0c782667a557fa77259.png#clientId=ufb45ba56-7e0b-4&from=paste&height=400&id=u496ce0a5&margin=[object Object]&name=image.png&originHeight=437&originWidth=534&originalType=binary&ratio=1&size=187876&status=done&style=none&taskId=u5834fa3b-b418-4518-8e23-7f8733cf1dc&width=489)
多个学生对应一个老师:
对于学生而言, **关联 **: 多个学生, 关联一个老师(多对一)
对于老师而言, **集合 **: 一个老师, 有很多学生(一对多)

10.1、按照查询嵌套处理(子查询)

测试环境搭建(mybatis-06)
1.导入lombok
2.新建实体类 Teacher、Student
3.建立Mapper接口
4.建立Mapper.XML文件
5.核心配置文件中绑定注册我们的Mapper接口或者配置文件文件! (方式很多)
6.测试查询是否成功

<?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=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.dao.StudentMapper">
    <!--
    思路:
        1.查询所有学生的信息
        2.根据学生查出来的tid, 寻找对应的老师!

    -->

    <select id="getStudent" resultMap="StudentTeacher">
        select *
        from mybatis.student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <!--property=实体类属性 column=数据库字段-->
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性, 我们需要单独处理 对象: association 集合: collection-->

        <!--property=Student中的对象属性 column=数据库对应的字段 javaType= property对象的类型 select=通过该方法查询到对象的数据-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select *
        from mybatis.teacher
        where id = #{id}
    </select>

</mapper>

10.2、按照结果嵌套处理(联表查询)

    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid, s.name sname, t.name tname
        from student s,
             teacher t
        where s.tid = t.id;
    </select>

    <!--这里数据库的变量名需要与 SQL中的别名一致-->
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <!--要什么就查什么-->
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

11.1、按照结果嵌套处理(列表查询)

测试环境搭建(mybatis-07)
1.导入lombok
2.新建实体类 Teacher、Student
3.建立Mapper接口
4.建立Mapper.XML文件
5.核心配置文件中绑定注册我们的Mapper接口或者配置文件文件! (方式很多)
6.测试查询是否成功
对于老师而言, **集合 **: 一个老师, 有很多学生(一对多)

 <!--获取该老师下的所有学生及老师信息 列表查询-->
    <select id="GetTeacher" resultMap="TeacherStudent">
        select s.id sid, s.name sname, t.id as tid, t.name tnamne
        from student s,
             teacher t
        where s.tid = t.id
          and t.id = #{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--ofType: 集合存储的对象类型-->
        <collection property="students" javaType="ArrayList" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

11.2、按照查询嵌套处理(子查询)

<!--获取该老师下的所有学生及老师信息2(子查询)-->
    <select id="GetTeacher2" resultMap="TeacherStudent2">
        select *
        from mybatis.teacher
        where id = #{tid}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <!--这里没有给Teacher的id赋值的原因可能是
        该column列,也就是id,被collection使用了,所以在处理resultMap映射时无法添值,
        自己添加一个result标签就可以了-->
        <result property="id" column="id"/>
        <!--property=Student中的对象属性 column=数据库对应的字段 javaType= property对象的类型 ofType=集合存储的类型 select=通过该方法查询到对象的数据-->
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherID"
                    column="id"/>

    </resultMap>

    <select id="getStudentByTeacherID" resultType="Student">
        select *
        from mybatis.student
        where tid = #{tid}
    </select>



小结

  1. 关联 - association [多对一]
  2. 集合 - collection [一对多]
  3. javaType & ofType

①JavaType 用来指定实体类中属性的类型
②ofType 集合存储的类型(pojo类型)

注意:
①保证SQL的可读性, 尽量保证通俗易懂
②注意一对多和多对一中, 属性名和字段名的问题!
③如果问题不好排查, 可以使用日志, 建议使用Log4j
面试必问:
Mysql引擎
innoDB底层原理
索引
索引优化


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值