Jdbc를 통한 Bulk Insert

2021-09-30
  • Java
  • SpringBoot

Jdbc를 통한 Bulk Insert


@Repository
@RequiredArgsConstructor
public class someJdbcRepository {

    private final JdbcTemplate jdbcTemplate;

    public int[] bulkInsert(List<SomeEntity> someEntities) {
        return this.jdbcTemplate.batchUpdate(
            "insert into some_table (some_value) values (?)",
            new BatchPreparedStatement() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    SomeEntity someEntity = someEntities.get(i);
                    ps.setLong(1, someEntity.getSomeValue(i));
                }

                @Override
                public int getBatchSize() {
                    return someEntities.size();
            });
    }
}
Profile picture

2yeseul

트리플에서 백엔드 개발을 맡고 있습니다. 무한 삽질을 기록합니다. ⚒️