ResultMap 关系映射详细使用

ResultMap简介

在使用mybatis时,很多时候使用ResultType接收查询对象太过于局限性
例如:

  • 查询出的字段与接收的实体类不符合
  • 碰到关系联系的时候(一对多,多对一)
    所以就引出了ResultMap。

其实官网已经能足够说明怎么使用
(https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps)

但是讲真,光看的话真的很难熟悉它,直接开敲

使用ResultMap

创建sql 模仿一对多或多对一关系

创建数据库

use resultmaptest;
create table teacher(
	tid int primary key AUTO_INCREMENT,
	tname varchar(32) not null
);

create table student(
	sid int PRIMARY key AUTO_INCREMENT,
	sname varchar(32) not null,
	tid int not null
);
alter table student add constraint FK foreign key(tid) references teacher(tid)

insert into teacher values(1,"lukew");

insert into student values(1,"张三",1);
insert into student values(2,"李四",1);
insert into student values(3,"王五",1);
insert into student values(4,"赵六",1);
insert into student values(5,"win7",1);

创建SpringBoot项目 导入mybatis依赖


这些不用多说了。。

我是用的web的方式测试
直接上代码

pojo---------------------------------------------------------------------------------

Student
@Data
public class Student {
    private int sid;
    private String sname;
    //这里不能和数据库的表结构一样使用tid来创建Student的属性,在关系型数据库里可以,但是java里这样做是没有意义的
    //所以这里使用的是Teacher对象,因为多个学生对应的是同一个老师
    private Teacher teacher;
}
Teacher
@Data
public class Teacher {
    private int tid;
    private String tname;
    //因为一个老师同时教了多个学生,所以可以这样,虽然数据库中没有定义,但是没有关系
    private List<Student> lists;
}

dao----------------------------------------------------------------------------------

StudentDao
@Mapper
public interface StudentDao {

    /**
     * 通过sid查询学生
     */
    Student queryById(@Param("sid") Integer sid);

    Teacher teacherQueryById(@Param("tid") Integer tid);
}
TeacherDao
@Mapper
public interface TeacherDao {

    /**
     * 通过tid查询老师
     */
    Teacher queryById(@Param("tid") Integer tid);

    Student studentQueryById(@Param("tid") Integer sid);
}

Mapper.xml------------------------------------------------------------------------

StudentMapper.xml
<?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.jh.resultmap.dao.StudentDao">

    <resultMap id="StudentMap" type="student">
        <result property="sid" column="sid" />
        <result property="sname" column="sname"/>
        <association property="teacher" javaType="teacher">
            <id column="tid" property="tid"/>
            <result column="tname" property="tname"/>
        </association>
    </resultMap>

    <select id="queryById" parameterType="int" resultMap="StudentMap">
        <!--
        多对一第一种方式:结果集映射
            因为我们要把老师的名字给查出来,然后再赋到teacher属性中,所以不能直接select * ...
            应该这样
        -->
        select sid,sname,t.tid,t.tname
        from student as s,teacher as t
        where s.tid=t.tid and sid=#{sid}
    </select>

    <!--*******************************************************************************************-->

    <!--
    第二种方式:子查询映射
        先把所有的student 结果查询出来
        再将tid对应上,效果等同以上
    -->
    <resultMap id="StudentMap2" type="student">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <association property="teacher" javaType="teacher" column="tid" select="teacherQueryById"/>
    </resultMap>

    <select id="queryById" parameterType="int" resultMap="StudentMap2">
        select * from student where sid=#{sid}
    </select>

    <select id="teacherQueryById" parameterType="int" resultType="teacher">
        select * from teacher where tid=#{tid}
    </select>


</mapper>
TeacherMapper.xml
<?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.jh.resultmap.dao.TeacherDao">


    <resultMap id="queryMap" type="teacher">
        <id column="tid" property="tid"/>
        <result column="tname" property="tname"/>
        <!--
            在一对多的关系中就得使用collection了
            因为一对多,一个的里面有多个,也可以想成一个里面多一个集合
            JavaType:指定属性的类型
            ofType:集合指定的泛型
        -->
        <collection property="lists" ofType="student" javaType="list">
            <result property="sid" column="sid"/>
            <result property="sname" column="sname"/>
            <association property="teacher" column="tid">
                <id column="tid" property="tid"/>
            </association>
        </collection>
    </resultMap>

    <select id="queryById" resultMap="queryMap" parameterType="int">
        select s.sid,t.tname,s.sname,t.tid from teacher t,student s where s.tid=t.tid and  t.tid=#{tid}
    </select>

    <!--*******************************************************************************************-->


    <!--
        集合中的这个同Student中的ResultMap一个道理
    -->

    <select id="queryById" parameterType="int" resultMap="tMap">
        select * from teacher where tid=#{tid}
    </select>
    
    <resultMap id="tMap" type="teacher">
        <id property="tid" column="tid"/>
        <result property="tname" column="tname"/>
        <collection property="lists" ofType="student" javaType="ArrayList" select="studentQueryById" column="tid">
            <id column="sid" property="sid"/>
            <result column="sname" property="sname"/>
        </collection>
    </resultMap>
    
    <select id="studentQueryById" parameterType="int" resultType="student">
        select * from student where tid=#{tid}
    </select>


</mapper>

Controller---------------------------------------------------------------------------

StudentController
@RestController
public class StudentController {
    @Autowired
    private StudentDao studentDao;

    @GetMapping("/stu/{sid}")
    public Student getStudentById(@PathVariable("sid") Integer sid){
        Student student = studentDao.queryById(sid);
        return student;
    }
}
TeacherController
@RestController
public class TeacherController {
    @Autowired
    private TeacherDao teacherDao;

    @GetMapping("/t/{tid}")
    public Teacher getTeacher(@PathVariable("tid") Integer tid){
        return teacherDao.queryById(tid);
    }
}

过程大部分写在了注释里,因为这样方便思考
两种方式,不能同时存在,需要注释掉其中一个才能测试
以上是参考 狂神视频结合自己总结出来
一句话,还是得多用、多敲、多想!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值