Jpa Batch Insert
JPA Batch Insert
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public int saveAll(List<ImageInfo> imageInfoList) {
int batchCount = 0;
int totalSize = imageInfoList.size();
if (totalSize <= BATCH_SIZE) {
insert(imageInfoList, batchCount);
return batchCount;
}
List<ImageInfo> subList = new ArrayList<>();
for (int i = 0; i < totalSize; i++) {
subList.add(imageInfoList.get(i));
if ((i + 1) % BATCH_SIZE == 0) {
batchCount = insert(subList, batchCount);
}
}
return batchCount;
}
private int insert(List<ImageInfo> request, int batchCount) {
jdbcTemplate.batchUpdate(
"INSERT INTO IMAGE_INFO("
+ "IS_PRODUCT,"
+ "LINK,"
+ "ORIGINAL,"
+ "ORIGINAL_HEIGHT,"
+ "ORIGINAL_WIDTH,"
+ "POSITION,"
+ "RELATED_CONTENT_ID,"
+ "SERPAPI_RELATED_CONTENT_LINK,"
+ "SOURCE,"
+ "SOURCE_LOGO,"
+ "THUMBNAIL,"
+ "TITLE) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
request, BATCH_SIZE, (ps, argument) -> {
ps.setBoolean(1, argument.getIsProduct());
ps.setString(2, argument.getLink());
ps.setString(3, argument.getOriginal());
ps.setInt(4, argument.getOriginalHeight());
ps.setInt(5, argument.getOriginalWidth());
ps.setLong(6, argument.getPosition());
ps.setString(7, argument.getRelatedContentId());
ps.setString(8, argument.getSerpapiRelatedContentLink());
ps.setString(9, argument.getSource());
ps.setString(10, argument.getSourceLogo());
ps.setString(11, argument.getThumbnail());
ps.setString(12, argument.getTitle());
});
request.clear();
batchCount++;
return batchCount;
}
This post is licensed under CC BY 4.0 by the author.