MyBatis 多表联合查询,字段重复的解决方法

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>

 

posted @ 2016-11-26 17:02  不知为何就叫呵呵  阅读(14200)  评论(1编辑  收藏  举报