我們可以從一個<resultMap>中擴展出一個新的<resultMap>,這樣原先<resultMap>屬性就可以繼承過來了。
<resultMap type="Student" id="StudentResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <result property="phone" column="phone" /> </resultMap> <resultMap type="Student" id="StudentWithAddre***esult" extends="StudentResult"> <result property="address.addrId" column="addr_id" /> <result property="address.street" column="street" /> <result property="address.city" column="city" /> <result property="address.state" column="state" /> <result property="address.zip" column="zip" /> <result property="address.country" column="country" /> </resultMap>
這樣id爲StudentResult的<resultMap>就擴展了id爲StudentResult的<resultMap>
如果你指向映射student表中的內容,你可以使用id爲StudentResult的<resultMap>
<select id="findStudentById" parameterType="int" resultMap="StudentResult"> SELECT * FROM STUDENTS WHERE STUD_ID=#{studId} </select>
如果你想要映射Student和Address表中的數據,你可以使用id爲StudentWithAddre***esult的<resultMap>
<select id="selectStudentWithAddress" parameterType="int" resultMap="StudentWithAddre***esult"> SELECT STUD_ID, NAME, EMAIL, PHONE, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON S.ADDR_ID=A.ADDR_ID WHERE STUD_ID=#{studId} </select>