zoukankan      html  css  js  c++  java
  • mybatis学习 十四 resultMap标签 一对一(联合查询)

    1.使用 resultMap 实现关联单个对象(联合查询方式)

    <resultMap type="Student" id="stuMap1">
        <id column="sid" property="id"/>
        <result column="sname" property="name"/>
        <result column="age" property="age"/>
        <result column="tid" property="tid"/>
        <association property="teacher" javaType="Teacher" >
    
            <id column="tid" property="id"/>
            <result column="tname" property="name"/>
        </association>
    </resultMap>
    
    <select id="selAll1" resultMap="stuMap1">
        select s.id sid,s.name sname,age age,t.id tid,t.name tname
         
        FROM student s left outer join teacher t on s.tid=t.id
    </select>    

    注意id为selAll1的select标签与下面id为selAll的select标签的区别

          <resultMap type="student" id="stuMap">
                  <id column="id" property="id"/>
                  <result column="name" property="name"/>
                  <result column="age" property="age"/>
                  <result column="tid" property="tid"/>
                  <association property="teacher" javaType="Teacher" column="tid" select="com.xxx.mapper.TeacherMapper.selById" >
                  </association>
              </resultMap>
              <select id="selAll" resultMap="stuMap">
                  select * from student
              </select>

    第一中是联合查询,第二中不是,是先查询每一个学生,然后再根据学生的tid去查询老师,第二种实现效率低

  • 相关阅读:
    图片展示和上传需要注意的问题
    大数据技能学习
    C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码)
    领导力
    .NetCore 三种生命周期注入方式
    Redis常见面试题
    .NET Core开发日志——Middleware
    编程的灵魂
    递推算法
    分治算法
  • 原文地址:https://www.cnblogs.com/cplinux/p/9657025.html
Copyright © 2011-2022 走看看