【mybatis-plus 根据指定字段批量更新数据库记录】

mybatis-plus 根据指定字段批量更新数据库记录

接口层:

public interface OrderInfoService extends IService<OrderInfo> {
    /**
     * 批量更新
     *
     * @param entityList
     * @param queryWrapperFunction
     */
    boolean updateBatchByQueryWrapper(Collection<OrderInfo> entityList, Function<OrderInfo, LambdaUpdateWrapper<OrderInfo>> queryWrapperFunction);
}

实现层:

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
 @Override
    public boolean updateBatchByQueryWrapper(Collection<OrderInfo> entityList, Function<OrderInfo, LambdaUpdateWrapper<OrderInfo>> queryWrapperFunction) {
        Assert.notEmpty(entityList, "error: entityList must not be empty");
        String sqlStatement = this.sqlStatement(SqlMethod.UPDATE);
        try (SqlSession batchSqlSession = sqlSessionBatch()) {
            int i = 0;
            for (OrderInfo entity : entityList) {
                MapperMethod.ParamMap param = new MapperMethod.ParamMap<>();
                param.put(Constants.ENTITY, entity);
                param.put(Constants.WRAPPER, queryWrapperFunction.apply(entity));
                batchSqlSession.update(sqlStatement, param);
                if (i >= 1 && i % DEFAULT_BATCH_SIZE == 0) {
                    batchSqlSession.flushStatements();
                }
                i++;
            }
            batchSqlSession.flushStatements();
        }
        return true;
    }

调用层:

 orderInfoService.updateBatchPOVerifyStateByQueryWrapper(orderInfos, orderInfo -> {
                        LambdaUpdateWrapper<OrderInfo> wrapper = Wrappers.lambdaUpdate();
                        //自定义指定字段
                        wrapper.eq(OrderInfo::getOrderId, orderInfo.getOrderId());
                        return wrapper;
                    });

你可能感兴趣的:(mybatisplus,java,mybatis,数据库)