加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何使用地图列表在Spring中执行批量更新?

发布时间:2020-12-14 05:26:56 所属栏目:Java 来源:网络整理
导读:我刚刚到 Spring,我试图插入一个List Map String,Object进桌子到目前为止,我一直在使用SqlParameterSource进行批量更新,当它提供了一个java bean时,这个工作正常.这样的事情 @Autowired private NamedParameterJDBCTemplate v2_template; public int[] bulkI
我刚刚到 Spring,我试图插入一个List< Map< String,Object>>进桌子到目前为止,我一直在使用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;

        }

然而,我尝试使用与地图列表相同的技术来代替一个bean,但它失败了(正确地).

public int[] bulkInsertIntoSiteTable(List<Map<String,Object>> list){
        SqlParameterSource[] batch = SqlParameterSourceUtils
                .createBatch(list.toArray());
        int[] updateCounts = v2_template
                .batchUpdate(
                        "insert into sitestatus (website,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)

它失败了,因为它认为列表是一批豆,我猜.我找不到一种在Spring中执行批量更新的方法,其中包含地图列表,并使用NamedParameterJDBCTemplate.请指教.

解决方法

根据Spring NamedParameterJDBCTemplate文档,找到 here,此方法可用于使用地图批量更新.

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;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读