mybatis复杂环境处理:多对一and一对多

这篇博客通过一个案例讲解了在MyBatis中如何处理多对一(老师与学生)和一对多(老师与多个学生)的关系。文中介绍了两种查询方式:嵌套查询和根据结果查询,并详细解析了mapper.xml中的配置,包括`<association>`和`<collection>`标签的使用,建议使用根据结果查询以实现更便捷的操作。
摘要由CSDN通过智能技术生成

用一个案例来进行学习:即老师和学生,多个学生关联一个老师,一个老师有多个学生,即多对一,一对多。
先来看多对一

@NoArgsConstructor
@AllArgsConstructor
@Data

public class Student {
    private Integer id;
    private String name;
    private Teacher teacher;
}

本来按着数据库一张表对应一个pojo的话,这里第三个属性应该是一个tid,但这里用的是一个teacher对象包含在学生中,即对象之间的组合,是比较方便的方式。
这是student的mapper

public interface StudentMapper {
    List<Student> getAllStudent();
    List<Student> getAllStudent2();
}

student的mapper.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.li.dao.StudentMapper">
    <!--这是按照结果在进行查询-->
    <select id="getAllStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname from teacher t, student s 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">
            <result property="name" column="tname"/>
        </association>
    </resultMap>
<select id="getAllStudent" resultMap="StudentTeacher">
    select *from student;
</select>
    <resultMap id="StudentTeacher" type="Student">
        <!--association针对的是对象-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{id};
    </select>
</mapper>

从这段代码中看出,我们一共设置了两种查询学生的方式。

  • 第一种嵌套查询,类似于子查询
<select id="getAllStudent" resultMap="StudentTeacher">
    select *from student;
</select>
 <resultMap id="StudentTeacher" type="Student">
   </resultMap>

这是查询学生的id和name的,现在的问题在于说那个teacher怎么办,这个时候就要出现一种标签。

  <resultMap id="StudentTeacher" type="Student">
        <!--association针对的是对象-->
        <association property="teacher" column="tid" javaType="Teacher" select=""/>
    </resultMap>

这个标签就是代表的针对于属性是对象的情况,property代表的对象本对象,column代表其在表中对应的列,javaType就是其对应的类,最后一个select,重点来了,我们知道嵌套查询的定义。

select name,age 
from person 
where age > ( select age 
from person 
where name = '张三');

类似这种,那么后面的那个select标签所代表的就是另一个select语句
所以我们需要查询老师的信息,综合一下

<select id="getAllStudent" resultMap="StudentTeacher">
    select *from student;
</select>
    <resultMap id="StudentTeacher" type="Student">
        <!--association针对的是对象-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{id};
    </select>

大家不知道有没有怀疑过select * from teacher where id=#{id};其中为啥是id,难道不应是tid嘛?
其实这里写啥都无所谓,你写成11111111111id都行,因为这里只是一个变量,代替的是上面那个column中的值。

以上的就是第一种嵌套查询,由于相对麻烦不建议使用哦。

  • 第二种是根据结果来查询。
    就是先把这个结果查出来,再进行相应的封装操作。
<select id="getAllStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname from teacher t, student s 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">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

如上代码所示,就是把查出来的结果一个一个都封装进每个学生对象中,建议选择这种,方便快捷。

再看一对多
先看pojo


@NoArgsConstructor
@AllArgsConstructor
@Data

public class Student {
    private Integer id;
    private String name;
    private Integer tid;
}

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Teacher {
    private Integer id;
    private String name;
    private List<Student> students;
}

就是一个老师中对应着一个含有多个学生的list
再看teacher的mapper

public interface TeacherMapper {
    Teacher getTeacherById(@Param("id") int id);
}

这次我们就直接按照结果查询,嵌套查询太麻烦了

<select id="getTeacherById" resultMap="TeacherStudent">
    select s.id sid,s.name sname,t.name tname,t.id tid from teacher t, student s where s.tid=t.id and t.id=#{id};
</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>

这里使用的标签是<collection>,这就是和<association>相对应的情况,它所对应的一般是list,所以这里用的其中的一个内置属性是ofType="student"并非是<association>中的javaType。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值