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