首页 > 解决方案 > 为什么我的 jdbcTemplate 查询没有返回任何行?

问题描述

我有一个用 spring-boot 构建的 API。请求将 JSON 发送到构建请求对象的端点。请求对象将其 Product 对象列表发送到一个方法,以更新 Product 中的 productDimensions 对象。

为了获得尺寸,我将 Sku 字符串和 Size 字符串发送到使用 jdbcTemplate 设置的名为 ProductRepository 的类(我认为)

该方法没有失败,但 SqlRowSet 返回 0 行,我无法弄清楚。请注意,Java 不是我的主要语言,所以我有点困惑。

我试过https://spring.io/guides/gs/relational-data-access/和其他几个 SO 链接

public class ProductRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public SqlRowSet findBySkuSize(String sku, String size) {
        return jdbcTemplate.queryForRowSet("SELECT * from PRODUCT_DIMENSIONS where SKU = '" + sku + "' and SIZE = '" + size + "'");
    }
}  

这就是我正在做的调用 ProductRepository

private ProductRepository productRepository;
//constructor
public FreightCalculationService(ProductRepository productRepository) 
{
    this.productRepository = productRepository;
}
private Obj Method(params){
  Obj obj = new Obj()
  SqlRowSet dataRows = productRepository.findBySkuSize(params);

我希望我的 H2 数据库中的数据在我的 SqlResultSet 中显示为一行,但是当我检查时,总行数为 0..

编辑:我的 jdbcTemplate 已填充,我已经清理了一些代码。

jdbcTemplatePropertys

编辑:当我在调试器中查看 SqlRowSet 对象时,我看到它不工作 [![在此处输入图像描述][2]][2] 调试控制台中没有引发错误。

[![[2]:https://i.stack.imgur.com/sSen0.png][2]][2]

所以看起来 Sql 语句有问题,当我将查询更改为 SELECT * FROM PRODUCT_DIMENSIONS 时,我得到 4 行,但是当我尝试易受 sql 注入影响的方法时,我什么也没得到,这可能是因为Java连接字符串的方式?我要查看准备好的陈述以查看是否有解决方法的任何人。

标签: javah2jdbctemplate

解决方案


推荐阅读