updateById
时间: 2023-11-01 14:58:42 浏览: 55
在mybatis-plus中,updateById方法用于根据ID更新实体对象的所有字段,但不会将字段设置为null。因此,如果想要将字段设置为null,可以使用update方法,并通过创建LambdaUpdateWrapper对象来设置字段的更新值。在LambdaUpdateWrapper中,使用set方法可以将字段设置为null。下面是使用update方法将字段设置为null的示例代码:
```java
@Override
public boolean updateArticleById(Integer id) {
Article article = Optional.ofNullable(articleMapper.selectById(id)).orElseThrow(RuntimeException::new);
LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(Article::getOfflineTime, null);
updateWrapper.set(Article::getContent, "try mybatis plus update null");
updateWrapper.set(Article::getPublishTime, LocalDateTime.now().plusHours(8));
updateWrapper.eq(Article::getId, article.getId());
int i = articleMapper.update(article, updateWrapper);
return i == 1;
}
```