mybatis查询

本文详细介绍了MyBatis中的一对一、一对多、多对多和自关联查询的实现方式,包括单向和双向关联关系,并通过具体的SQL查询和映射配置展示了如何进行数据查询。通过内联、外连接、子查询等多种方法,演示了如何在员工和部门的场景中进行关联关系的查询操作。
摘要由CSDN通过智能技术生成

一、关联查询

关联关系

  1. 一对一

  2. 一对多(多对一)

  3. 多对多

1.1 单向关联关系

只能由一方查询出另一方:

比如一对多和(多对一):

只能由一方查询出多方,或者只能由多方查询出一方(类似一对一)

1.2 双向关联关系

关联的双方可以互相查询

比如一对多和(多对一):

既能由一方查询出多方,也能由多方查询出一方(类似一对一)

1.3 自关联查询

自己查询出自己的上级或者下级同类的对象的信息

二、关联关系查询

场景:员工和部门符合一对多(多对一)的关联关系映射

2.1 many2one(多对一)

 

情况一: 查询员工并只从员工查询部门(从多方查一方)

可以使用两种方式查询

方式一:使用等值、内联机、外连接这种连接查询语句来查询关联信息

要点:在SQL语句中一次性将需要的信息全部查询出来

<!--resultMap完成手动映射-->
    <resultMap id="empMapper" type="emp">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
        <!--多方查询一方,使用association标签
            property属性:设置关联属性
            javaType属性: 设置封装的JavaBean的类型
​
        -->
        <association property="apart" javaType="apart">
            <id column="aid" property="aid"/>
            <result column="aname" property="aname"/>
        </association>
    </resultMap>
​
    
    
    
    <select id="findByEid" resultMap="empMapper">
​
        select e.eid, e.ename, e.age, e.gender, a.aid, a.aname
        from t_emp as e
        inner join t_apart a
        on e.aid = a.aid
        and e.eid = #{eid}
​
    </select>

方式二:使用子查询方式查询

员工映射文件

 <!--resultMap完成手动映射-->
    <resultMap id="empMapper" type="emp">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
​
        <!--使用association标签
            column属性: 设置列名
            property属性: 设置属性名
            javaType属性:设置关联的属性的类型
            select属性: 指定另一个映射器方法来查询
        -->
        <association  column="aid" property="apart"
                      javaType="apart"
                      select="com.dyh.dao.ApartMapper.findByAid"/>
​
    </resultMap>
​
    
    
    
    <select id="findByEid" resultMap="empMapper">
​
       select eid, ename, age, gender, aid from t_emp
       where eid = #{eid}
    </select>

部门映射文件

<?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">
<mapper namespace="com.dyh.dao.ApartMapper">
    
    <resultMap id="apartMapper" type="apart">
        <id column="aid" property="aid"/>
        <result column="aname" property="aname"/>
    </resultMap>
​
    <select id="findByAid" resultMap="apartMapper">
        select aid, aname from t_apart
        where aid = #{aid}
    </select>
​
</mapper>

2.2 One2Many(一对多)

方式一:连接查询

 <!--resultMap完成手动映射-->
    <resultMap id="apartMapper" type="apart">
        <id column="aid" property="aid"/>
        <result column="aname" property="aname"/>
​
        <!--如果关联属性是集合,一方查询多方
            使用collection标签
            property属性:设置属性名称
            ofType属性:设置集合中元素的类型
        -->
        <collection property="emps" ofType="emp">
            <id column="eid" property="eid"/>
            <result column="ename" property="ename"/>
            <result column="age" property="age"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
​
    
    
    
    <select id="findByAid" resultMap="apartMapper">
​
        select a.aid, a.aname, e.eid, e.ename, e.age, e.gender
        from t_apart as a
        inner join t_emp as e
        on a.aid = e.aid
        and a.aid = #{aid}
​
    </select>

方式二: 子查询

部门映射文件

<!--resultMap完成手动映射-->
    <resultMap id="apartMapper" type="apart">
        <id column="aid" property="aid"/>
        <result column="aname" property="aname"/>
        
        <collection property="emps" ofType="emp" column="aid"
                    select="com.dyh.dao.EmpMapper.findEmpsByAid"/>
​
    </resultMap>
    
    <select id="findByAid" resultMap="apartMapper">
​
        select aid, aname from t_apart
        where aid = #{aid}
​
    </select>

员工的映射文件

 

2.3 双向关联

既可以从一方查询多方,又可以从多方查询一方

<!--resultMap完成手动映射-->
    <resultMap id="empMapper" type="emp">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
    </resultMap>
​
    
    <select id="findEmpsByAid" resultMap="empMapper">
​
        select eid, ename, age, gender from t_emp
        where aid = #{aid}
​
    </select>

2.4 多对多

场景:学生和兴趣班

 

2.5 自关联

比如菜单(menu)、新闻标签(newsLabel)有一级、二级、三级...菜单,自关联就是,其中一个菜单能查询出其父节点,或者查询出其子节点

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值