spring data jpa-创建动态查询注解

ghhaqwfi  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(439)

我想知道是否可以用@query(value=query)替换@query(value=“”),其中query是我可以在某处编辑的字符串。
我必须创建一个函数来搜索我的数据库中使用许多细节。
下面是我调用存储库的代码:

public List<FilteringRuleEto> findRuleByParam(FilteringRuleDto filteringRule) {

        String Query = "SELECT rule FROM FilteringRuleEto rule WHERE";

        if (!filteringRule.getRouteClass().equals(null)) {
            Query += " rule.routeClass LIKE ?1 AND";
        }
        if (!filteringRule.getCodeOperator().equals(null)) {
            Query += " rule.codeOperator LIKE ?2 AND";
        }
        if (!filteringRule.getCommercialTrunk().equals(null)) {
            Query += " rule.commercialTrunk LIKE ?3 AND";
        }
        if (!filteringRule.getSupportType().equals(null)) {
            Query += " rule.supportType LIKE ?4 AND";
        }
        if (!filteringRule.getCalledNumber().equals(null)) {
            Query += " rule.calledNumber LIKE ?5 AND";
        }
        if (!filteringRule.getCalledNumberRange().equals(null)) {
            Query += " rule.calledNumberRange = ?6 AND";
        }
        if (!filteringRule.getCalledNumberWhitelist().equals(null)) {
            Query += " rule.calledNumberWhitelist = ?7 AND";
        }
        if (!filteringRule.getCallingNumber().equals(null)) {
            Query += " rule.callingNumber LIKE ?8 AND";
        }
        if (!filteringRule.getCallingNumberRange().equals(null)) {
            Query += " rule.callingNumberRange = ?9 AND";
        }
        if (!filteringRule.getCallingNumberWhitelist().equals(null)) {
            Query += " rule.callingNumberWhitelist = ?10 AND";
        }
        if (!filteringRule.getBeginDate().equals(null)) {
            Query += " rule.beginDate = ?11 AND";
        }
        if (!filteringRule.getEndDate().equals(null)) {
            Query += " rule.endDate = ?12 AND";
        }
        if (!filteringRule.getTgid().equals(null)) {
            Query += " rule.tgid LIKE ?13 AND";
        }
        if (!filteringRule.getFilteringType().equals(null)) {
            Query += " rule.filteringType LIKE ?14 AND";
        }
        if (!filteringRule.getDateInsert().equals(null)) {
            Query += " rule.dateInsert = ?15 AND";
        }
        if (!filteringRule.getEndDate().equals(null)) {
            Query += " rule.dateUpdate = ?16 AND";
        }
        if (!filteringRule.getComment().equals(null)) {
            Query += " rule.comment = ?17 AND";
        }
        Query.substring(0, Query.length() - 3); 

        //Here I want to send the String Query to the Repository

        return this.filteringRuleRepository.findRuleByParameters(
                filteringRule.getRouteClass(),
                filteringRule.getCodeOperator(),
                filteringRule.getCommercialTrunk(),
                filteringRule.getSupportType(),
                filteringRule.getCalledNumber(),
                filteringRule.getCalledNumberRange(),
                filteringRule.getCalledNumberWhitelist(),
                filteringRule.getCallingNumber(),
                filteringRule.getCallingNumberRange(),
                filteringRule.getCallingNumberWhitelist(),
                filteringRule.getBeginDate(),
                filteringRule.getEndDate(),
                filteringRule.getTgid(),
                filteringRule.getFilteringType(),
                filteringRule.getDateInsert(),
                filteringRule.getDateUpdate(),
                filteringRule.getComment()
        );

    }

在我的存储库中:

public interface FilteringRuleRepository extends JpaRepository<FilteringRuleEto, Integer> {

     @Query(value=theDynamicQuery)
     List<FilteringRuleEto> findRuleByParameters(String routeClass, String codeOperator, String commercialTrunk,
                                                 String supportType, String calledNumber, boolean calledNumberRange,
                                                 boolean calledNumberWhitelist, String callingNumber, boolean callingNumberRange,
                                                 boolean callingNumberWhitelist, OffsetDateTime beginDate, OffsetDateTime endDate,
                                                 String tgid, String filteringType, OffsetDateTime dateInsert, OffsetDateTime dateUpdate,
                                                 String comment);

}

目标是尽可能快地进行搜索。我不想使用17个参数的查询,当用户只想在数据库中搜索其中的2个参数时。
我跟随这个网站写代码:https://www.baeldung.com/spring-data-jpa-query
但我不认为第九部分是我要找的。或者我误解了。
谢谢你的阅读。

62o28rlo

62o28rlo1#

我已经开发了一个名为springdynamicjpa的库,以使用jpa实现动态查询更加容易。
您可以使用它来编写查询模板。根据调用方法时的参数,查询模板将在执行之前构建到不同的查询字符串中。

public interface FilteringRuleRepository extends JpaRepository<FilteringRuleEto, Integer> {
    @DynamicQuery("select t from FilteringRuleEto t\n" +
        "<@where>\n" +
        "  <#if routeClass?has_content>\n" +
        "    and t.routeClass like :routeClass\n" +
        "  </#if>\n" +
        "  <#if codeOperator?has_content>\n" +
        "    and t.codeOperator like :codeOperator\n" +
        "  </#if>\n" +
        "  <#if commercialTrunk?has_content>\n" +
        "    and t.commercialTrunk like :commercialTrunk\n" +
        "  </#if>\n" +
        "</@where>"
    )
    List<FilteringRuleEto> findRuleByParameters(String routeClass, String codeOperator, String commercialTrunk,
                                                String supportType, String calledNumber, boolean calledNumberRange,
                                                boolean calledNumberWhitelist, String callingNumber, boolean callingNumberRange,
                                                boolean callingNumberWhitelist, OffsetDateTime beginDate, OffsetDateTime endDate,
                                                String tgid, String filteringType, OffsetDateTime dateInsert, OffsetDateTime dateUpdate,
                                                String comment);
}

相关问题