问题:用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:[email protected](“expertGoodAtId”)与@Param(value=”expertGoodAtId”)是一个意思
2: GROUP_CONCAT(vm.models_name)是mysql对查询2条相同数据合并成一行显示,并且必须根据某个字段分组,统计时使用distinct排除重复数据
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/11210.html