获取数据时Java与SQL

时间:2016-10-18 12:29:57

标签: java sql

我有一个表格,我已经保存了一些参数,这些参数作为决定哪个"接收器"的逻辑。选择。我已经说明了下表的内容/数据:

table content

为了选择正确的接收器,我使用了以下SQL:

SELECT *
FROM tableOfReceivers
WHERE client = 'DU'
      AND (country = countryOfDestination OR '*' = countryOfDestination)
      AND (
        (packages >= minNumberOfPackages OR minNumberOfPackages IS NULL) AND
        (packages <= maxNumberOfPackages OR maxNumberOfPackages IS NULL)
      )
      AND (
        (weight >= minNetWeight OR minNetWeight IS NULL) AND
        (weight <= maxNetWeight OR maxNetWeight IS NULL)
      )
ORDER BY priority;

上述SQL的变量是:

country = BE
packages = 5
weight = 50

SQL工作正常,我得到了我想要的结果。但我需要将其转换为Java,当我这样做时,我觉得它过于复杂,而且我认为必须更容易做到这一点。

以下是我目前正在使用的Java代码:

ArrayList<tableOfReceivers > listOfReceivers = getPersistenceManager().getQueryManager().queryAll(
            tableOfReceivers.class, "this.client = ?1", "this.client", new Object[] { client }, 0);

ArrayList<tableOfReceivers> result = new ArrayList<tableOfReceivers>();

for (tableOfReceivers  rec : listOfReceivers) {
    if ((StringUtils.equals(countryOfDestination, rec.getCountryOfDestination())
            || StringUtils.equals("*", rec.getCountryOfDestination()))) {

        if (((numberOfPackages >= nvl(rec.getMinNumberOfPackages(), 0))
                || rec.getMinNumberOfPackages() == null)
                && (numberOfPackages <= nvl(rec.getMaxNumberOfPackages(),0))
                || rec.getMaxNumberOfPackages() == null) {

            if (((totalNetWeight >= nvl(rec.getMinNetWeight(),0)) || rec.getMinNetWeight() == null)
                    && (totalNetWeight <= nvl(rec.getMaxNetWeight(),0)) || rec.getMaxNetWeight() == null) {
                result.add(rec);
            }
        }
    }
}

Collections.sort(result, new Comparator<tableOfReceivers >() {

    @Override
    public int compare(tableOfReceivers  o1, tableOfReceivers  o2) {
        return o1.getPriority().compareTo(o2.getPriority());
    }
});

System.out.println("Receiver selection result is:" + "\n");
if(!result.isEmpty()){
    System.out.println(result.get(0).getReceiver() + "\n");
}       

}

public static Integer nvl(Integer value, Integer alternateValue) {
    if (value == null)
        return alternateValue;
    return value;
}

那么,是否有更简单,更好的方法来获取我只需要使用Java的东西?

谢谢!

1 个答案:

答案 0 :(得分:1)

我会使用Java 8

List<tableOfReceivers> result = 
    listOfReceivers.stream()
                   .filter(rec -> countryOfDestination.equals(rec.getCountryOfDestination())
                                  || "*".equals(rec.getCountryOfDestination())
                   .filter(numberOfPackages >= nvl(rec.getMinNumberOfPackages(), numberOfPackages ))
                   .filter(numberOfPackages <= nvl(rec.getMaxNumberOfPackages(), numberOfPackages ))
                   .filter(totalNetWeight >= nvl(rec.getMinNetWeight(), totalNetWeight))
                   .filter(totalNetWeight <= nvl(rec.getMaxNetWeight(), totalNetWeight))
                   .sort(Comparator.comparing(rec -> rec.getPriority()))
                   .collect(Collectors.toList());

或者你可以使用像in

这样的辅助方法
public static boolean in(int value, Integer min, Integer max) {
    return (min == null || min <= value) &&
           (max == null || max >= value);
}

public static boolean countryMatch(String a, String b) {
    return "*".equals(a) || "*".equals(b) || a.equals(b);
}

List<tableOfReceivers> result = 
    listOfReceivers.stream()
                   .filter(rec -> countryMatch(countryOfDestination, rec.getCountryOfDestination()))
                   .filter(rec -> in(numberOfPackages, rec.getMinNumberOfPackages(), rec.getMaxNumberOfPackages())))
                   .filter(rec -> in(totalNetWeight, rec.getMinNetWeight(), rec.getMaxNetWeight(), totalNetWeight))
                   .sort(Comparator.comparing(rec -> rec.getPriority()))
                   .collect(Collectors.toList());