无效参数 1:参数索引超出范围。 Java Spring
Invalid parameter 1: Parameter index is out of range. Java Spring
我有一个 Spring 引导应用程序,它从 MQ 中获取 XML 并将 XML 映射到哈希映射中,并且 运行s 是一个更新查询基于XML 中的内容。我在使用哈希映射时遇到问题。我可以 运行 查询(通过对值进行硬编码),它会相应地更新 table ,但是当我尝试使用哈希映射传递给查询(具有相同的值)时,我得到这个错误说 "Invalid parameter 1: Parameter index is out of range." 我无法弄清楚为什么散列映射不起作用,因为当我将它输出到屏幕时它似乎正确映射。任何帮助表示赞赏! (对格式感到抱歉。无法弄清楚如何将 xml 和代码分成两页。)
@Repository
public class OrderChargeRepository extends JdbcDaoSupport {
@Autowired
private Properties queries;
@Autowired
public OrderChargeRepository(DataSource dataSource) {
super();
this.setDataSource(dataSource);
}
public void update(SupplierPayments supplierPayments) {
String sqlUpdate = queries.getProperty("updateTable");
for (SupplierPayment supplierPayment : supplierPayments.getSupplierPayments()) {
for (Invoices invoices : supplierPayment.getSupplierPayment()) {
for (Invoice invoice : invoices.getInvoices()) {
for (InvoiceLines invoiceLines : invoice.getInvoice()) {
for (InvoiceLine invoiceLine : invoiceLines.getInvoiceLines()) {
Map<String,Object> parms = new HashMap<String,Object>();
parms.put("paymentId", supplierPayment.getID());
parms.put("paymentDate", supplierPayment.getPaymentDate());
parms.put("invoiceLineAmount", invoiceLine.getAmount());
parms.put("referenceNumber", invoice.getreferenceNumber());
parms.put("scac", supplierPayment.getSCAC());
parms.put("supplierInvoiceReferenceNumber", invoice.getSupplierInvoiceReferenceNumber());
this.getJdbcTemplate().update(sqlUpdate, parms);
}
}
}
}
}
}
}
//properties.xml
//Contains query property
<util:properties id="queries">
<prop key="updateTable">
<![CDATA[
UPDATE ALI.TORDER_CHARGE
SET CHK_NBR = :paymentId
, PAY_D = DATE(:paymentDate)
, PAY_A = :invoiceLineAmount
, LST_UPD_S = CURRENT TIMESTAMP
, LST_UPD_UID = 'BATCH'
WHERE ORD_I = (Select ORD_I from aaa.torder where ORD_NBR_CH = :referenceNumber)
AND CUS_C = :scac
AND IVC_REF_NBR = :supplierInvoiceReferenceNumber
]]>
</prop>
</util:properties>
</beans>
getJdbcTemplate()
JdbcDaoSupport returns 一个 JdbcTemplate。所以你调用的方法是 JdbcTemplate.update(String sql, Object... args)
,因此
- 查询不应包含命名参数,而仅包含占位符 (
?
)
- 整个地图用作查询的唯一第一个参数
您需要 NamedParameterJdbcTemplate 才能实现您想要的目标。只需将 "classic" JdbcTemplate 包装到 NamedParameterJdbcTemplate 中,它应该可以正常工作。
我有一个 Spring 引导应用程序,它从 MQ 中获取 XML 并将 XML 映射到哈希映射中,并且 运行s 是一个更新查询基于XML 中的内容。我在使用哈希映射时遇到问题。我可以 运行 查询(通过对值进行硬编码),它会相应地更新 table ,但是当我尝试使用哈希映射传递给查询(具有相同的值)时,我得到这个错误说 "Invalid parameter 1: Parameter index is out of range." 我无法弄清楚为什么散列映射不起作用,因为当我将它输出到屏幕时它似乎正确映射。任何帮助表示赞赏! (对格式感到抱歉。无法弄清楚如何将 xml 和代码分成两页。)
@Repository
public class OrderChargeRepository extends JdbcDaoSupport {
@Autowired
private Properties queries;
@Autowired
public OrderChargeRepository(DataSource dataSource) {
super();
this.setDataSource(dataSource);
}
public void update(SupplierPayments supplierPayments) {
String sqlUpdate = queries.getProperty("updateTable");
for (SupplierPayment supplierPayment : supplierPayments.getSupplierPayments()) {
for (Invoices invoices : supplierPayment.getSupplierPayment()) {
for (Invoice invoice : invoices.getInvoices()) {
for (InvoiceLines invoiceLines : invoice.getInvoice()) {
for (InvoiceLine invoiceLine : invoiceLines.getInvoiceLines()) {
Map<String,Object> parms = new HashMap<String,Object>();
parms.put("paymentId", supplierPayment.getID());
parms.put("paymentDate", supplierPayment.getPaymentDate());
parms.put("invoiceLineAmount", invoiceLine.getAmount());
parms.put("referenceNumber", invoice.getreferenceNumber());
parms.put("scac", supplierPayment.getSCAC());
parms.put("supplierInvoiceReferenceNumber", invoice.getSupplierInvoiceReferenceNumber());
this.getJdbcTemplate().update(sqlUpdate, parms);
}
}
}
}
}
}
}
//properties.xml
//Contains query property
<util:properties id="queries">
<prop key="updateTable">
<![CDATA[
UPDATE ALI.TORDER_CHARGE
SET CHK_NBR = :paymentId
, PAY_D = DATE(:paymentDate)
, PAY_A = :invoiceLineAmount
, LST_UPD_S = CURRENT TIMESTAMP
, LST_UPD_UID = 'BATCH'
WHERE ORD_I = (Select ORD_I from aaa.torder where ORD_NBR_CH = :referenceNumber)
AND CUS_C = :scac
AND IVC_REF_NBR = :supplierInvoiceReferenceNumber
]]>
</prop>
</util:properties>
</beans>
getJdbcTemplate()
JdbcDaoSupport returns 一个 JdbcTemplate。所以你调用的方法是 JdbcTemplate.update(String sql, Object... args)
,因此
- 查询不应包含命名参数,而仅包含占位符 (
?
) - 整个地图用作查询的唯一第一个参数
您需要 NamedParameterJdbcTemplate 才能实现您想要的目标。只需将 "classic" JdbcTemplate 包装到 NamedParameterJdbcTemplate 中,它应该可以正常工作。