jdbctemplate 批量插入

public void batchImport(List<Map<String, Object>> list) {
        String sql = "insert into table1(field1, field2) values(?,?)";
int[] types = new int[2];
        types[0] = Types.VARCHAR;
        types[1] = Types.NUMERIC;

        List<Object[]> paramsArr = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> item = list.get(i);
            Object[] params = new Object[2];
            params[0] = item.get("param0");
params[1] = item.get("param1");
paramsArr.add(params); if (i == tables.size() - 1 || (i != 0 && i % 100 == 0)) {//每100条执行一次 jdbcTemplate.batchUpdate(sql, paramsArr, types); paramsArr = new ArrayList<Object[]>(); } } }

猜你喜欢

转载自www.cnblogs.com/white-knight/p/9429374.html
今日推荐