How to perform batch update in Spring with a list of maps?

前端 未结 4 1870
旧巷少年郎
旧巷少年郎 2021-01-04 06:51

New to Spring, I am trying to insert a List> into a table. Until now I have been using the SqlParameterSource for b

4条回答
  •  耶瑟儿~
    2021-01-04 07:31

    As per Spring NamedParameterJDBCTemplate docs, found here, this method can be used for batch updating with maps.

    int[] batchUpdate(String sql, Map[] batchValues)

    The real challange was to a get an array of Map from a corresponding List>. I used the following code to get the array and perform the batch update.

    public static Map[] getArrayData(List> list){
            @SuppressWarnings("unchecked")
            Map[] maps = new HashMap[list.size()];
    
            Iterator> iterator = list.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                Map map = (Map) iterator
                        .next();
                maps[i++] = map;
            }
    
            return maps;
        }
    

提交回复
热议问题