@GetMapping("/generateOldCarModelRanking")
@ApiOperation("老旧车车型排名")
public Result generateOldCarModelRanking(@RequestParam(value = "year")String year,@RequestParam(value = "isCommercial")boolean isCommercial ,@RequestParam(value = "fuelType")String fuelType,@RequestParam(value = "pageNum")int pageNum,@RequestParam(value = "pageSize")int pageSize){
log.info("老旧车车型排名,isCommercial:{},fuelType:{},pageNum:{},pageSize:{}",isCommercial,fuelType,pageNum,pageSize);
return vehicleBaseInfoService.generateOldCarModelRanking(year,isCommercial,fuelType,pageNum,pageSize);
}
这是我的接口,注意参数
fuelType 是String类型的
我来测试接口:
然后看见idea报错:
Error querying database. Cause: java.lang.NumberFormatException: For input string: "A"
Cause: java.lang.NumberFormatException: For input string: "A"
看看我的sql,其中有一句
<if test="fuelType != null and fuelType != ''">
and vbi.FUEL_TYPE = #{fuelType}
<if test="fuelType == 'A' and year != null and year != ''">
and TIMESTAMPDIFF(YEAR,vbi.REGISTER_DATE,'${year}-12-31')+1 > 8
</if>
<if test="fuelType == 'B' and year != null and year != ''">
and TIMESTAMPDIFF(YEAR,vbi.REGISTER_DATE,'${year}-12-31')+1 > 5
</if>
</if>
错误原因
我将String类型的fuelType和Char类型的'A'进行了比较。mybatis认为拿一个字符串(String)和一个字符(char)比较,也会报错
解决办法
转成字符串:'A'.toString()
<if test="fuelType != null and fuelType != ''">
and vbi.FUEL_TYPE = #{fuelType}
<if test="fuelType == 'A'.toString() and year != null and year != ''">
and TIMESTAMPDIFF(YEAR,vbi.REGISTER_DATE,'${year}-12-31')+1 > 8
</if>
<if test="fuelType == 'B'.toString() and year != null and year != ''">
and TIMESTAMPDIFF(YEAR,vbi.REGISTER_DATE,'${year}-12-31')+1 > 5
</if>
</if>
参考:java - mybatis参数格式化异常:NumberFormatException: For input string:"xx" - 呆萌的程序猿 - SegmentFault 思否