从循环中添加规范

huangapple go评论42阅读模式
英文:

Add specification from a loop

问题

以下是代码部分的中文翻译:

在一个Spring Boot 3应用程序中我尝试使用规约Specification)。

public Page<User> advancedSearch(UserSearch search, Pageable page) {

    String[] splittedValues = search.name.split(" ");

    Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {

        ...

        return pre;
    };

    return findAll(specification, page);
}

我需要为splittedValues中的每个值添加一个全局规约

for (String splittedName : splittedValues) {
   specification.or(splittedName);
}

并将其传递给findAll

我不明白如何做到这一点

编辑

你的解决方案似乎有效但会生成以下查询

where
1=1
or like e1_0.name "%bob%"
or like e1_0.name "%jame%"


有没有办法得到以下查询:

where
1=1
and (
like e1_0.name "%bob%"
or like e1_0.name "%jame%"
)

请注意,这个翻译只包括代码部分,不包括其他内容。

英文:

In a spring boot 3 application I try to use specification

public Page&lt;User&gt; advancedSearch(UserSearch search, Pageable page) {

      String[] splittedValues = search.name.split(&quot; &quot;);
    

      Specification&lt;User&gt; hasPersonWithName = (Root&lt;User&gt; root, CriteriaQuery&lt;?&gt; cq, CriteriaBuilder cb) -&gt; {

            ...

            return pre;
        };

    return findAll(specification, page);

}

I need for every value in splittedValues to add a global specification

for (String splittedName: splittedValues) {
   specification.or(splittedName);
}

and pass it to findAll

I don't understand how to do it

Edit

you solution seem to work but

that generate

where
    1=1 
    or like e1_0.name &quot;%bob%&quot;
    or like e1_0.name &quot;%jame%&quot;

It's there a way to get

where
    1=1 
    and (
    like e1_0.name &quot;%bob%&quot;
    or like e1_0.name &quot;%jame%&quot;
    )

答案1

得分: 1

使用 or 运算符,您可以组合多个条件,检查 Username 是否包含 splittedValues 中的一个。然后,从规约返回谓词。

希望这是您想要的,如果我理解错了,请纠正我。如果是这样,请参照以下示例进行调整以适应您自己的代码。

编辑:要获取 1=1 and (like e1_0.name "%bob%" or like e1_0.name "%jame%"),您可以使用 cb.or 方法组合条件,然后使用 cb.andnameConditionspre 结合。

public Page<User> advancedSearch(UserSearch search, Pageable page) {
    String[] splittedValues = search.name.split(" ");

    Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
        Predicate pre = cb.conjunction();
        Predicate nameConditions = null;
        for (String splittedName : splittedValues) {
            if (nameConditions == null) {
                nameConditions = cb.like(root.get("name"), "%" + splittedName + "%");
            } else {
                nameConditions = cb.or(nameConditions, cb.like(root.get("name"), "%" + splittedName + "%"));
            }
        }
        if (nameConditions != null) {
            pre = cb.and(pre, nameConditions);
        }
        return pre;
    };

    return findAll(hasPersonWithName, page);
}

请注意,这是您提供的代码的翻译部分。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

Using or operator , you combine multiple conditions, checking whether the name of User contains one of the splittedValues. Predicate is then returned from specification.

I hope that was what you wanted, in case I didn't understand, please correct me. Here is an example if so, adapt to your own code.

Edit: To get 1=1 and (like e1_0.name &quot;%bob%&quot; or like e1_0.name &quot;%jame%&quot;), you can use cb.or method to combine the conditions, resulting nameConditions is then combined with pre using cb.and.

public Page&lt;User&gt; advancedSearch(UserSearch search, Pageable page) {
    String[] splittedValues = search.name.split(&quot; &quot;);

    Specification&lt;User&gt; hasPersonWithName = (Root&lt;User&gt; root, CriteriaQuery&lt;?&gt; cq, CriteriaBuilder cb) -&gt; {
        Predicate pre = cb.conjunction();
        Predicate nameConditions = null;
        for (String splittedName: splittedValues) {
            if (nameConditions == null) {
                nameConditions = cb.like(root.get(&quot;name&quot;), &quot;%&quot; + splittedName + &quot;%&quot;);
            } else {
                nameConditions = cb.or(nameConditions, cb.like(root.get(&quot;name&quot;), &quot;%&quot; + splittedName + &quot;%&quot;));
            }
        }
        if (nameConditions != null) {
            pre = cb.and(pre, nameConditions);
        }
        return pre;
    };

return findAll(hasPersonWithName, page);
}

huangapple
  • 本文由 发表于 2023年2月18日 22:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定