MyBatis 多表聯合查詢,兩張表中欄位重複時,在配置檔案中,sql語句聯合查詢時使用欄位別名,resultMap中對應的column屬性使用相應的別名:
<resultMap type="Vote" id="VoteMapper"> <id column="id" property="id"/> <result column="theme" property="theme"/> <result column="isuse" property="isuse"/> <collection property="Options" ofType="VoteOption" > <id column="vid" property="id"/> <result column="vote_id" property="voteId"/> <result column="option" property="option"/> <result column="poll" property="poll"/> </collection> </resultMap> <select id="findById" parameterType="int" resultMap="VoteMapper"> SELECT v.*,vt.id vid,vt.vote_id,vt.option,vt.poll FROM vote v join vote_option vt on v.id=vt.vote_id WHERE v.id=#{id} </select>