mybatis-plus 复杂and or查询

r囧r小猫 2022-12-15 11:13 597阅读 0赞

在查询过程中 经常会有类似如下查询

  1. A='123' and (B='456' or C='789' or D='110')
  2. lambdaWrapper.eq("A","123");
  3. lambdaWrapper.and(wrapper->{
  4. wrapper.or().eq("B","456");
  5. wrapper.or().eq("C","789");
  6. wrapper.or().eq("D","110");
  7. })

项目中应用,根据集合拼接查询条件

  1. lambdaWrapper.and(wrapper -> {
  2. for (DataAuthorityDO dataAuthorityDO : list) {
  3. if (dataAuthorityDO.getIsContainChild().equals(DataAuthorityStatus.IS_CONTAIN_CHILD.code())) {
  4. wrapper.or().likeRight(OrgBase::getFullPath, dataAuthorityDO.getFullPath());
  5. } else {
  6. wrapper.or().eq(OrgBase::getId, dataAuthorityDO.getOrgId());
  7. }
  8. }
  9. return wrapper;
  10. });
  11. // A or (B and C)
  12. .eq("a", "A").or(i -> i.eq("b", "B").eq("c", "C"));
  13. // A or (B or C)
  14. .eq("a", "A").or(i -> i.eq("b", "B").or().eq("c", "C"));
  15. // A and (B and C)
  16. .eq("a", "A").and(i -> i.eq("b", "B").eq("c", "C"));
  17. // A and (B or C)
  18. .eq("a", "A").and(i -> i.eq("b", "B").or().eq("c", "C"));

发表评论

表情:
评论列表 (有 0 条评论,597人围观)

还没有评论,来说两句吧...

相关阅读