【Mybatis】实现连(多)表查询(多对一)

补充

association: 一个复杂类型的关联;许多结果将包装成这种类型,当实体类中的属性为“其他属性类”的时候,在回填结果的时候,需要使用association做映射

一、环境搭建

1. 数据库搭建

Student:
在这里插入图片描述

Teacher:
在这里插入图片描述

2. 实体类的创建:

Student:

package stdpei.pojo;

import lombok.Data;

/**
 * @author huxuehao
 * @create 2021-06-20-6:46
 */
@Data
public class Student {
    private int id;
    private String name;

    private Teacher teacher;
}

Teacher:

package stdpei.pojo;

import lombok.Data;

/**
 * @author huxuehao
 * @create 2021-06-20-6:45
 */
@Data
public class Teacher {
    private int id;
    private String name;
}

3. xxxMapper的搭建

StudentMapper:

public interface StudentMapper {
    List<Student> getStudents();
    List<Student> getStudents2();
}

4.注册Mapper

mybatis-config.xml:

<!--注册mapper-->
    <mappers>
        <mapper class="stdpei.dao.TeacherMapper"/>
        <mapper class="stdpei.dao.StudentMapper"/>
    </mappers>

二、通过【结果集嵌套】的方式解决

1.查询结果要与实体类相对应

首先我们需要明白,我们的查询结果最后是要回填到对应的实体类(Studen)中去的。所以,查询的东西应该与实体类中相对应。

在Student中的内容包括四个(uid、uname、tid、tname),如下:
在这里插入图片描述
所以在查询上的时候,我们也需要查询上述的四条信息,如下:
在这里插入图片描述

2.查询结果的回填要准确

很明显,最后的查询结果是要回填到Student中,但是在student中有实体类类型的属性,我们需要使用“association”标签做映射。

首先先确定待回填的位置:
在这里插入图片描述
再确定使用什么进行回填:
在这里插入图片描述

3.代码及测试

代码:StudentMapper.xml

<mapper namespace="stdpei.dao.StudentMapper">

    <!--通过结果集嵌套解决“一对多”-->
    <select id="getStudents" resultMap="st">
        select s.id sid, s.name sname, s.tid tid, t.name tname
        from `student` s,`teacher` t
        where s.tid = t.id
    </select>
    
    <resultMap id="st" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>
</mapper>

测试:

    @Test
    public void getStudent(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.getStudents();
        for (Student student : students) {
            System.out.println(student);
        }

        sqlSession.close();
    }

在这里插入图片描述

三、通过【查询嵌套】的方式解决

1.查询原理

这个方法上一个方法的不同点在于将,这个方法需要多条查询语句。
"第二条语句"借助"第一条语句的部分查询结果"进行再查询,将两条语句的查询结果回填到实体类中

2.详情介绍

在这里插入图片描述

3.代码及测试

代码:StudentMapper.xml

<mapper namespace="stdpei.dao.StudentMapper">
	<!--通过查询嵌套解决“一对多”-->
    <select id="getStudents2" resultMap="st2">
        select * from `student`
    </select>
    
    <resultMap id="st2" type="Student">
        <association property="teacher"  javaType="Teacher" column="tid" select="getTeacher2"/>
    </resultMap>
    
    <select id="getTeacher2" resultType="stdpei.pojo.Teacher">
        select * from teacher where id = #{tid}
    </select>
</mapper>

测试:

    @Test
    public void getStudent2(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.getStudents2();
        for (Student student : students) {
            System.out.println(student);
        }

        sqlSession.close();
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值