首页 > 解决方案 > 需要使用 BooleanBuilder 连接多个数组值

问题描述

尝试连接看起来像伪代码的数组值: SELECT * FROM (fiscalYear = 2017 OR fiscalYear = 2018) AND (accountingPeriod = 2017 OR accountingPeriod = 2018)

当前输出为: SELECT * FROM fiscalYear = 2017 OR fiscalYear = 2018

我尝试过使用 BooleanBuilder 的多个连接,并且还围绕所有返回创建了一个大循环。没有任何效果。

@Service
public class FilterService {

    @Autowired
    SpendRepository spendRepository;

    public Iterable<Spend> filterSpendingByAll(Integer[] fiscalYear, Integer[] accountingPeriod, String[] department, String[] apCategory,
                                               String[] jurisdiction, String[] functionName, String[] vendorId) {

        QSpend spend = QSpend.spend;

        BooleanBuilder builder = new BooleanBuilder();

        for (int i = 0; i < fiscalYear.length; i++){
            builder.or(spend.fiscalYear.eq(fiscalYear[i]));
        }

        for (int i = 0; i < accountingPeriod.length; i++){
            builder.or(spend.accountingPeriod.eq(accountingPeriod[i]));
        }

        for (int i = 0; i < department.length; i++){
            builder.or(spend.department.like("%" + department[i] + "%"));
        }

        for (int i = 0; i < apCategory.length; i++){
            builder.or(spend.apCategory.like("%" + apCategory[i] + "%"));
        }

        for (int i = 0; i < jurisdiction.length; i++){
            builder.or(spend.jurisdiction.like("%" + jurisdiction[i] + "%"));
        }

        for (int i = 0; i < functionName.length; i++){
            builder.or(spend.functionName.like("%" + functionName[i] + "%"));
        }

        for (int i = 0; i < vendorId.length; i++){
            builder.or(spend.vendorId.like("%" + vendorId[i] + "%"));
        }

        for (int i = 0; i < vendorId.length; i++){
            builder.or(spend.vendorId.eq(vendorId[i]));
        }

        return this.spendRepository.findAll(builder);


    }
}

实际结果是: SELECT * FROM fiscalYear = 2017 OR fiscalYear = 2018

预期结果是: SELECT * FROM (fiscalYear = 2017 OR fiscalYear = 2018) AND (accountingPeriod = 2017 OR accountingPeriod = 2018)

标签: javasql-serverspring-boot

解决方案


推荐阅读