简体   繁体   English

如何在规范方法中使用多个参数过滤 object?

[英]How to filter an object using multiple parameters in the specification method?

I am writing an online-store using Spring-Boot ( MVC ) and Hiberbate .我正在使用Spring-Boot ( MVC ) 和Hiberbate编写一个在线商店。 I decided to use the Jpa specification to filter the list of objects.我决定使用Jpa specification来过滤对象列表。 For example, filter coffee with parameters of roast and type of coffee .例如,使用烘焙参数和咖啡类型过滤咖啡 I did all the guide and everything worked for me, but with only one input parameter, roasting.我做了所有的指南,一切都对我有用,但只有一个输入参数,烘焙。 How to add one or more parameters for the filter, please tell me...如何为过滤器添加一个或多个参数,请告诉我...

For example my realisation:例如我的实现:

public class CoffeeSpecification {

    public static Specification<Coffee> getCoffeesByRoasting(String name) {
        return (root, query, criteriaBuilder) -> {
            Join<Object, Object> roastingJoin = root.join(Coffee_.ROASTING);
            return criteriaBuilder.equal(roastingJoin.get(Roasting_.NAME), name);
        };
    }}

Service:服务:

public PageDTO<DrinkDTO> findAllFilter(int page, int pageSize, String roastingName, String coffeeType) {

        PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.by("price").ascending());

        final Page<Coffee> coffees = coffeeRepository
                .findAll(CoffeeSpecification.getCoffeesByRoasting(roastingName), pageRequest);

        return new PageDTO<>(drinkMapper.drinksToDrinksDTO(coffees));
    }

Controller: Controller:

@GetMapping("/coffees")
    public PageDTO<DrinkDTO> getAllCoffees(@RequestParam(value = "page", defaultValue = "1") int page,
                                           @RequestParam(value = "page_size", defaultValue = "5") int pageSize,
                                           @RequestParam String roastingName) {

        return coffeeService.findAllFilter(page, pageSize, roastingName, coffeeType);
    }

I have used JpaSpecification for filtering area based on mobile and pincode and using the following code to achieve the same我已经使用JpaSpecification基于mobilepincode过滤区域,并使用以下代码实现相同

public class UserAreaMappingSpecifications {
    public static Specification<UserAreaMapping> userAreaMappingByFilter(String pinCode, String mobile) {
        return (Specification<UserAreaMapping>) (root, query, cb) -> {
            Predicate pinCodePredicate = Objects.nonNull(pinCode) ? cb.equal(root.get("pinCode"), pinCode) : cb.conjunction();
            Predicate mobilePredicate = Objects.nonNull(mobile) ? cb.equal(root.get("mobile"), mobile) : cb.conjunction();
            return cb.and(pinCodePredicate, mobilePredicate);
        };
    }
}

I have used predicates and combining them using and operator.我使用了谓词并使用 and 运算符将它们组合在一起。

Possibly, you can also do with your implementation as well可能,您也可以执行您的实现

public class CoffeeSpecification {

    public static Specification<Coffee> getCoffeesByRoasting(String name, String type) {
        return (root, query, criteriaBuilder) -> {
            Join<Object, Object> roastingJoin = root.join(Coffee_.ROASTING);
            Join<Object, Object> typeJoin = root.join(Coffee_.TYPE);
            Predicate rostingPredicate = criteriaBuilder.equal(roastingJoin.get(Roasting_.NAME), name);
            Predicate typePredicate = criteriaBuilder.equal(roastingJoin.get(Roasting_.TYPE), type);
            return cb.and(rostingPredicate, typePredicate);

        };
    }}

Hope I was able to solve your problem.希望我能够解决您的问题。

Note: You can use or() operator based on your requirement instead of using and() operator.注意:您可以根据需要使用or()运算符,而不是使用and()运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM