You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot+Thymeleaf心愿单网站更新时price字段赋值错误求助

问题解决:更新心愿商品时价格字段赋值错误

核心问题分析

错误提示显示文本值Apple Watch Series 6被传入price字段,根源是JDBC模板执行SQL时的参数顺序与SQL语句占位符顺序完全不匹配:

你的SQL语句占位符顺序为:name → text → price → link → is_bought → id

UPDATE wish SET name = ?, text = ?, price = ?, link = ?,  is_bought = ? WHERE id = ?;

但调用jdbcTemplate.update时传入的参数顺序是:id → name → text → price → link → isBought,导致对应price的第三个占位符拿到了name的文本值,直接触发类型不匹配错误。

解决方案

1. 修正JDBC参数顺序

修改updateWish方法中jdbcTemplate.update的参数顺序,严格对应SQL占位符的顺序:

public void updateWish(int id, String name, String text, double price, String link, boolean isBought) {
    System.out.println("price: " + price);
    System.out.println("desc: " + text);
    System.out.println("name: " + name);
    System.out.println("link: " + link);
    System.out.println("isBought: " + isBought);
    String query = "UPDATE wish SET name = ?, text = ?, price = ?, link = ?,  is_bought = ? WHERE id = ?;";
    // 修正后参数顺序:name, text, price, link, isBought, id
    jdbcTemplate.update(query, name, text, price, link, isBought, id);
}

2. 恢复数据库price字段类型为decimal(10,2)

无需改成double类型,decimal在存储价格类数据时精度更可靠,不会出现浮点误差,改回原类型即可。

3. 优化前端表单输入体验(可选)

将price输入框类型从text改为number,限制步长避免非数值输入:

<input id="price" type="number" step="0.01" name="price" th:value="${wish.price}">

验证说明

修正参数顺序后重新测试,price字段会正确接收数值参数,不会再出现文本值赋值错误。

内容的提问来源于stack exchange,提问作者user24470272

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:12:15