工作中遇到这么一个问题,嵌套查询,返回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.xxx.cloud.entity.DeviceFields" select="com.xxx.cloud.dao.DeviceFieldsMapper.selectByDeviceId"></association>
<collection property="pointDataList" column="deviceId" select="selectxxxDevicePointDataList"></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>