背景:项目中需要用到mybatis的联表查询,问题出现在一对一关联查询上。参考:http://www.cnblogs.com/wucj/p/5148813.html
开始的配置情况:
<resultMap id="FullResultMap" type="com.schooldevice.domain.Problem" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="deviceid" property="deviceid" jdbcType="VARCHAR" />
<result column="reporterid" property="reporterid" jdbcType="INTEGER" />
<result column="probleminfo" property="probleminfo" jdbcType="VARCHAR" />
<result column="reporttime" property="reporttime" jdbcType="VARCHAR" />
<result column="endtime" property="endtime" jdbcType="VARCHAR" />
<result column="condi" property="condi" jdbcType="INTEGER" />
<association property="dev" javaType="com.schooldevice.domain.Dev">
<id property="id" column="d_id" jdbcType="INTEGER"/>
<result property="num" column="d_num" jdbcType="VARCHAR"/>
<result property="name" column="d_name" jdbcType="VARCHAR"/>
<result property="head" column="d_head" jdbcType="VARCHAR"/>
<result property="address" column="d_address" jdbcType="VARCHAR"/>
<result property="repairedtimes" column="d_repairedtimes" jdbcType="INTEGER"/>
<result property="endtime" column="d_endtime" jdbcType="VARCHAR"/>
<result property="type" column="d_type" jdbcType="INTEGER"/>
</association>
<association property="reporter" javaType="com.schooldevice.domain.User">
<id property="id" column="r_id" jdbcType="INTEGER"/>
<result property="email" column="r_email" jdbcType="VARCHAR"/>
<result property="password" column="r_password" jdbcType="VARCHAR"/>
<result property="nickname" column="r_nickname" jdbcType="VARCHAR"/>
<result property="head" column="r_head" jdbcType="VARCHAR"/>
<result property="condi" column="r_condi" jdbcType="VARCHAR"/>
<result property="time" column="r_time" jdbcType="VARCHAR"/>
</association>
</resultMap>
其中association中的column与数据库表中的column字段名不同,目的是区别开三张表中的重名字段。(例如asscoiation dev中column=”d_num",而在数据库表中是column=“num"),然而这样并不能成功联表查询成功。因为其他配置和网上教程都是一样的,怀疑问题就是出现在为了区别表字段而更改字段名这,于是做出修改:
<resultMap id="FullResultMap" type="com.schooldevice.domain.Problem" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="deviceid" property="deviceid" jdbcType="VARCHAR" />
<result column="reporterid" property="reporterid" jdbcType="INTEGER" />
<result column="probleminfo" property="probleminfo" jdbcType="VARCHAR" />
<result column="reporttime" property="reporttime" jdbcType="VARCHAR" />
<result column="endtime" property="endtime" jdbcType="VARCHAR" />
<result column="condi" property="condi" jdbcType="INTEGER" />
<association property="dev" javaType="com.schooldevice.domain.Dev">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="num" column="num" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="head" column="head" jdbcType="VARCHAR"/>
<result property="address" column="address" jdbcType="VARCHAR"/>
<result property="repairedtimes" column="repairedtimes" jdbcType="INTEGER"/>
<result property="endtime" column="endtime" jdbcType="VARCHAR"/>
<result property="type" column="type" jdbcType="INTEGER"/>
</association>
<association property="reporter" javaType="com.schooldevice.domain.User">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="email" column="email" jdbcType="VARCHAR"/>
<result property="password" column="password" jdbcType="VARCHAR"/>
<result property="nickname" column="nickname" jdbcType="VARCHAR"/>
<result property="head" column="head" jdbcType="VARCHAR"/>
<result property="condi" column="condi" jdbcType="VARCHAR"/>
<result property="time" column="time" jdbcType="VARCHAR"/>
</association>
</resultMap>
这样就成功查询出数据了。值得注意的是在一对多的联表查询时,我就是更改了collection中的column字段名却没有出现这种问题,实在是令人费解。
2017/5/30日更新:改成上面的方法之后虽然可以查出数据,但是在数据的准确性上出现了问题,几张表的字段相同的话数据会取第一个相同字段的值。显然不能满足我们的需求,这时做出这样的修改:
<resultMap id="FullResultMap" type="com.schooldevice.domain.Problem" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="deviceid" property="deviceid" jdbcType="VARCHAR" />
<result column="reporterid" property="reporterid" jdbcType="INTEGER" />
<result column="probleminfo" property="probleminfo" jdbcType="VARCHAR" />
<result column="reporttime" property="reporttime" jdbcType="VARCHAR" />
<result column="endtime" property="endtime" jdbcType="VARCHAR" />
<result column="condi" property="condi" jdbcType="INTEGER" />
<association property="dev" javaType="com.schooldevice.domain.Dev">
<id property="id" column="d_id" jdbcType="INTEGER"/>
<result property="num" column="d_num" jdbcType="VARCHAR"/>
<result property="name" column="d_name" jdbcType="VARCHAR"/>
<result property="head" column="d_head" jdbcType="VARCHAR"/>
<result property="address" column="d_address" jdbcType="VARCHAR"/>
<result property="repairedtimes" column="d_repairedtimes" jdbcType="INTEGER"/>
<result property="endtime" column="d_endtime" jdbcType="VARCHAR"/>
<result property="type" column="d_type" jdbcType="INTEGER"/>
</association>
<association property="reporter" javaType="com.schooldevice.domain.User">
<id property="id" column="r_id" jdbcType="INTEGER"/>
<result property="email" column="r_email" jdbcType="VARCHAR"/>
<result property="password" column="r_password" jdbcType="VARCHAR"/>
<result property="nickname" column="r_nickname" jdbcType="VARCHAR"/>
<result property="head" column="r_head" jdbcType="VARCHAR"/>
<result property="condi" column="r_condi" jdbcType="VARCHAR"/>
<result property="time" column="r_time" jdbcType="VARCHAR"/>
</association>
</resultMap>
查询语句:
<select id="findByReporterId" resultMap="FullResultMap" parameterType="java.lang.Integer" >
select
p.id,p.deviceid,p.reporterid,p.probleminfo,p.reporttime,p.endtime,p.condi,d.id d_id,d.num d_num,d.name d_name,d.head d_head,d.address d_address,d.repairedtimes d_repairedtimes,d.endtime d_endtime,d.type d_type,r.id r_id,r.email r_email,r.password r_password,r.nickname r_nickname,r.head r_head,r.condi r_condi,r.time r_time
from t_problem p
left join t_dev d on p.deviceid=d.num
left join t_user r on p.reporterid=r.id
where p.reporterid=#{reporterId,jdbcType=INTEGER}
order by p.reporttime desc
</select>
这样给表字段取别名就可以正常取数据了。核心就在于使联表的同名字段在查询时区别开来即查询语句里给字段取别名。
总结:当时写完博客后发现了同名字段取值问题,没有即使更新博客,导致博客在正确性上打了折扣,以后一定注意。