Mybatis—ResultMap结果集映射

1.多对一处理

【多对一】,关联,多个学生对应一个老师

【一对多】,集合

测试环境搭建

  1. 导入lombok

  2. 新建实体类Teacher、Student(用注解@Data即可)

  3. 建立Mapper接口

  4. 建立Mapper.xml

<!--以Teacher来举例-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--绑定接口-->
<mapper namespace="dao.StudentMapper">
</mapper>
  1. 在核心配置文件中绑定注册我们的Mapper接口或者文件
   <mappers>
       <mapper class="dao.StudentMapper"/>
       <mapper class="dao.TeacherMapper"/>
   </mappers>

测试:用代码来实现sql语句:select s.id,s.name,t.name from student s,teacher t where s.tid = t.id

1.1按照查询嵌套处理

<mapper namespace="dao.StudentMapper">
<!--    1.查询学生所有信息
        2.根据查询出来的学生的tid寻找对应的老师-->

    <select id="getStudent" resultMap="StudentTeacher">
        select * from mybatis.student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
<!--        复杂的属性要单独处理
            对象:association,当处理的结果是像类的对象这种复杂类型就用这个
			javaType是property对象数据的类型,select是用查出的结果column作为参数执行下一个sql语句
            集合:collection-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="pojo.Teacher">
        select * from mybatis.teacher where id = #{id}
    </select>
</mapper>

1.2按照结果嵌套查询

<!--    按照结果嵌套处理-->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid ,s.name sname,t.name tname
        from mybatis.student s , mybatis.teacher t
        where s.tid = t.id;
    </select>

    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <!--先把结果查出来,在才Teacher这个类型的结果中再继续映射,查Teacher中的数据-->
            <result property="name" column="tname"/>
        </association>
    </resultMap>

2.一对多处理

一个老师对应多名学生

修改老师类的属性

@Data
public class Teacher {

    private int id;
    private  String name;
    //一个老师对应多个学生
    private List<Student> students;
}

2.1按照结果嵌套查询

<!--    按照结果嵌套查询-->
    <select id="getTeacher01" resultMap="TeaStu">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from mybatis.student s,mybatis.teacher t
        where s.tid = t.id and tid = #{tid}
    </select>

<!--    结果集类型为teacher,所以type是teacher-->
    <resultMap id="TeaStu" type="Teacher">
<!--        设置查询的结果Teacher对象的属性-->
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
<!--        复杂的属性,我们需要单独处理,对象:association 集合:collection
            javaType:指定属性类型,但是对于集合中的泛型信息,要使用ofType获取
            因为在Teacher中students是一个集合,所以要获取里面的信息要用ofType-->
        
<!--        property是Teacher中的学生集合属性-->
        <collection property="students" ofType="Student">
<!--            这里面就设置单个学生对象的属性-->
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

2.2按照查询嵌套查询

<!--    按照查询嵌套查询-->
    <select id="getTeacher02" resultMap="TeaStu2">
        select * from mybatis.teacher where id = #{tid}
    </select>

    <resultMap id="TeaStu" type="Teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
<!--        处理学生集合-->
<!--        这里将column里的数据作为参数传到下一个select语句中-->
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudent" column="id"/>
    </resultMap>
    <select id="getStudent" resultType="Student">
        select * from mybatis.student where tid = #{tid}
    </select>

总结:

  1. 关联—association 【多对一】
  2. 集合—collection 【一对多】
  3. javaType & ofType
    1. javaType 用来指定实体类中的属性的类型
    2. ofType用来映射到List或集合中的pojo,泛型中的约束类型
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值