gpt4 book ai didi

java - 如何在 Spring 中使用 map 列表执行批量更新?

转载 作者:IT老高 更新时间:2023-10-28 13:57:47 28 4
gpt4 key购买 nike

Spring 新手,我正在尝试插入 List<Map<String, Object>>成一张 table 。直到现在我一直在使用SqlParameterSource用于批量更新,当向它们提供 java bean 时它工作正常。像这样的:

    @Autowired
private NamedParameterJDBCTemplate v2_template;

public int[] bulkInsertIntoSiteTable(List<SiteBean> list){
SqlParameterSource[] batch = SqlParameterSourceUtils
.createBatch(list.toArray());
int[] updateCounts = v2_template
.batchUpdate(
"insert into sitestatus (website, status, createdby) values (:website, :status, :username)",
batch);

return updateCounts;

}

但是,我尝试了用 map 列表代替 bean 的相同技术,但失败了(确实如此)。

public int[] bulkInsertIntoSiteTable(List<Map<String, Object>> list){
SqlParameterSource[] batch = SqlParameterSourceUtils
.createBatch(list.toArray());
int[] updateCounts = v2_template
.batchUpdate(
"insert into sitestatus (website, status, createdby) values (:website, :status, :username)",
batch);

return updateCounts;

}

以上代码失败,异常如下:

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'website': Invalid property 'website' of bean class [org.springframework.util.LinkedCaseInsensitiveMap]: Bean property 'website' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.jdbc.core.namedparam.NamedParameterUtils.buildValueArray(NamedParameterUtils.java:322)
at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils$1.setValues(NamedParameterBatchUpdateUtils.java:45)
at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:893)
at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:1)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:884)
at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(NamedParameterBatchUpdateUtils.java:40)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(NamedParameterJdbcTemplate.java:303)
at tester.utitlies.dao.VersionTwoDao.bulkInsertIntoSites(VersionTwoDao.java:21)
at tester.utitlies.runner.Main.main(Main.java:28)

它失败了,因为它认为列表是一批 bean,我猜。我找不到在 Spring 中使用 map 列表 和使用 NamedParameterJDBCTemplate 执行批量更新的方法.请指教。

最佳答案

根据 Spring NamedParameterJDBCTemplate文档,找到 here ,此方法可用于使用 map 进行批量更新。

int[] batchUpdate(String sql, Map<String,?>[] batchValues)

真正的挑战是获得 Map<String, Object> 的数组来自相应的List<Map<String, Object>> .我使用以下代码获取数组并执行批量更新。

public static Map<String, Object>[] getArrayData(List<Map<String, Object>> list){
@SuppressWarnings("unchecked")
Map<String, Object>[] maps = new HashMap[list.size()];

Iterator<Map<String, Object>> iterator = list.iterator();
int i = 0;
while (iterator.hasNext()) {
Map<java.lang.String, java.lang.Object> map = (Map<java.lang.String, java.lang.Object>) iterator
.next();
maps[i++] = map;
}

return maps;
}

关于java - 如何在 Spring 中使用 map 列表执行批量更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17422674/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com