mybatis 一对多查询

在mybatis 中一对多查询,我遇到两种情况:
1、使用两表 join,返回包含一对多的 list 的对象 。
2、2次sql,使用 IN (id)。(大表查询会很影响效率,查询时间会很长,所以有的公司禁止大表join)

example:

public class Student {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

public class Course {
    private Integer id;
    private Integer stuId;
    private String name;
    private Integer score;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getStuId() {
        return stuId;
    }

    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }
}

public class Info extends Student {
    private List<Course> courses;

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }
}

一个 student 会有多个course,要想查询一个班级所有同学的课程呢?

这是明显的一对多的类型。

使用 collection 我这边就不仔细说了,就是将course作为集合放到 Info 类里,
ofType 是对应的course 类型。
我们在 mapper 中需要注意的是 :

 <resultMap id="BaseResultMap" type="biz.bean.Student">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="age" property="age" jdbcType="INTEGER"/>
    </resultMap>

    <resultMap id="InfoMap" type="biz.bean.Info" extends="BaseResultMap">
        <collection property="courses" ofType="biz.bean.Course">
            **<id column="cid" property="id" jdbcType="INTEGER"/>**
            <result column="stu_id" property="stuId" jdbcType="INTEGER"/>
            <result column="name" property="name" jdbcType="VARCHAR"/>
            <result column="score" property="score" jdbcType="INTEGER"/>
        </collection>
    </resultMap>

sql:

    <select id="selectStudentInfos" resultMap="InfoMap">
        SELECT s.id,s.name,s.age,c.cid,c.stu_id,c.name,c.score
        FROM stu s  JOIN course c on  c.stu_id=s.id
    </select>

<select id="selectStudentInfos" resultMap="InfoMap">
    select * from course where stu_id IN
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
</select>

至此,谨记,共勉。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值