Mybatis test条件表达式类型问题
记录一个使用mybatis时遇到的一个逆天bug,坑了我俩小时
mapper接口:
List<AirRasterDataVO> selectAirRasterDataByRegion(@Param("size") int size, @Param("provinceCode") String provinceCode, @Param("parentRegion") String parentRegion, @Param("region") String region, @Param("area") List<String> area, @Param("gridId") String gridId, @Param("gridCode") List<String> gridCode, @Param("index") List<String> index, @Param("time") String time, @Param("type") String type);
mapper接口中传递了一个type参数,调用时一直出现报错:
{"code": 400,"success": false,"data": {},"msg": "nested exception is org.apache.ibatis.exceptions.PersistenceException: \r\n### Error querying database. Cause: java.lang.NumberFormatException: For input string: \"w\"\r\n### Cause: java.lang.NumberFormatException: For input string: \"w\""
}
一直说我传的这个type,(“w”),类型转换有问题,一直要给我转成数字类型的,我找了好久,百思不得其解,最终看到我choose when的代码
<choose><when test="type=='y'">WHERE year_key=#{time} and province_code=#{provinceCode}</when><otherwise>WHERE update_time=#{time} and province_code=#{provinceCode}</otherwise></choose>
一开始我真看不出来这choose when有什么问题,就是很正常的字符串比较,直到我给**‘y’**加上toString,问题解决
<choose><when test="type=='y'.toString()">WHERE year_key=#{time} and province_code=#{provinceCode}</when><otherwise>WHERE update_time=#{time} and province_code=#{provinceCode}</otherwise>
</choose>