Reputation: 25
I'm trying to perform bulk insertion/ update operation for the very first time. I'm using Mybatis Annotaions (Mappers) to carry out database related operations.
I have a @Param which is List<Map<String, String>> recordList
and looks something like:
[{record_no=1, first_name="Alpha", last_name="Tester", age=23, gender="female"},
{record_no=2, first_name="Beta", last_name="Tester", age=21, gender="male"}]
Mapper would have:
@Insert({"<script>",
"insert into demo_record (first_name, last_name, age, gender, record_no) values ",
"<foreach collection='recordList' item='record' index='index' open='(' separator = '),
( close=')' > <Here I do not know how to pass/ access Map's values.
Would it be just #{first_name} or something else?>
</foreach>",
"</script>"})
void doBatchInsert(@Param("recordList") List<Map<String, String>> recordList);
**** I'm not sure what exactly open='(' separator = '), ( close=')'
does as well. I'd greatly appretiate it If someone can shed a light on it. ****
I'm following @Repository -> @Service and then @Autowire RecordService.java
for example Controller class has:
@Autowired
private RecordService recordservice;
.
.
recordservice.doBatchInsert(recordList);
.
.
return "success";
Also, how many bulk insertion of records above approch would be able to hadle? (records could range from 5000 to 50000)
P.S. :- This is also my first time posting a question here. Please go easy on me, if I've failed to post the question correctly! Thanks a lot!! :)
Upvotes: 0
Views: 16100
Reputation: 991
Take a look at this FAQ for a recommendation on coding batch inserts: https://github.com/mybatis/mybatis-3/wiki/FAQ#how-do-i-code-a-batch-insert
What you are showing is not a batch - it is one giant insert statement. If you truly will have 5000 to 50000 records, then you will hit the limit of parameters allowed for a JDBC prepared statement very quickly. It would be better to use a true JDBC batch as described in the FAQ.
Upvotes: 4