MyBatis关联查询,一对多关联查询

------------------------------------抄笔记------------------------------------

MyBatis关联查询,一对多关联查询

实体关系图,一个国家对应多个城市

在这里插入图片描述

一对多关联查询可用三种方式实现:

单步查询,利用collection标签为级联属性赋值;
分步查询:
利用association标签进行分步查询;
利用collection标签进行分步查询

1.单步查询

利用collection标签实现一对多单步关联查询:

指定进行关联查询的Java Bean字段,即collection标签的 property 属性;
指定集合中的Java Bean类型,即collection标签的 ofType属性;

实体类

public class CountryPlus {
    Long id;
    String name;
    Date lastUpdate;
    List<City> cityList;
}
public class City {
    Long id;
    String name;
    Long countryId;
    Date lastUpdate;
}

查询标签

   <select id="selectCountryPlusById" resultMap="countryPlusResultMap">
        select country.country_id as country_id,
                country,
                country.last_update as last_update,
                city_id,
                city,
                city.country_id as city_country_id,
                city.last_update as city_last_update
        from country left join city on  country.country_id = city.country_id
        where country.country_id=#{id}
    </select>

resultMap

<resultMap id="countryPlusResultMap" type="canger.study.chapter04.bean.CountryPlus">
    <id column="country_id" property="id"/>
    <result column="country" property="name"/>
    <result column="last_update" property="lastUpdate"/>
    <collection property="cityList" ofType="canger.study.chapter04.bean.City">
        <id column="city_id" property="id"/>
        <result column="city" property="name"/>
        <result column="city_country_id" property="countryId"/>
        <result column="city_last_update" property="lastUpdate"/>
    </collection>
</resultMap>

2.分步查询

利用collection标签进行分步查询

指定collection标签的 property 属性;
通过select属性指定下一步查询使用的 statement id;
通过column属性向下一步查询传递参数,传递多个参数的方法见MyBatis关联查询,一对一关联查询中的分步查询;

select标签

<select id="selectCountryPlusByIdStep" resultMap="countryPlusResultMapStep">
    select *
    from country
    where country_id=#{id}
</select>

resultMap标签

<resultMap id="countryPlusResultMapStep" type="canger.study.chapter04.bean.CountryPlus">
    <id column="country_id" property="id"/>
    <result column="country" property="name"/>
    <result column="last_update" property="lastUpdate"/>
    <collection property="cityList"
                 select="canger.study.chapter04.mapper.CityMapper.selectCityByCountryId"
                 column="country_id">
    </collection>
</resultMap>

利用association标签进行分步查询
和使用collection标签的方式相同

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值