Java中int和Integer在Mybatis-plus中对于数据库字段会导致的问题

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数据类型去对应相关数据库字段的实体类
会导致这些字段未被指明的字段也被更新。
这些字段会呈现默认值的情况。
所以好像可以用包装类还是用包装类吧。
小菜鸡的一点见解,有错误之处请多多指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值