1. 程式人生 > >mybatis學習 十四 resultMap標簽 一對一(聯合查詢)

mybatis學習 十四 resultMap標簽 一對一(聯合查詢)

rop prope mybatis div pro sele sel 關聯 bsp

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去查詢老師,第二種實現效率低

mybatis學習 十四 resultMap標簽 一對一(聯合查詢)