MybatisPlus updateById踩坑

MybatisPlus updateById踩坑

提示:这里简述项目相关背景:

mybatisPlus,执行updateById,或者updateBatchById,报空指针异常

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.lang.NullPointerException
### The error may exist in club/caihappy/mapper/ArticleMapper.java (best guess)
### The error may involve club.caihappy.mapper.ArticleMapper.updateById
### The error occurred while executing an update
### Cause: java.lang.NullPointerException
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96) ~[mybatis-spring-2.0.7.jar:2.0.7]
	at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.executeBatch(SqlHelper.java:192) ~[mybatis-plus-extension-3.5.2.jar:3.5.2]
	at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.executeBatch(SqlHelper.java:217) ~[mybatis-plus-extension-3.5.2.jar:3.5.2]
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.executeBatch(ServiceImpl.java:240) ~[mybatis-plus-extension-3.5.2.jar:3.5.2]
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.updateBatchById(ServiceImpl.java:192) ~[mybatis-plus-extension-3.5.2.jar:3.5.2]
	at com.baomidou.mybatisplus.extension.service.IService.updateBatchById(IService.java:268) ~[mybatis-plus-extension-3.5.2.jar:3.5.2]
	at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>) ~[mybatis-plus-extension-3.5.2.jar:3.5.2]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.7.jar:5.3.7]

解决方案:

这是做mybatisPlus自动填充配置的时候没有判断tokenid是否为空,因为这个接口是不需要前端携带token的,所有会报错

原代码:    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", SecurityUtils.getUserId(), metaObject);
    }
改后:
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if (Objects.nonNull(SecurityUtils.getUserId())) {
            this.setFieldValByName("updateBy", SecurityUtils.getUserId(), metaObject);
        }
    }
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值