MyBatis-一对多、多对一

15 篇文章 0 订阅
5 篇文章 0 订阅

一对多和多对一,是一个相对的概念。

以老师和学生为例,最常见的情况就是:一个老师教授很多学生。站在学生的角度,是很多学生被一个老师教,即很多学生关联一个老师(多对一);站在老师的角度,是一个老师教授很多学生,即一个老师有多个学生(一对多)。

在MyBatis中,处理多对一的问题使用 association标签(关联);处理一对多的问题使用 collection标签(集合)。

多对一

按照查询嵌套处理

查询所有的学生信息。

根据查询出来的学生的tid,寻找对应的老师。

<select id="getStudent" resultMap="StudentTeacher">
        select * from mybatis_test.student;
</select>
<resultMap id="StudentTeacher" type="Student">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <association column="tid" property="teacher" javaType="Teacher" select="getTeacher" />
</resultMap>
<select id="getTeacher" resultType="Teacher">
        select * from mybatis_test.teacher where id = #{id};
</select>

按照结果嵌套处理

<select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname from mybatis_test.student s,mybatis_test.teacher t where s.tid = t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
        <result column="sid" property="id"/>
        <result column="sname" property="name"/>
        <association property="teacher" javaType="Teacher">
            <result column="tname" property="name"/>
        </association>
</resultMap>

一对多

按照查询嵌套处理

 <select id="getTeacher2" resultMap="TeacherStudent2">
        select * from mybatis_test.teacher;
 </select>
<resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
        select * from mybatis_test.student where tid = #{id}
</select>

按照结果嵌套处理

 <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from mybatis_test.student s,mybatis_test.teacher t
        where s.tid = t.id;
 </select>
 <resultMap id="TeacherStudent" type="Teacher">
        <result column="tid" property="id"/>
        <result column="tname" property="name"/>
        <collection property="students" ofType="Student">
            <result column="sid" property="id"/>
            <result column="sname" property="name"/>
            <result column="tid" property="tid"/>
        </collection>
 </resultMap>

按照结果嵌套处理和按照查询嵌套处理,分别对应了sql中的联表查询和子查询。

总结

  1. 多对一:关联,使用association
  2. 一对多:集合,使用collection
  3. javaType 和 ofType的区别
    • JavaType 用来指定实体类中属性的类型
    • ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型

需要注意的是:

  • 需要保证SQL的可读性,尽量保证通俗易懂
  • 注意一对多和多对一中,属性名和字段的问题
  • 如果问题不好排查错误,可以使用日志 , 建议使用 Log4j

SQL 语句写的不好,会出现慢SQL的问题(查询时间过长)。

MySql的面试高频问点

  • MySql引擎
  • InnoDB底层原理
  • 索引
  • 索引优化
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多的关联查询。在MyBatis-Plus一对多的关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张表,一张是学生表,另一张是课程表,一个学生可以选多门课程,那么我们就可以用一对多关联查询来查询某个学生选的所有课程。 首先,在学生表定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程表的外键关联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程表定义一个属性Long studentId,并使用@TableField注解将该属性与学生表的主键关联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值