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简单一点的方法

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

相关推荐
桦说编程15 分钟前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen16 分钟前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研19 分钟前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
wyiyiyi42 分钟前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
记忆不曾留1 小时前
Mybatis 源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
mybatis·二级缓存·sqlsession会话·executor执行器·一级缓存localcache
CCCC13101631 小时前
嵌入式学习(day 28)线程
jvm·学习
没有bug.的程序员1 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
天宇_任1 小时前
Mysql数据库迁移到GaussDB注意事项
数据库·mysql·gaussdb
甄超锋1 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
星星火柴9362 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法