通过本机查询按多个参数组合进行搜索

时间:2018-09-07 20:56:55

标签: java jpa nativequery

我目前正在研究一种功能,它将允许系统通过6个参数的组合来搜索公共服务收据,这些参数可以为null,这意味着不应使用以下参数过滤收据:accountNumber,amountRangeMin,amountRangeMax,dateRangeMin ,dateRangeMax,publicServiceId。但是,为每个参数组合制作方法都不是一种选择,我认为必须有更好的方法,起初我的方法如下:

在我的服务中,我有这种方法:

public Map<String,Object> findPublicServiceReceiptsByParams(Integer accountNumber, BigDecimal amountRangeMin,
        BigDecimal amountRangeMax, LocalDate dateRangeMin, LocalDate dateRangeMax, Integer publicServiceId) {
    Map<String,Object> publicServiceReceipts = new HashMap<String,Object>();
    String accountNumberFilter = !(accountNumber==null) ? accountNumber.toString() : "AccountNumberTableName";
    String amountRangeMinFilter = !(amountRangeMin==null) ? amountRangeMin.toString() : "table.AmountColumnName";
    String amountRangeMaxFilter = !(amountRangeMax==null) ? amountRangeMax.toString() : "table.AmountColumnName";
    String dateRangeMinFilter = !(dateRangeMin==null) ? dateRangeMin.toString() : "Table.ReceiptCreationDateColumn";
    String dateRangeMaxFilter = !(dateRangeMax==null) ? dateRangeMax.toString() : "Table.ReceiptCreationDateColumn";
    String publicServiceIdFilter = !(publicServiceId==null) ? publicServiceId.toString() : "table.publicServiceIdColumn";
    publicServiceReceipts = publicServiceReceiptRepository.findPublicServiceReceiptsByParams(accountNumberFilter,
            amountRangeMinFilter, amountRangeMaxFilter, dateRangeMinFilter, dateRangeMaxFilter,
            publicServiceIdFilter);
    return publicServiceReceipts;
}

然后在我的存储库中有:

final static String FIND_PUBLIC_SERVICES_BY_ARGS = "Select (Insert whatever logic should go in here to select columns from receipts the where clause is the one that matters)"
        + "    WHERE ACT.ACT_AccountNumber=:accountNumberFilter\n"
        + "      AND PSE.PSE_Id=:publicServiceIdFilter\n"
        + "      AND PSR.PSR_CreateDate BETWEEN :dateRangeMinFilter AND :dateRangeMaxFilter\n"
        + "      AND PSR.PSR_Amount BETWEEN :amountRangeMinFilter AND :amountRangeMaxFilter\n"
        + " order by PSR.PSR_CreateDate desc";

@Query(nativeQuery = true, value = FIND_PUBLIC_SERVICES_BY_ARGS)
Map<String, Object> findPublicServiceReceiptsByParams(@Param("accountNumberFilter") String accountNumberFilter,
        @Param("amountRangeMinFilter") String amountRangeMinFilter,
        @Param("amountRangeMaxFilter") String amountRangeMaxFilter,
        @Param("dateRangeMinFilter") String dateRangeMinFilter,
        @Param("dateRangeMaxFilter") String dateRangeMaxFilter,
        @Param("publicServiceIdFilter") String publicServiceIdFilter);

}

我的理由是,如果参数为null,则表示使用Web Service的人对该参数不感兴趣,因此,如果发生这种情况,我将该变量设置为Column Name,以使它不会影响WHERE子句和理论使它更简单,但我发现它会以字符串形式发送名称,因此不会被认为是sql语句,这是我的思想上的缺陷,正如我所说的,除了编写每个方法外,还必须有其他方法对于每种组合,我都非常感谢帮助:)。

2 个答案:

答案 0 :(得分:0)

您应该使用Criteria API,它是用于创建动态查询的。在这种情况下,并不一定要使用命名查询。 有了它,您可以执行以下操作:

@PersistenceContext
EntityManager em;

List<YourEntity> method(String argument) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<YourEntity> cq = cb.createQuery(YourEntity.class);
    Root<YourEntity> root = cq.from(YourEntity.class);

    List<Predicate> predicates = new ArrayList<>();
    if (argument == null) {
        predicates.add(cb.equal(root.get("yourAttribute"), argument);
    }
    // rest of your logic goes here

    cq.where(predicates.toArray(new Predicate[]{}));
    return em.createQuery(cq).getResultList();
}

答案 1 :(得分:0)

我找到了解决此问题的方法,我做到了这一点(我将仅显示本机Query,因为这是我所更改的内容):

DECLARE  @actNum varchar(50),@crdNum varchar(50),@pseId varchar(50),@dateMin varchar(50),@dateMax varchar(50),@amountMin varchar(50),@amountMax varchar(50)
SET @actNum = :actNum
SET @crdNum = :crdNum 
SET @pseId =  :pseId 
SET @dateMin = :dateMin 
SET @dateMax = :dateMax 
SET @amountMin = :amountMin 
SET @amountMax = :amountMax 
--Whatever Select with joins statement
                   WHERE ACT.ACT_AccountNumber = CASE WHEN @actNum = 'N/A' 
                                             THEN ACT.ACT_AccountNumber 
                                             ELSE @actNum END
                 AND CRD_CardNumber = CASE WHEN @crdNum = 'N/A' 
                                      THEN CRD_CardNumber 
                                      ELSE @crdNum END
                 AND PSE.PSE_Id= CASE WHEN @pseId = 'N/A' 
                                 THEN PSE.PSE_Id 
                                 ELSE @pseId END
                 AND PSR.PSR_CreateDate >= CASE WHEN @dateMin = 'N/A' 
                                                THEN PSR.PSR_CreateDate 
                                                ELSE @dateMin END 
                                        AND PSR.PSR_CreateDate <= CASE WHEN @dateMax = 'N/A' 
                                            THEN PSR.PSR_CreateDate 
                                            ELSE @dateMax END
                 AND PSR.PSR_Amount BETWEEN CASE WHEN @amountMin = 'N/A' 
                                            THEN PSR.PSR_Amount 
                                            ELSE @amountMin END 
                                    AND CASE WHEN @amountMax = 'N/A' 
                                        THEN PSR.PSR_Amount 
                                        ELSE @amountMax END 
            ORDER BY PSR.PSR_CreateDate DESC

后端将以“ N / A”(如果不应使用它来过滤数据)或实际值的形式发送参数,这对我来说很好!