学无先后,达者为师

网站首页 编程语言 正文

MyBatis-plus wrapper.and用法 | apply

作者:m0_59259076 更新时间: 2023-10-11 编程语言

MyBatis-plus 通过 wrapper.and方法给sql添加额外的查询条件

MyBatis-plus方法:
    public Children and(boolean condition, Consumer<Children> consumer) {
        return this.and(condition).addNestedCondition(condition, consumer);
    }

一、需求:如果用户穿插了查询参数,就给sql附加条件(当然,这个条件可以是复杂的条件),否则查询不用带上条件。

示例:

    @org.junit.Test
    public void test() {
        String name = "狗哥";
        QueryWrapper<Animal> wrapper = new QueryWrapper<>();
        wrapper.and(StringUtils.isNotBlank(name),item->item.apply(
                "FIND_IN_SET({0},name)",name));
        List<Animal> animalList = animalMapper.selectList(wrapper);
        log.info("总记录:{}",animalList.size());
        animalList.forEach(System.out::println);
    }

一、需求:如果用户穿插了查询参数,就给sql附加条件(当然,这个条件可以是复杂的条件),否则查询其他的条件。

    default Children and(Consumer<Param> consumer) {
        return this.and(true, consumer);
    }
    @org.junit.Test
    public void test() {
        String name = "";
        QueryWrapper<Animal> wrapper = new QueryWrapper<>();
        wrapper.and(item->item.apply(
                "FIND_IN_SET({0},name)",name).or().eq("name","猫哥"));
        List<Animal> animalList = animalMapper.selectList(wrapper);
        log.info("总记录:{}",animalList.size());
        animalList.forEach(System.out::println);
    }
 wrapper.apply("(receipt_no Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or customer Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or matter Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or bill_unit Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or receipt_money Like '%"+incomeCollectionReceipt.getSearchValue()+"%' or item_name Like '%"+incomeCollectionReceipt.getSearchValue()+"%')");

原文链接:https://blog.csdn.net/m0_59259076/article/details/125120100

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新