mybatis关于级联查询结果集嵌套映射对象非列表的处理问题


工作中遇到这么一个问题,嵌套查询,返回json的时候,作为属性,deviceFields是一个device中的一个对象属性,在json返回的时候想要得到的应该是deviceFields:{ 具体属性}

但是实际上我发现我返回的确实  deviceFields:[{ 具体属性}],多了个中括号

这明显是把查询的结果作为了一个集合,而没有映射为一个对象,仔细查看,原来是在association上应该添加上javaType的对应的对象路径,而我把这个给漏掉了。下面是正确的

<resultMap type="map" id="getDeviceListResult">

  <result property="deviceId" column="deviceId"/>
  <association property="deviceFields" column="deviceId" javaType="com.proAAA.cloud.entity.DeviceFields" select="com.proAAA.cloud.dao.DeviceFieldsMapper.selectByDeviceId"></association>
  <collection property="pointDataList"  column="deviceId" select="selectxxxxDevicePointDataList"></collection>
  </resultMap> 

 <select id="getDeviceListByUserId" parameterType="map" resultMap="getDeviceListResult">

SELECT d.id deviceId,d.device_no deviceNo,d.gateway_uid uid,d.ext_addr extAddr,d.endpoint 'endpoint',d.device_name deviceName,
  device_type 'type',d.sub_device_type subType,d.product_id productId,d.company_id companyId,d.model model,d.version 'version',
  d.start_time startTime,d.del_flag delFlag 
  FROM d_device d, d_device_fields f,d_device_user_bind dub
  WHERE f.del_flag=0
  AND d.del_flag=0
  AND dub.del_flag=0
  AND d.company_id=#{companyId} 
  AND d.`product_id`=#{productId}
  AND d.id=f.device_id
  AND d.gateway_uid = dub.uid
  AND dub.user_id=#{userId}
  <if test="status != null">
      AND f.`online`=#{status}
      </if>
      <if test="rent != null and rent == 0">
      AND f.`rent` IS NULL
      </if>
      <if test="rent != null and rent != 0">
      AND f.`rent` = #{rent}
      </if>
      <if test="content != null">
      AND ( d.device_name like #{content}  OR d.model like #{content})
      </if>
            <if test="pointStatus != null">
      AND EXISTS (
SELECT 1
FROM d_device_point_data dpd
WHERE dpd.device_id = d.id
      AND dpd.del_flag = 0
      AND dpd.point_index = 23
      AND dpd.value = #{pointStatus}
)
      </if> order by f.online desc
  </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值