mybatisplus saveorupdatebatch 判断字段不是主键怎么办
时间: 2023-07-08 09:20:15 浏览: 125
如果你在使用 MyBatisPlus 的 saveOrUpdateBatch 方法时,想要判断一个字段不是主键,可以使用 `set` 方法来设置更新字段,如下所示:
```java
List<User> userList = new ArrayList<>();
// 假设 User 中有 id、name 和 age 三个字段,其中 id 是主键
User user1 = new User();
user1.setId(1);
user1.setName("张三");
user1.setAge(20);
userList.add(user1);
User user2 = new User();
user2.setName("李四");
user2.setAge(25);
userList.add(user2);
// 判断 name 不是主键,使用 set 方法更新
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(User::getName, user2.getName());
updateWrapper.eq(User::getAge, user2.getAge());
// 执行 saveOrUpdateBatch 方法
userService.saveOrUpdateBatch(userList, updateWrapper);
```
这样,如果在执行 `saveOrUpdateBatch` 时,发现 `User` 对象中的 id 字段为空,则会根据 updateWrapper 中的条件进行更新操作,达到判断字段不是主键的目的。