問題:用mybatis查詢時傳入一個Long參數且進行判斷時報:There is no getter for property named 'expertGoodAtId' in 'class java.lang.Long'
原始xml檔案:
<pre name="code" class="html"><!-- App專家接口查詢:根據搜尋條件查詢,按專家id分組,按專家分數倒叙排序 -->
<select id="expertByScoreOrderQuery" parameterType="java.lang.Long" resultType="java.util.Map">
select ex.id,ex.expert_name AS expertName,ex.phone,ex.header_url AS headerUrl,ex.longitude,ex.latitude,ee.score,
GROUP_CONCAT(vm.models_name)AS expertGoodAt
from ykat_expert ex
LEFT JOIN ykat_expert_evaluate ee ON ex.id=ee.expert_id
LEFT JOIN ykat_expert_models em ON ee.expert_id=em.expert_id
LEFT JOIN ykat_vehicle_models vm ON em.vehicle_models_id=vm.id
where 1=1 and ex.is_del=1
<if test="expertGoodAtId!=null and expertGoodAtId!='' ">
and vm.id=#{expertGoodAtId}
</if>
GROUP BY ex.id
ORDER BY ee.score desc
</select>
解決方法:
方法一:無論什麼參數均使用“_parameter”
<!-- App專家接口查詢:根據搜尋條件查詢,按專家id分組,按專家分數倒叙排序 -->
<select id="expertByScoreOrderQuery" parameterType="java.lang.Long" resultType="java.util.Map">
select ex.id,ex.expert_name AS expertName,ex.phone,ex.header_url AS headerUrl,ex.longitude,ex.latitude,ee.score,
GROUP_CONCAT(vm.models_name)AS expertGoodAt
from ykat_expert ex
LEFT JOIN ykat_expert_evaluate ee ON ex.id=ee.expert_id
LEFT JOIN ykat_expert_models em ON ee.expert_id=em.expert_id
LEFT JOIN ykat_vehicle_models vm ON em.vehicle_models_id=vm.id
where 1=1 and ex.is_del=1
<if test="_parameter!=null and _parameter!='' ">
and vm.id=#{expertGoodAtId}
</if>
GROUP BY ex.id
ORDER BY ee.score desc
</select>
方法二:dao接口參數添加注釋,說明是對應哪個參數名(實際開發中常用方法)
/**
* @descript:App查詢專家:根據查詢條件,按專家分數倒叙排序
* @param expertGoodAtId 專家擅長車輛id
* @return
*/
List<Map<String, Object>> expertByScoreOrderQuery(@Param(value="expertGoodAtId")Long expertGoodAtId);
原因分析
Mybatis預設采用ONGL解析參數,是以會自動采用對象樹的形式取long.num值引起報錯
備注:
1:mybaties中注解value可以省略不寫如@Param("expertGoodAtId")與@Param(value="expertGoodAtId")是一個意思
2: GROUP_CONCAT(vm.models_name)是mysql對查詢2條相同資料合并成一行顯示,并且必須根據某個字段分組,統計時使用distinct排除重複資料