MyBatis兩張表中存在相同欄位名,聯表查詢時的衝突解決辦法
阿新 • • 發佈:2019-02-16
1. 經常使用類似left join做查詢,偶爾遇到兩張表的欄位名相同(即column名字一致),此時可以在mybatis中這樣配置
例子:
table1表和table2表都包含一個username欄位,但實體中欄位名是不同的,當聯表查詢時,可以起一個別名t2_username做轉換,然後通過實體接收即可。<select id="demo" resultMap="DemoResultMap"> SELECT DISTINCT t1.username, t1.password, t2.username "t2_username", t2.age FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id WHERE t1.age = #{age,jdbcType=VARCHAR} </select> <resultMap id="DemoResultMap" type="com.demo.vo.UserVo" > <id column="id" property="id" jdbcType="VARCHAR"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> <result column="t2_username" property="t2Username" jdbcType="VARCHAR"/> <result column="age" property="age" jdbcType="VARCHAR"/> </resultMap>
參考: