MybatisPlus报错error: entityList must not be empty

昨天写了几个数据处理的定时任务,晚上放到服务器上跑竟然报错了,看到群里的消息连夜爬起来改bug

集合为空保存不了,saveBatch方法保存空集合也没出过问题,这个saveOrUpdateBatch方法里面有对集合的判断,如果集合为空直接报错。开发的时候就要对集合进行判断,集合为空不执行批量保存修改这个方法。

public boolean saveOrUpdateBatch(Collection entityList, int batchSize) {
        Assert.notEmpty(entityList, "error: entityList must not be empty");
        Class cls = currentModelClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
        String keyProperty = tableInfo.getKeyProperty();
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
        try (SqlSession batchSqlSession = sqlSessionBatch()) {
            int i = 0;
            for (T entity : entityList) {
                Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty);
                if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
                    batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity);
                } else {
                    MapperMethod.ParamMap param = new MapperMethod.ParamMap<>();
                    param.put(Constants.ENTITY, entity);
                    batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
                }
                // 不知道以后会不会有人说更新失败了还要执行插入 
                if (i >= 1 && i % batchSize == 0) {
                    batchSqlSession.flushStatements();
                }
                i++;
            }
            batchSqlSession.flushStatements();
        }
        return true;
    }

解决方法在保存之前加个判断,如果集合为空直接结束。在批量处理数据的方法里面要注意各种极端情况,注意对空集合空数据的判断,以免出现各种错误。


你可能感兴趣的:(#,异常解决方案,java,后端)