Mybatis-10 多对一和一对多

多对一

  • 多个学生对应一个老师

  • 对于学生这边而言,关联, 多个学生关联一个老师 【多对一】

  • 对于老师而言,集合,一个老师,有很多学生【一对多】

SQL:连表查询,子查询

测试环境搭建


  1. 导入Lombok

  1. 新建实体类student\teacher

  1. 建立Mapper接口

  1. 建立Mapper.xml文件

  1. 在核心配置文件中绑定注册我们的Mapper接口或者文件【方式很多,随便选】

  1. 测试查询是否成功

按照查询嵌套处理


复杂的属性:对象:association(这里通过学生来查询一个对应的老师,所以是对象) 集合:collection

javaType:指定一个属性的类型

<!--  查询思路:    1.查询所有学生           2.根据tid 查询老师    -->
    <resultMap id="StudentTeacher" type="Student">
<!--        复杂的属性需要单独处理-->
        <result property="id" column="id"/>
<!--        复杂的属性:对象:association
                     集合:collection
 -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getStudent" resultMap="StudentTeacher">
        select * from student;
    </select>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{id};
    </select>

按照结果嵌套处理


<!--方法2:按照结果嵌套处理    -->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname 
        from student s,teacher t 
        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>

回顾Mysql多对一查询方式:

  • 子查询

  • 连表查询

一对多


比如,一个老师拥有多个学生,对于老师而言,就是一对多的关系。

复杂的属性:对象:association 集合:collection(这里就应该对应多个,是集合)

javaType:指定一个属性的类型,集合中的泛型信息,我们使用ofType获取

环境搭建

实体类要做修改:

@Data
public class Teacher {
    private int id;
    private String name;
    //一个老师拥有多个学生
    private List<Student> students;
}
​
@Data
public class Student {
    private int id;
    private String name;
    private int tid;
}

按照查询嵌套查询

    <select id="getTeacher2" resultMap="TeacherStudent2">
        select * from teacher where id=#{tid};
    </select>

    <resultMap id="TeacherStudent2" type="teacher">
        <collection property="students" javaType="ArrayList" ofType="Student"         select="getStudent" column="id"/>
    </resultMap>

    <select id="getStudent" resultType="student">
        select * from student where tid=#{tid};
    </select>

查询结果:

​Teacher(
    id=0,  //因为没有去查老师的id,所以是0
    name=张老师, 
    students=[
        Student(id=11, name=张三, tid=1),
        Student(id=22, name=李四, tid=1)
        ]
    )

按照结果嵌套处理(推荐)

public interface TeacherMapper {
    //获取指定老师下的所有学生和老师的信息
    public List<Teacher> getTeacher(@Param("tid") int id);

}
    <select id="getTeacher" resultMap="TeacherStudent">
        select t.id tid,t.name tname,s.id sid,s.name sname
        from teacher t,student s
        where s.tid=t.id and t.id=#{tid};
    </select>

    <resultMap id="TeacherStudent" type="teacher">
        <result column="tid" property="id"/>
        <result column="tname" property="name"/>
        
//这里是集合,所以用collection,集合里面的对象类型为Student
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

查找结果:

​Teacher(
    id=1, 
    name=张老师,
    students=[
        Student(id=11, name=张三, tid=1), 
        Student(id=22, name=李四, tid=1)
        ]
    )

小结

  1. 关联 -association 【多对一】

  1. 集合 -collection 【一对多】

  1. JavaType & ofType

  1. javaType用来指定实体类中属性得类型

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

注意点:

  • 保证sql的可读性,尽量保证通俗易懂

  • 注意一对多和多对一中,属性名和字段的问题

  • 如果问题不好排除,可以使用日志,建议使用Log4j

面试高频:

  • mysql引擎

  • InnoDB的底层原理

  • 索引

  • 索引优化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值