SpringMVC之matrixVariable

本文介绍如何利用SpringMVC中的MatrixVariable特性简化多条件组合查询,通过实例演示按类别、价格区间及生产者等维度筛选商品的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

matrixVariable允许我们非常方便地进行多条件组合查询!这里我以商品查询为例,详细介绍matrixVariable的使用。
我们经常有如下需求:查询一定价格区间的商品;查询某些类别的商品;查询某些生产者的商品……第一反应你是不是想到了使用pojo封装查询条件,然后快速完成查询功能?抱歉,这里你看不到我用pojo的实现。但你可以试着看看使用SpringMVC的matrixVariable是怎么实现的。
先看看查询的展示结果:
1.没有使用查询条件查询结果
这里写图片描述

2.使用查询条件查询一(类别,价格区间,生产者):
这里写图片描述

3.使用查询条件查询二(品牌,类别):
这里写图片描述

下面分个讲解条件查询……
按照类别,价格区间,生产者查询,其实最后一个查询条件使用@RequestParam,类别使用@PathVariable,真正使用到matrixVariable的是 价格区间。

Controller层代码:

@RequestMapping("/{category}/{ByCriteria}")
    public String filterProducts(
            @PathVariable("category") String category,
            @MatrixVariable(pathVar="ByCriteria") Map<String, List<String>> filterParams,
            @RequestParam("manufacturer") String manufacturer, Model model) {

        model.addAttribute("products", productService.getProductsByCriteria(category, filterParams, manufacturer));
        return "products";
    }

Service层代码:

public Set<Product> getProductsByCriteria(String category, Map<String, List<String>> filterParams, String manufacturer) {
        return productRepository.getProductsByCriteria(category, filterParams, manufacturer);
    }

Dao层代码:

public Set<Product> getProductsByCriteria(String category, Map<String, List<String>> filterParams, String manufacturer) {

        Set<Product> productsByPrice = new HashSet<Product>();
        Set<Product> productsByManufacturer = new HashSet<Product>();

        Set<Product> productsByCategory = new HashSet<Product>(getProductByCategory(category));

        Set<String> criterias = filterParams.keySet();

        if(criterias.contains("low")) {
            for(String lowPrice : filterParams.get("low")) {
                productsByPrice = getByPrice(lowPrice, ">");
            }
        }
        if(criterias.contains("high")) {
            for(String highPrice : filterParams.get("high")) {
                productsByPrice.retainAll(getByPrice(highPrice, "<"));
            }
        }
        productsByManufacturer = new HashSet<Product>(getProductsByManufacturer(manufacturer));

        productsByCategory.retainAll(productsByPrice);
        productsByPrice.retainAll(productsByManufacturer);

        return productsByCategory;
    }

public List<Product> getProductByCategory(String category) {

        List<Product> productsByCategory = new ArrayList<Product>();

        for(Product product : listOfProducts) {
            if(category.equalsIgnoreCase(product.getCategory())) {
                productsByCategory.add(product);
            }
        }
        return productsByCategory;
    }
private Set<Product> getByPrice(String price, String signal) {

        Set<Product> productByPrice = new HashSet<Product>();
        BigDecimal decimalPrice = new BigDecimal(price);

        if(signal.equals("<")) {
            for(Product product : listOfProducts) {
                if(product.getUnitPrice().compareTo(decimalPrice)<0) {
                    productByPrice.add(product);
                }
            }
        }
        else if(signal.equals(">")) {
            for(Product product : listOfProducts) {
                if(product.getUnitPrice().compareTo(decimalPrice)>0) {
                    productByPrice.add(product);
                }
            }
        }

        return productByPrice;
    }
public List<Product> getProductsByManufacturer(String manufacturer) {

        List<Product> productsByManufactures = new ArrayList<Product>();

        for(Product product : listOfProducts) {
            if(manufacturer.equalsIgnoreCase(product.getManufacturer())) {
                productsByManufactures.add(product);
            }
        }
        return productsByManufactures;
    }

SpringMVC配置文件中配置如下:

<mvc:annotation-driven enable-matrix-variables="true"/>

一切OK!
根据 品牌,类别查询代码如下:

@RequestMapping("/filter/{ByCriteria}")
    public String getProductsByFilter(@MatrixVariable(pathVar="ByCriteria")
            Map<String, List<String>> filterParams, Model model) {

        model.addAttribute("products", productService.getProductsByFilter(filterParams));
        return "products";
    }
public Set<Product> getProductsByFilter(Map<String, List<String>> filterParams) {
        return productRepository.getProductsByFilter(filterParams);
    }
public Set<Product> getProductsByFilter(Map<String, List<String>> filterParams) {

        Set<Product> productsByBrand = new HashSet<Product>();
        Set<Product> productsByCategory = new HashSet<Product>();

        Set<String> criterias = filterParams.keySet();

        if(criterias.contains("brand")) {
            for(String brandName : filterParams.get("brand")) {
                for(Product product : listOfProducts) {
                    if(brandName.equalsIgnoreCase(product.getManufacturer())) {
                        productsByBrand.add(product);
                    }
                }
            }
        }
        if(criterias.contains("category")) {
            for(String category : filterParams.get("category")) {
                productsByCategory.addAll(this.getProductByCategory(category));
            }
        }

        productsByCategory.retainAll(productsByBrand);

        return productsByCategory;
    }

完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值