Preparation
First, we need to ensure the following:
- You have installed the MySQL database and can connect normally.
- You have configured the MyBatis environment and can successfully execute a single insert statement.
Database table preparation
To demonstrate the process of bulk insert, we create a users
table named with the following fields:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100)
);
MyBatis mapping file
We need to write a MyBatis mapping file to define the SQL statement for the insert operation. In this example, we will use a mapping file in XML format.
First, create a UserMapper.xml
file called and add the following content to it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.UserMapper">
<insert id="insertBatch" parameterType="java.util.List">
INSERT INTO users (name, email)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.name}, #{item.email})
</foreach>
</insert>
</mapper>
In the code above, we have defined an insertBatch
insert statement called . It takes a java.util.List
type parameter where each element is an User
object. We used <foreach>
labels to loop through the list and generate the corresponding insert statement.
java code
Next, we need to use MyBatis to perform batch insert operations in Java code. First, we need to create a User
class to represent a user in the database:
public class User {
private String name;
private String email;
// 省略构造函数和getter/setter方法
}
We can then write an UserMapper
interface to define methods for bulk insert operations:
public interface UserMapper {
void insertBatch(List<User> users);
}
Finally, in our Java code, we need to use SqlSessionFactory
and SqlSession
to perform bulk insert operations. Here is a simple example:
String resource = "path/to/your/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper userMapper = session.getMapper(UserMapper.class);
List<User> users = new ArrayList<>();
users.add(new User("John", "john@example
.com"));
users.add(new User("Alice", "[email protected]"));
userMapper.insertBatch(users);
session.commit();
}
In the code above, we first SqlSessionFactoryBuilder
construct an SqlSessionFactory
instance with , and then use it to create a SqlSession
. Next, we get UserMapper
an instance of the interface and create a list containing the user data to be inserted. Finally, we call insertBatch
the method to perform the bulk insert, and call the method to commit the transaction after the insert is complete commit
.
run code
Now, we have completed all the preparations. Running this code, MyBatis will batch insert our user data into users
tables in the MySQL database.
Summarize
In this article, we learned how to use MyBatis to implement MySQL batch insert operations. We first prepared the database tables and MyBatis mapping files, and then wrote Java code to perform batch insert operations. By using the batch insert function of MyBatis, we can significantly improve the performance and efficiency of inserting large amounts of data.
Hope this blog can help you, thanks for reading! If you have any problems or questions, feel free to ask.