MyBatis两张表字段名相同, 会导致bean属性都映射为第一个表的列,
解决方法:
通过设置别名的方式让其产生区别,如
<select id="queryBySekillId" resultMap="successKilled"> select
sk.seckill_id "seckill_id",
sk.user_phone "user_phone",
sk.state "state",
sk.create_time "create_time",
s.seckill_id "seckill_id",
s.name "name",
s.number "number",
s.start_time "start_time",
s.end_time "end_time",
<strong>s.create_time "screate_time"</strong>
from success_killed sk
inner join seckill s
on sk.seckill_id = s.seckill_id
where sk.seckill_id = #{seckill_id} ; </select>
上 success_killed表和seckill表中有相同字段 create_time (id字段其实确实是相同的故在此忽略),通过将表seckill的create_time字段命名为screate_time用以区别。
<resultMap type="Seckill" id="seckill">
<id column="seckill_id" property="seckill_id"/>
<result column="name" property="name"/>
<result column="number" property="number"/>
<result column="start_time" property="start_time" />
<result column="end_time" property="end_time"/>
<result column="screate_time" property="create_time" />
</resultMap>
同时将 resultMap中该段的 column 属性该为 screate_time.故只要让查询出的字段名与column相同,它会通过映射找到相应属性进行封装。