IWrapper、QueryWrapper和LambdaQueryWrapper使用

条件更新updateByWrapper

通常我们在更新的时候new一个对象出来在去更新,减少更新的字段

Integer update = new DemoUser().updateByWrapper(new DemoUser().setFirstLogin(Boolean.FALSE),
        Pops.<DemoUser>lambdaUpdate().from(DemoUser.MODEL_MODEL).eq(IdModel::getId, userId)

使用基础模型的updateById方法更新指定字段的方法:

  • new 一下update对象出来,更新这个对象。
    WorkflowUserTask userTaskUp = new WorkflowUserTask();
    userTaskUp.setId(userTask.getId());
    userTaskUp.setNodeContext(json);
    userTaskUp.updateById();

条件删除updateByWrapper

    public List<T> delete(List<T> data) {
        List<Long> petTypeIdList = new ArrayList();
        for(T item:data){
            petTypeIdList.add(item.getId());
        }
        Models.data().deleteByWrapper(Pops.<PetType>lambdaQuery().from(PetType.MODEL_MODEL).in(PetType::getId,petTypeIdList));
        return data;
    }

构造条件查询数据

  • 示例1: LambdaQueryWrapper拼接查询条件
    private void queryPetShops() {
        LambdaQueryWrapper<PetShop> query = Pops.<PetShop>lambdaQuery();
        query.from(PetShop.MODEL_MODEL);
        query.setSortable(Boolean.FALSE);
        query.orderBy(true, true, PetShop::getId);
        List<PetShop> petShops2 = new PetShop().queryList(query);
        System.out.printf(petShops2.size() + "");
    }
  • 示例2: IWrapper拼接查询条件
    private void queryPetShops() {
        IWrapper<PetShop> wrapper = Pops.<PetShop>lambdaQuery()
                .from(PetShop.MODEL_MODEL).eq(PetShop::getId,1L);
        List<PetShop> petShops4 = new PetShop().queryList(wrapper);
        System.out.printf(petShops4.size() + "");
    }
  • 示例3: QueryWrapper拼接查询条件
    private void queryPetShops() {
        //使用Lambda获取字段名,防止后面改字段名漏改
        String nameField = LambdaUtil.fetchFieldName(PetTalent::getName);
        //使用Lambda获取Clumon名,防止后面改字段名漏改
        String nameColumn = PStringUtils.fieldName2Column(nameField);
        QueryWrapper<PetShop> wrapper2 = new QueryWrapper<PetShop>().from(PetShop.MODEL_MODEL)
                .eq(nameColumn, "test");
        List<PetShop> petShops5 = new PetShop().queryList(wrapper2);
        System.out.printf(petShops5.size() + "");
    }

IWrapper转为LambdaQueryWrapper

    @Function.Advanced(type= FunctionTypeEnum.QUERY)
    @Function.fun(FunctionConstants.queryPage)
    @Function(openLevel = {FunctionOpenEnum.API})
    public Pagination<PetShopProxy> queryPage(Pagination<PetShopProxy> page, IWrapper<PetShopProxy> queryWrapper) {
        LambdaQueryWrapper<PetShopProxy> wrapper = ((QueryWrapper<PetShopProxy>) queryWrapper).lambda();
       // 非存储字段从QueryData中获取
        Map<String, Object> queryData = queryWrapper.getQueryData();
        if (null != queryData && !queryData.isEmpty()) {
            String codes = (String) queryData.get("codes");
            if (org.apache.commons.lang3.StringUtils.isNotBlank(codes)) {
                wrapper.in(PetShopProxy::getCode, codes.split(","));
            }
        }

        return new PetShopProxy().queryPage(page, wrapper);
    }

Oinone社区 作者:望闲原创文章,如若转载,请注明出处:https://doc.oinone.top/backend/11467.html

访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验

(2)
望闲的头像望闲数式管理员
上一篇 2024年5月25日 下午7:22
下一篇 2024年5月25日 下午8:10

相关推荐

发表回复

登录后才能评论