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

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

相关推荐
西岭千秋雪_1 分钟前
Redis性能优化
数据库·redis·笔记·学习·缓存·性能优化
随便取个六字2 分钟前
GIT操作 学习
git·学习
chuanauc5 分钟前
Kubernets K8s 学习
java·学习·kubernetes
小张是铁粉10 分钟前
docker学习二天之镜像操作与容器操作
学习·docker·容器
极限实验室14 分钟前
INFINI Labs 产品更新 | INFINI Console 1.29.6 发布 – 优化监控图表异常毛刺等
数据库·产品
先睡17 分钟前
优化MySQL查询
数据库·sql
一头生产的驴21 分钟前
java整合itext pdf实现自定义PDF文件格式导出
java·spring boot·pdf·itextpdf
YuTaoShao28 分钟前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
zzywxc78731 分钟前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
小张是铁粉34 分钟前
oracle的内存架构学习
数据库·学习·oracle·架构