mark一下学习博客系统开发时遇到的一个基础问题,
由于Java中int类型默认为0而Integer作为包装类默认是null
所以Mybatis-plus生成sql语句的时候可能会导致意料之外的字段被更改
例如
@Async("taskExecutor")
public void updateArticleViewCounts(ArticleMapper articleMapper, Article article){
Integer viewCounts = article.getViewCounts();
Article articleUpdate = new Article();
articleUpdate.setViewCounts(viewCounts + 1);
LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(Article::getId, article.getId());
updateWrapper.eq(Article::getViewCounts, article.getViewCounts());
articleMapper.update(articleUpdate, updateWrapper);
}
这是一个博客系统常见的更新阅读量的操作
但是如果采用int数据类型去对应相关数据库字段的实体类
会导致这些字段未被指明的字段也被更新。
这些字段会呈现默认值的情况。
所以好像可以用包装类还是用包装类吧。
小菜鸡的一点见解,有错误之处请多多指教