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>