1. 程式人生 > >mybatis連表查詢不能查詢到關聯物件的值

mybatis連表查詢不能查詢到關聯物件的值

背景:專案中需要用到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>

這樣給表字段取別名就可以正常取資料了。核心就在於使聯表的同名欄位在查詢時區別開來即查詢語句裡給欄位取別名。

總結:當時寫完部落格後發現了同名欄位取值問題,沒有即使更新部落格,導致部落格在正確性上打了折扣,以後一定注意。