Mybatis05-一对多和多对一处理

多对一的处理

  • 将配置文件等完成

查询嵌套处理

  • 思路
    1. 查询所有的学生信息
    2. 根据所有的学生tid,查询对应的老师
  • StudentMapper.xml
	<select id="getStudent" resultMap="Student_Teacher">
        select * from student;
    </select>

    <resultMap id="Student_Teacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性,需要单独处理   对象:association   集合:collection   -->
        <!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{tid}
    </select>
  • 理解:如同数据库中的嵌套查询。首先查出所有的student,然后根据学生的teacher属性,传入tid来查询对应的teacher。类似于Sql中的子查询

按照结果嵌套处理

  • 思路:直接查询出结果,进行结果集的映射
  • StudentMapper.xml
	<!--按照结果嵌套处理-->
    <select id="getStudent2" resultMap="Student_Teacher2">
        select s.id sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid = t.id;
    </select>

    <!--在sql中起了别名,列应该就变为别名-->
    <resultMap id="Student_Teacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <!--复杂类型-->
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>
  • 理解:从两个表中查出结果,然后在resultMap中对结果的每一个字段进行相应处理。类似于Sql中的联表查询

一对多的处理

根据嵌套查询方式

  • TeacherMapper.xml
 	<!--按照嵌套查询方式-->
    <select id="getTeacher2" resultMap="Teacher_student_2">
        select * from teacher where id = #{tid}
    </select>

    <resultMap id="Teacher_student_2" type="teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
         <!--因为student类型为List,因此javaType="ArrayList",其中的具体类型为"Student"-->
        <collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudent"/>
    </resultMap>

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

根据结果嵌套查询

  • TeacherMapper.xml
 	<!--按照结果嵌套查询-->
    <select id="getTeacher" resultMap="Teacher_Student">
        select s.id sid,s.name sname, t.name tname,t.id tid
        from student s, teacher t
        where s.tid = t.id and t.id = #{tid}
    </select>

    <resultMap id="Teacher_Student" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--javaType ="" 执行属性的类型    集合中的泛型信息,使用ofType获取-->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

总结

  • 知识点:
    • 关联-association
    • 集合-collection
    • 所以association是用于一对一和多对一,而collection是用于一对多的关系
    • JavaType和ofType都是用来指定对象类型的
      • JavaType是用来指定pojo中属性的类型
      • ofType指定的是映射到list集合属性中pojo的类型。
  • 注意说明:
    • 保证SQL的可读性,尽量通俗易懂
    • 根据实际要求,尽量编写性能更高的SQL语句
    • 注意属性名和字段不一致的问题
    • 注意一对多和多对一 中:字段和属性对应的问题
    • 尽量使用Log4j,通过日志来查看自己的错误
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值