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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM