Bulk/Batch insert using JDBC template
According to my previous blog — Bulk/Batch insert using Spring Data JPA and Mysql database, we can understand that batch insert is not possible with JPA using Mysql as database. we have to either go with JdbcTemplate or query DSL-SQL.
In this blog, let’s try to implement the JDBC template
Maven Dependencies to be added
Let’s create an entity class tweets where we can store tweets
Let’s create a service class that inserts a batch of records into the tweet table using the JDBC Template. To do that follow the simple code below
Now if you run the above method, you will notice that the entire data gets inserted into the database using a single query as shown below.
To see the below log you have to add the maven dependency — datasource-proxy-spring-boot-starter with properties — logging.level.net.ttddyy.dsproxy.listener=debug. This helps you to see the queries that are actually performed on our database.
Advantages of using JDBC Template
Jdbc will be faster than query-DSL SQL and Spring Data JPA
Conclusion
we can able to batch insert the records using the JDBC template on the MySQL database. this is cool, right?
For GitHub code click here.