Mybatis中一对多和多对一的映射学习

首先搭建学习环境
创建两张表,老师表和学生表
在这里插入图片描述
在这里插入图片描述
学生的tid是外键,依赖老师的id。

然后编写基本的配置文件
在这里插入图片描述
学生实体类:

package com.dongmu.pojo;
import lombok.Data;
@Data
public class Student {
    private int id;
    private String name;
    private Teacher teacher;
}

老师实体类:

package com.dongmu.pojo;


import lombok.Data;

@Data
public class Teacher {
    int id;
    String name;

}

然后查询学生,并输出对应的老师。这时候就会出现问题,因为老师是引用数据类型
mapper文件

<?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.dongmu.dao.StudentDao">
    <select id="getAllSutdents" resultType="Student">
        select s.*,t.name as teacher from student s,teacher t where s.tid = t.id
  </select>
</mapper>

查询结果
在这里插入图片描述
使用resultMap的associate指定结果的类型
在这里插入图片描述

 <select id="getAllSutdents2" resultMap="map2">
        select s.id,s.name,t.name as tname from student s,teacher t where s.tid = t.id
    </select>

    <resultMap id="map2" type="Student">
        <result column="id" property="id"/>
        <result column="name" property="name"/>  
        <association property="teacher" javaType="Teacher">
            <result column="tname" property="name"/>
            <result column="s.id" property="id"/>
        </association>
    </resultMap>

在这里插入图片描述
下面介绍一对多的方法
dao接口:

package com.dongmu.dao;

import com.dongmu.pojo.Teacher;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface TeacherDao {
    public List<Teacher> getAllTeacher(@Param("tid") int id);

    public List<Teacher> getAllTeacher2(@Param("tid") int id);
}

mapper文件

<?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.dongmu.dao.TeacherDao">
按照查询嵌套处理
    <select id="getAllTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname, t.name tname ,t.id tid
        from  student s ,teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>

    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
按照结果嵌套处理
    <select id="getAllTeacher2" resultMap="Teacher2">
       select  * from teacher where id=#{tid}
    </select>
    <resultMap id="Teacher2" type="Teacher">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="selectstu">
        </collection>
    </resultMap>
    <select id="selectstu" resultType="Student">
        select * from student where tid=#{id}
    </select>


</mapper>

javaType指定实体类中属性的类型
ofType:用来指定集合映射到List或者集合中的类型,(泛型中的约束类型)

注意慢sql问题,要注意sql的效率问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北海冥鱼未眠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值