mybatis关联查询中 列名重复的解决办法
2020-07-26 本文已影响0人
harv9y
假如两个表中有相同的列名,mybatis在查询时只会保留一个列的值,另一个会被覆盖掉,得不到正确的值。
解决办法 ,为其中一个重复的列起别名
<select id="getInfoByUserId" parameterType="int" resultMap="workInfo">
select
wi.*,
w.work_id,
w.no,
w.type ,
w.content,
w.origin,
w.user_id,
w.create_time as w_create_time,
w.due_time,
w.due_time_alt,
w.finished as w_finished,
w.finish_time,
w.other
from
work w,
work_info wi
where w.work_id = wi.wid and w.user_id = #{id} and wi.status < 3
同时resultmap中也要改一下column的值
<resultMap extends="ResultMapWithBLOBs" id="workInfo" type="com.***.entity.WorkInfo">
<association property="work" javaType="Work">
<id column="work_id" jdbcType="INTEGER" property="workId" />
<result column="no" jdbcType="CHAR" property="no" />
<result column="type" jdbcType="INTEGER" property="type" />
<result column="origin" jdbcType="CHAR" property="origin" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<result column="w_create_time" jdbcType="DATE" property="createTime" />
<result column="due_time" jdbcType="DATE" property="dueTime" />
<result column="due_time_alt" jdbcType="CHAR" property="dueTimeAlt" />
<result column="w_finished" jdbcType="TINYINT" property="finished" />
<result column="finish_time" jdbcType="DATE" property="finishTime" />
<result column="other" jdbcType="CHAR" property="other" />
</association>
</resultMap>
参考:https://blog.csdn.net/jiang18238032891/article/details/88806168