MyBatis Plus Insert Method Guide
The insert method in MyBatis Plus is used to insert data into the database.
When using the insert method, it is necessary to pass an entity object as a parameter, which contains the data to be inserted. Based on the mapping relationship in the annotations or configuration files of the entity class, MyBatis Plus will automatically map the attributes in the entity object to the fields in the database table.
The example code is provided below:
User user = new User();
user.setName("John");
user.setAge(25);
user.setEmail("john@example.com");
int rows = userMapper.insert(user);
if (rows > 0) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
}
In the above example, User is an entity class, and userMapper is a Mapper interface generated by MyBatis Plus. By calling the insert method of userMapper, the user object is inserted into the database. The return value is the number of rows inserted.
Note: When using the insert method, make sure that the fields of the database table correspond to the attributes of the entity class one by one, and that the types of the attributes in the entity class match the fields of the database table. Additionally, if the primary key of the database table is auto-incremented, the primary key value will be automatically written back to the entity class object after inserting the data.