mybatis-plus saveOrUpdateBatch
The saveOrUpdateBatch method in MyBatis-Plus is used to save or update a batch of entities. It takes a list of entities as input and determines whether to save or update each entity based on whether it has a primary key value.
If an entity has a primary key value, it will be updated. If it does not have a primary key value, it will be saved.
The method signature is as follows:
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize)
where T is the entity class, entityList is the list of entities to save or update, and batchSize is the batch size of each save or update operation.
Example usage:
List<User> userList = new ArrayList<>();
userList.add(new User("John", 25));
userList.add(new User("Jane", 30));
userList.add(new User("Bob", 35));
boolean success = userService.saveOrUpdateBatch(userList, 2);
System.out.println(success);
In this example, we have a list of User entities that we want to save or update. We call the saveOrUpdateBatch method on the userService object, passing in the userList and a batch size of 2. The method returns a boolean indicating whether the operation was successful.
Note that the saveOrUpdateBatch method will generate a single SQL statement for each batch of entities, rather than one statement per entity. This can improve performance when saving or updating a large number of entities
原文地址: https://www.cveoy.top/t/topic/fdKq 著作权归作者所有。请勿转载和采集!