spring jdbc 批量插入或者批量更新

  public int[] updateEventCountOfUser(final long uid, final Map eventsCountMap) {
        final String sql = "update credits_event set EVENT_COUNT = EVENT_COUNT + ? where LUID = ? and event_id= ?";
        int[] u = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                int eventId = eventsCountMap.keySet().iterator().next();
                ps.setInt(1, eventsCountMap.get(eventId));
                ps.setLong(2, uid);
                ps.setInt(3, eventId);
            }

            @Override
            public int getBatchSize() {
                return eventsCountMap.size();
            }
        });
        return u;
    }

你可能感兴趣的:(Thinking项目学习)