mybatis处理多对一和一对多问题

多对一

多个学生有一个老师

在这里插入图片描述

实体类

@Data //GET,SET,ToString,有参,无参构造
public class Teacher {
     private int id;
     private String name;
}
@Data
public class Student {
    private int id;
    private String name;
    //多个学生可以是同一个老师,即多对一
    private Teacher teacher;
}

Mapper接口

public interface StudentMapper {
    //获取所有学生及对应老师的信息
    public List<Student> getStudents();
}

Mapper.xml

方式一:按查询嵌套处理

<select id="getStudents" resultMap="StudentTeacher">
   select * from student
</select>
<!--resultMap 的id和上方resultMap属性值相同,形成映射-->
<resultMap id="StudentTeacher" type="Student">
    <!--其他属性名和字段名相同,可省略result映射-->
   <!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
   <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    <!--select标签和下方查询老师结果集的id相同-->
</resultMap>

<select id="getTeacher" resultType="teacher">
     select * from teacher where id = #{id}
</select>

方式二:按结果嵌套处理

<select id="getStudents2" resultMap="StudentTeacher2" >
    select s.id sid, s.name sname , t.name tname
    from student s,teacher t
    where s.tid = t.id
</select>

<resultMap id="StudentTeacher2" type="Student">
    <!--column值为上方sql语句的别名-->
    <id property="id" column="sid"/>
    <result property="name" column="sname"/>
    <!--关联对象property 关联对象在Student实体类中的属性-->
    <association property="teacher" javaType="Teacher">
         <result property="name" column="tname"/>
    </association>
</resultMap>

按照查询进行嵌套处理就像SQL中的子查询

按照结果进行嵌套处理就像SQL中的联表查询

一对多

一个老师教多个学生

实体类

@Data
public class Student {
    private int id;
    private String name;
    private int tid;
}
@Data
public class Teacher {
    private int id;
    private String name;
    //一个老师多个学生
    private List<Student> students;
}

Mapper接口

public interface TeacherMapper {
    //获取指定老师,及老师下的所有学生
    public Teacher getTeacher(int id);
}

Mapper.xml

方式一:按查询嵌套处理

<select id="getTeacher2" resultMap="TeacherStudent2">
    select * from teacher where id = #{id}
</select>

<resultMap id="TeacherStudent2" type="Teacher">
    <!--column是一对多的外键 , 写的是一的主键的列名-->
    <collection property="students" javaType="ArrayList" ofType="Student" column="id"
select="getStudentByTeacherId"/>
</resultMap>

<select id="getStudentByTeacherId" resultType="Student">
    select * from student where tid = #{id}
</select>

方式二:按结果嵌套处理

<select id="getTeacher" 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=#{id}
</select>
    
<!--
JavaType和ofType都是用来指定对象类型的
JavaType是用来指定pojo中属性的类型
ofType指定的是映射到list集合属性中pojo的类型。
-->
<resultMap id="TeacherStudent" type="Teacher">
     <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>

association是用于一对一和多对一,而collection是用于一对多的关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值