Mybatis-plus通过其他字段批量更新或新增

根据某个或者多个非ID字段进行批量更新

示例通过名称与id两个字段更新

    @Override
    public boolean updateBatchByColumn(List<TestTable> list) {
        return updateBatchByQueryWrapper(list, item->new QueryWrapper<>().eq("name",item.getName()).eq("id",item.getId()));
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatchByQueryWrapper(Collection<TestTable> entityList, Function<TestTable, QueryWrapper> wrapperFunction) {
        String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE);
        return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
            Map<String, Object> param = CollectionUtils.newHashMapWithExpectedSize(2);
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, wrapperFunction.apply(entity));
            sqlSession.update(sqlStatement, param);
        });
    }

引用mybatis-plus根据某个指定字段批量更新数据库


通过其他字段批量更新或新增

 /**
     * 条件批量添加更新
     *
     * @param entityList 数据
     * @param function 条件
     * @return boolean
     */
    public boolean saveOrUpdateBatchByColumn(Collection<TestTable> entityList, Function<TestTable, QueryWrapper> function ) {
        return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, LogFactory.getLog(AvgRowSub.class), entityList, 1000, (sqlSession, entity) -> {
            Map<String, Object> param = Maps.newHashMap();
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, function.apply(entity));
            return CollectionUtils.isEmpty(sqlSession.selectList(this.getSqlStatement(SqlMethod.SELECT_MAPS), param));
        }, (sqlSession, entity) -> {
            Map<String, Object> param = Maps.newHashMap();
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, function.apply(entity));
            sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE), param);
        });
    }

你可能感兴趣的:(记录,mybatis,java)