mybatis05:复杂查询:(多对一,一对多)

mybatis05:复杂查询:(多对一,一对多)


文章目录


前言:


多对一 : 关联 : 使用association



一对多 : 集合: 使用collection



sql语句分析:






提示:以下是本篇文章正文内容:

一、多对一:student 对 teacher

1.按照查询嵌套处理:与子查询很相似


xml 复制代码
    <!--
    思路:1. 查询所有的学生
         2. 根据查询出来的tid 寻找对应的老师
	-->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from student
    </select>
    
    <resultMap id="StudentTeacher" type="com.zhu.pojo.Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性与要单独处理 对象 association  集合 collection-->
        <association property="teacher" column="tid" 
        javaType="com.zhu.pojo.Teacher" select="getTeacher"/>   
    </resultMap>
    
    <select id="getTeacher" resultType="com.zhu.pojo.Teacher">
        select * from teacher where id = #{id}
    </select>

1.select标签,将resultType修改为resultMap,那么我们就需要写一个resultMap标签来映射(ORM)

2.多的一方 与 一的一方,student 与 teacher的关系是"关联"(association),这里是复杂查询的部分了,我们运用子查询的思想,根据tid来再去查询teacher对象。



2.按照结果嵌套处理(推荐自己使用)

xml 复制代码
    <!-- 思路:
    1.我们正常写sql语句(联表查询),
    2.但是对结果集映射里面的teacher对象,他的属性"再映射一次" 
    -->
    <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>
    
     <resultMap id="StudentTeacher2" type="com.zhu.pojo.Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="com.zhu.pojo.Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

1.resultType修改为resultMap

2.我们对查询结果中的类,在映射一遍



二、一对多:teacher对student

1. 写sql难,但写resultMap简单。(推荐自己使用)


xml 复制代码
<!--  按结果嵌套查询-->
<select id="getTeacher" resultType="com.zhu.pojo.Teacher">
    select * from teacher;
</select>
<select id="GetTeacher" resultMap="GetStudent">
    select s.id sid ,s.name sname ,t.name tname,t.id tid
    from student s,teacher t
    where s.tid = t.id and t.id=#{tid}
</select>
<resultMap id="GetStudent" type="com.zhu.pojo.Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
<!--
javaType="" 指定的属性的类型
集合中的泛型信息用 ofType 获取
-->
    <collection property="students" ofType="com.zhu.pojo.Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>


2. 写sql简单,但写resultMap难。

xml 复制代码
<!--先查询老师,再查学生-->
<select id="GetTeacher2" resultMap="GetStudent2">
    select *from teacher where id=#{tid}
</select>
<resultMap id="GetStudent2" type="com.zhu.pojo.Teacher">
    <collection property="students"  javaType="ArrayList" ofType="com.zhu.pojo.Student" select="GetStudentByTeacherId" column="id"/>
</resultMap>
<select id="GetStudentByTeacherId" resultType="com.zhu.pojo.Student">
    select *from student where tid = #{tid}
</select>

总结

提示:这里对文章进行总结:

💕💕💕

重点:推荐使用 写sql难一点,但写resultMap简单一点的方法

(完完整整将语句语句给写完!!!)

相关推荐
m0_689618282 分钟前
水凝胶发生器,不对称设计妙,医电应用前景广
笔记
Ace'3 分钟前
每日一题&&学习笔记
笔记·学习
A尘埃5 分钟前
SpringBoot的数据访问
java·spring boot·后端
yang-23076 分钟前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端
IM_DALLA6 分钟前
【Verilog学习日常】—牛客网刷题—Verilog进阶挑战—VL25
学习·fpga开发·verilog学习
沉登c6 分钟前
幂等性接口实现
java·rpc
挥剑决浮云 -17 分钟前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
代码之光_198018 分钟前
SpringBoot校园资料分享平台:设计与实现
java·spring boot·后端
向上的车轮20 分钟前
Django学习笔记二:数据库操作详解
数据库·django