如何使用 Boolean Builder Spring 引导来过滤记录

How to filter records using Boolean Builder Spring Boot

JSON 我的代码:

{
        "webId": 1713,
        "updateDate": "2021-10-05 09:51 AM",
        "createdDate": "2021-10-05 09:51 AM",
        "clientId": 1301,
        "createdBy": null,
        "updatedBy": "ABC",
        "createdById": 1713,
        "updatedById": 1713,
        "email": "ABC@gmail.com",
        "externalId": "ABC",
        "userNumber": "DEF",
        "firstName": "ABC",
        "lastName": "XYZ",
        "surName": "",
        "password": "{ZCjP0UXZnP1ai8StklMx07KjKGW681JwVYp6sT3Fi6A=}1f662684556ea6ca39ef11e93ec73de0",
        "status": "ACTIVE",
        "birthday": null,
        "phoneNumber": "",
        "loginCount": null,
        "lastLogin": null,
        "language": null,
        "timeZone": null,
        "profilePicture": null,
        "userRoleModels": [
            {
                "webId": 8202,
                "updateDate": "2021-10-12 03:46 PM",
                "createdDate": "2021-10-12 03:46 PM",
                "clientId": 1301,
                "createdBy": "ABC",
                "updatedBy": "ABC",
                "createdById": 1713,
                "updatedById": 1713,
                "role": "100",
                "roleCodeText": "Admin",
                "userId": 1713
            },
            {
                "webId": 8152,
                "updateDate": "2021-10-12 03:46 PM",
                "createdDate": "2021-10-12 03:46 PM",
                "clientId": 1301,
                "createdBy": "ABC",
                "updatedBy": "ABC",
                "createdById": 1713,
                "updatedById": 1713,
                "role": "200",
                "roleCodeText": "Manager",
                "userId": 1713
            }
        ],
        "userPermissionId": null
    },
    {
        "webId": 8702,
        "updateDate": "2021-08-26 08:45 AM",
        "createdDate": "2021-08-11 12:00 AM",
        "clientId": 1301,
        "createdBy": "Test",
        "updatedBy": "Test",
        "createdById": 8702,
        "updatedById": 8702,
        "email": null,
        "externalId": null,
        "userNumber": null,
        "firstName": "TEST",
        "lastName": "TEST",
        "surName": null,
        "password": "{EwGlmHpntU1ESocNYysUaaXm8bRpAo3j3OmScvUeQlU=}c5b4fc0e67cdc0ab5ab9e1637e52f43c",
        "status": "ACTIVE",
        "birthday": "2021-08-23",
        "phoneNumber": "03041390843",
        "loginCount": null,
        "lastLogin": null,
        "language": "123",
        "timeZone": null,
        "profilePicture": null,
        "userRoleModels": [
            {
                "webId": 18004,
                "updateDate": "2021-09-04 02:26 PM",
                "createdDate": "2021-09-04 02:26 PM",
                "clientId": 1301,
                "createdBy": "TEST",
                "updatedBy": "TEST",
                "createdById": 8702,
                "updatedById": 8702,
                "role": "100",
                "roleCodeText": "Admin",
                "userId": 8702
            }
        ],
        "userPermissionId": null
    }

有很多记录,但我只附上2条记录。

存在一个包含元素“role”的 userRoleModel 数组。我必须使用该角色过滤所有记录,但用户可以一次过滤多个角色。喜欢

URL: http://localhost:8183/api/security/userAccounts?status=ACTIVE&code=100,200

所以结果应该是具有 userRoleModels 角色=100 和 200 的所有记录

public Page<UserAccount> getAllUserAccounts(String status, List<String> code,
                                            Pageable pageable) {
    BooleanBuilder filter = new BooleanBuilder();

    if (StringUtils.isNotBlank(status)) {
        filter.and(QUserAccount.userAccount.status.eq(UserAccount.UserAccountStatus.valueOf(status)));
    }
    if (!ObjectUtils.isEmpty(code)) {     //this check is not working
        filter.and(QUserAccount.userAccount.userRoles.any().role.contains((Expression<String>) code));         //what should come here
    }

    return userAccountRepository.findAll(filter, pageable);
}

contains 不适用于列表。在这种情况下,您应该改用 in。请看例子。

public Page<UserAccount> getAllUserAccounts(String status, List<String> code,
                                        Pageable pageable) {
    BooleanBuilder filter = new BooleanBuilder();

    if (StringUtils.isNotBlank(status)) {
        filter.and(QUserAccount.userAccount.status.eq(UserAccount.UserAccountStatus.valueOf(status)));
    }
    if (!ObjectUtils.isEmpty(code)) {     //this check is not working
        filter.and(QUserAccount.userAccount.userRoles.any().role.in(code));         
    }

    return userAccountRepository.findAll(filter, pageable);
}