mybatisplus之Wrappers.lambdaQuery常用写法

从表里查出某一个字段  this.listObjs(WrapperLambdaQuery(table).select(要查询的字段).eq(),Object::toString);

List itemIds = this.listObjs(Wrappers.lambdaQuery(ItemInfo.class).select(ItemInfo::getId).eq(ItemInfo::getItemCode, item.getItemCode()).eq(ItemInfo::getItemBarcode, item.getItemBarcode()), Object::toString);

取list中的第一个数据

CardBase cardBase = cardBaseService.list(Wrappers.lambdaQuery(CardBase.class).select(CardBase::getBaseName)
                    .eq(CardBase::getGoodsId, id)).stream().findFirst().orElse(null);

in的使用--查询 

List batches = cardBatches.stream().map(CardBatch::getBatchNo).collect(Collectors.toList());
        List cardTakes = cardTakeService.list(Wrappers.lambdaQuery(CardTake.class).in(CardTake::getBatchNo, batches).orderByDesc(CardTake::getCreateTime));   //根据createTime降序排序

根据条件更新表里某些字段

@Slf4j
@Service
@AllArgsConstructor
public class OrderInfoService extends BaseServiceImpl {}


private final OrderInfoService infoService;
infoService.update(Wrappers.lambdaUpdate(OrderInfo.class)
                    .set(OrderInfo::getOrderStatus, CommonConstant.order_status.status_discard)
                    .set(OrderInfo::getId, orderInfo.getId() + "-")
                    .eq(OrderInfo::getId, ele.getParentId()));

多表查询---inSql(字段,sql语句) 

    @GetMapping("list")
    public R> list(@RequestParam Map param, Query query) {
        String sql = CommonSql.project_task_sql(param.get("requiredBUType_eq"));
        param.remove("requiredBUType_eq");
        LambdaQueryWrapper wrapper = Condition.getQueryWrapper(param, ReportProject.class).lambda();
        if(sql != null) wrapper.inSql(ReportProject::getProjectId, sql);  //要查询的ReportProject表中的projectId的值判断与另外一个表匹配
        IPage pages = reportProjectService.page(Condition.getPage(query),wrapper.orderByAsc(ReportProject::getIsArchived));
        return data(ReportProjectWrapper.build().pageVO(pages));
    }

public class CommonSql {
    public static String project_task_sql(Object require_BU) {
        if(Objects.nonNull(require_BU)){
            //从另外一个表中查出要匹配的projectId
            return "select id from project_info where project_id in (select project_id from project_task where require_BU = '" + require_BU + "')";
        }
        return null;
    }
}

 mybatisPlus官网:条件构造器 | MyBatis-Plus (baomidou.com)

后续会继续补充

你可能感兴趣的:(mybatis-plus,mybatis-plus)