Cause: java.sql.SQLException: Bad format for BigDecimal '1,087.50' in column 18.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_29774479/article/details/89518337

问题场景:

订单信息导出时,在选择某一时间段数据时会报错,但是一般不报错。

错误信息

error:org.springframework.dao.TransientDataAccessResourceException: Error attempting to get column 'totalRebate' from result set. Cause: java.sql.SQLException: Bad format for BigDecimal '1,087.50' in column 18. ; SQL []; Bad format for BigDecimal '1,087.50' in column 18.; nested exception is java.sql.SQLException: Bad format for BigDecimal '1,087.50' in column 18. StackTrace:org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:108) message:Error attempting to get column 'totalRebate' from result set. Cause: java.sql.SQLException: Bad format for BigDecimal '1,087.50' in column 18. ; SQL []; Bad format for BigDecimal '1,087.50' in column 18.; nested exception is java.sql.SQLException: Bad format for BigDecimal '1,087.50' in column 18.

错误原因分析:

查看报错信息为SQLException,但是本地运行sql并没有问题。于是进一步查看报错信息Bad format for BigDecimal分析可能是类型没有对应上导致数字转换出错,但是查看mybatis转化的对象字段类型是bigdecimal,与数据库的字段类型一致。在仔细看报错信息‘1,087.50’,第一眼感觉有点怪怪的但是没有发现问题。只好找到对用的sql进行排查,在看sql的过程中看到了使用了FORMAT作为四舍五入的函数。

查询FORMAT函数使用方法后找到问题的关键是:FORMAT函数会四舍五入之后返回一个以逗号分隔的字符串,所以导致出现了‘1,087.50’这样的数字,乍一看没有问题,事实上这个已经是字符串了,这才导致了mybatis转换时出现了异常。一般不报错因为这个字段一般比较小,导致这个字段不一定含有逗号,只要没有逗号,最终都会进行类型转换。

解决办法:

把FORMAT函数替换成ROUND即可

FORMAT函数的使用:https://www.yiibai.com/mysql/mysql_function_format.html

猜你喜欢

转载自blog.csdn.net/sinat_29774479/article/details/89518337