【Mybatis 学习】一篇文章带你学会 Mybatis 多表查询之一对多查询

需求:查询所有用户信息及用户关联的账户信息。
分析:用户信息和他的账户信息为一对多关系,并且查询过程中如果用户没有账户信息,此时也要将用户信息查询出来,我们想到了左外连接查询比较合适。
在这里插入图片描述

一、一对多查询

(1)修改User 类,为其加入 账户信息

@Data
public class User implements Serializable{
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    /**
     * 一对多关系映射:主表实体应该包含从表实体的集合引用
     */
    private List<Account> accounts;
}

(2)对应的UserDao 接口的查询方法

public interface UserDao {
    /**
     * 查询所有操作
     * @return
     */
    List<User> findAll();
}

(3)相应的UserDao.xml 的编写

<mapper namespace="mybatis.dao.UserDao">
    <!--提取重复的 SQL 语句-->
    <sql id="defaultUser">
        select * from user
    </sql>
    <!--定义封装account和user的resultMap-->
    <resultMap id="userAccountMap" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
        <!-- 配置user对象中accounts集合的映射 -->
        <collection property="accounts" ofType="account">
            <id column="aid" property="id"></id>
            <result column="uid" property="uid"></result>
            <result column="money" property="money"></result>
        </collection>
    </resultMap>

    <!--查询所有用户-->
    <!--id 表示方法名称,resultType 表示要封装到哪里去-->
    <select id="findAll" resultMap="userAccountMap">
        select * from user u left outer join account a on u.id = a.uid;
    </select>
</mapper>

这里注意collection 是用于建立一对多中集合属性的对应关系,ofType 用于指定集合元素的数据类型

collection:用于建立一对多中集合属性的对应关系
property="accounts":对应的实体类中的属性
ofType="account":指定关联查询的结果集中的对象类型。此处可以使用别名,也可以使用全限定名。

二、分布实现一对多查询

背景表同:分布实现一对一查询

	/**
     * 通过分步查询查询部门以及部门所对应的学生信息
     * 分步查询第一步:通过cid查询班级
     */
    StuClass getClassAndStuByStepOne(@Param("cid") Integer cid);
	<resultMap id="classAndStuClassByStepResultMap" type="stuClass">
        <id property="cid" column="cid"></id>
        <result property="className" column="cname"></result>
        <collection property="students"
                     select="com.atguigu.mybatis.mapper.StuMapper.getClassAndStuByStepTwo"
                     column="cid"
                     fetchType="eager"></collection>
    </resultMap>

    <!--StuClass getClassAndStuByStepOne(@Param("cid") Integer cid);-->
    <select id="getClassAndStuByStepOne" resultMap="classAndStuClassByStepResultMap">
        select * from class where cid = #{cid}
    </select>
	/**
     * 通过分步查询查询部门以及部门所对应的学生信息
     * 分步查询第二步:查询学生信息
     */
    Student getClassAndStuByStepTwo(@Param("cid") Integer cid);
	<!--Student getStuAndClassByStepOne(@Param("sid") Integer sid);-->
    <select id="getStuAndClassByStepOne" resultMap="stuAndClassByStepResultMap">
        select * from stu where sid = #{sid}
    </select>
	@Test
    public void testGetClassAndStuByStep(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);
        StuClass students = mapper.getClassAndStuByStepOne(1);
        System.out.println(students.getStudents());
        // System.out.println(student.getAClass().getCid());
    }

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南淮北安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值