其他分享
首页 > 其他分享> > QueryWrapper查询失效

QueryWrapper查询失效

作者:互联网

错误代码:

if (!CollectionUtils.isEmpty(warehouseIds)) {
            lambda.in(StockTransferEntity::getInWarehouseId, warehouseIds).or().in(StockTransferEntity::getOutWarehouseId, warehouseIds);
        }

打印sql:

SELECT * FROM dfs_stock_transfer 
WHERE (in_warehouse_id IN (375822,375825,375824,375823) 
            OR out_warehouse_id IN (375822,375825,375824,375823)AND out_warehouse_name = '34') 
ORDER BY create_time DESC LIMIT 20

错误结果:

 

 

 错误原因:

QueryWrapper.in()查询使后面的QueryWrapper查询条件失效:

lambda.eq(StockTransferEntity::getOutWarehouseName, outWarehouseName);

正确代码:

        if (!CollectionUtils.isEmpty(warehouseIds)) {
            List<Integer> finalWarehouseIds = warehouseIds;
            lambda.and(wrapper -> wrapper.in(StockTransferEntity::getInWarehouseId, finalWarehouseIds)
                    .or().in(StockTransferEntity::getOutWarehouseId, finalWarehouseIds));
        }

打印sql:

SELECT * FROM dfs_stock_transfer
WHERE ((in_warehouse_id IN (375822,375825,375824,375823) 
            OR out_warehouse_id IN (375822,375825,375824,375823)) AND out_warehouse_name = '34')
ORDER BY create_time DESC LIMIT 20

正确结果:

 

标签:QueryWrapper,375823,375822,375825,warehouseIds,StockTransferEntity,查询,warehouse,
来源: https://www.cnblogs.com/axibug/p/16359926.html