开发者

Spring queryForLong - Loss of precision

开发者 https://www.devze.com 2022-12-16 15:34 出处:网络
I have a Spring application running against a MySql database.I have a sales table like: +-------------------+------------------------------+------+-----+---------+-------+

I have a Spring application running against a MySql database. I have a sales table like:

+-------------------+------------------------------+------+-----+---------+-------+
| Field             | Type                         | Null | Key | Default | Extra |
+-------------------+------------------------------+------+-----+---------+-------+
| Price             | decimal(19,4)                | NO   |     | NULL    |       |
| ItemId            | int(10) unsigned             | NO   |     | NULL    |       |
+-------------------+------------------------------+-----开发者_如何学编程-+-----+---------+-------+

In my Spring class, I have the following:

long price = getSimpleJdbcTemplate().queryForLong("SELECT price FROM sales WHERE itemID = :itemID", params);
logger.info("price: " + price + ");

However, this is returning a price of 1, even if this query directly on the database returns 0.8500. How do I avoid this loss of precision from the decimal in the database to the long in my code?


long does not support decimal places because it is an integer type. Have you tried queryForObject(sql, BigDecimal.class, params)?

0

精彩评论

暂无评论...
验证码 换一张
取 消