关于Mybatis的多对一和一对多

首先是多对一

例如多个学生对应一个老师

学生的实体类

public class Student {
    private Integer id;
    private String name;
    //多个学生对应一个老师
    private Teather teather;

    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 Teather getTeather() {
        return teather;
    }

    public void setTeather(Teather teather) {
        this.teather = teather;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teather=" + teather +
                '}';
    }

    public Student() {
    }

    public Student(Integer id, String name, Teather teather) {
        this.id = id;
        this.name = name;
        this.teather = teather;
    }
}

接着是老师的实体类

public class Teather {
    private Integer id;
    private String name;

    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;
    }

    @Override
    public String toString() {
        return "Teather{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Teather(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Teather() {
    }
}

在学生的StudentMapper.xml写下相关的配置文件其中的学生表的tid对应老师的id

<?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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.pengwk.dao.StudentMapper">
    <!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeather2" >
    select s.id sid,s.name sname,t.name tname from studnt s,teacher t where s.tid=t.id

</select>

<resultMap id="StudentTeather2" type="Student">
    <result property="id" column="sid"></result>
    <result property="name" column="sname"></result>
    <association property="teather" javaType="Teather">
        <result property="name" column="tname"></result>
    </association>
</resultMap>
    <!--===============================================================-->

    <!--
    按照查询嵌套查询
    -->
    <!--
    思路
    1.查询所有的学生
    2.根据查询出来的学生的tid,寻找对应的老师
    -->
<select id="getStudent" resultMap="StudentTeather">
    select * from  student s,teather t where s.tid=t.id
</select>
    <resultMap id="StudentTeather" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性,我们需要单独处理
        对象使用:association
        集合使用:collection
        -->

        <association property="teather" column="tid" javaType="Teather" select="getTeather"/>
    </resultMap>

    <select id="getTeather" resultType="Teather">
        select * from teather where id=#{id}
    </select>

</mapper>

接下来是一对多

一个老师对应多个学生

学生的实体类

@Data
public class Student {
    private Integer id;
    private String name;
    private Integer tid;


}

注:@Data是使用的注解插件Lombok

老师的实体类

@Data
public class Teather {
    private Integer id;
    private String name;
    //一个老师对应多个学生
    private List<Student> students;

}

接下来是TeaTherMapper.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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.pengwk.dao.TeatherMapper">
<!--按结果嵌套查询-->
<select id="getTeather" resultMap="TeatherStudent">
    select s.id sid,s.name sname,t.name tname ,t.id tid
    from student s,teather t where s.tid=t.id and t.id=#{tid}
</select>
    <resultMap id="TeatherStudent" type="Teather">
        <result property="id" column="tid"></result>
        <result property="name" column="tname"/>
        <!--因为是一对多所以学生查出来的是一个集合要用collection-->
        <!--javaType 可以指定属性的类型
        集合中的泛型信息:我们使用ofType获取-->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    <!--
   按照查询嵌套查询
   -->
    <select id="getTeather2" resultMap="TeatherStudent2">
        select * from teather where id=#{tid}
    </select>
    <resultMap id="TeatherStudent2" type="Teather">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getId" column="id"></collection>
    </resultMap>
    <select id="getId" resultType="Student">
        select * from student where tid=#{id}
    </select>
</mapper>

其中最主要的就是有两种查询的方法:一种是按照结果的嵌套查询sql语句复杂但是结果的关系简单,还有一个就是sql语句简单但是关系复杂。

通过视频的学习也是对其中的javaType和ofType有所了解

javaType是用来指定实体类中的属性

ofType是用来指定映射到List或者集合中的pojo类型,泛型中的约束类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值