需要使用BooleanBuilder连接多个数组值

时间:2019-07-18 20:31:13

标签: java sql-server spring-boot

尝试连接看起来像伪代码的数组值: 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)

0 个答案:

没有答案