【问题标题】:Concat @Query value from function parameter in spring boot [duplicate]Spring Boot中函数参数的Concat @Query值[重复]
【发布时间】:2019-10-08 12:05:38
【问题描述】:

我们可以通过 QUERY 从字符串中追加 Query 的值吗? 如果没有,还有其他选择吗?

注意:需要在页面中返回数据

@Query(nativeQuery = true, value = "从表实体中选择实体 1=1" + 查询 + "AND 日期 =:日期")

Page getSearchedTable(String query, @Param("date") LocalDate businessDate, Pageable pageable);

【问题讨论】:

  • 您是在尝试动态传递查询字符串,对吗?
  • 是的,先生,正在尝试连接查询。

标签: java spring-boot jpa spring-data-jpa


【解决方案1】:

您可以使用以下查询来获取可分页数据。记住几点

  1. 您必须在等号和参数(如 (data = :date))之间留出空格
  2. 无需给nativeQuery = true,可以使用实体字段
  3. 您不能在 Spring Boot 的本机查询中使用“查询”作为变量。您必须作为参数从服务中传递。
  4. 只需在所需查询中传递您的查询参数,如选择或根据您的要求。下面的例子。

@Query(value = "select entity from table entity where  1=1 AND date = :date AND query = :query")
Page<YourTableEntity> getSearchedTable(@Param("query") String query, @Param("businessDate") LocalDate businessDate, Pageable pageable);

但是,如果您必须强制使用 nativeQuery,那么您可以选择以下任何查询。


1.第一个选项

 @Query(
      value = "select entity from table entity where  1=1 AND date = :date AND query = :query ORDER BY id", 
      countQuery = "SELECT count(*) FROM table", 
      nativeQuery = true)
      Page<YourTableEntity> getSearchedTable(@Param("query") String query, @Param("businessDate") LocalDate businessDate, Pageable pageable);

2。第二种选择

@Query(value = "select entity from table entity where  1=1 AND date = :date AND query = :query ORDER BY id \n-- #pageable\n", 
      countQuery = "SELECT count(*) FROM table",
      nativeQuery = true)
      Page<YourTableEntity> getSearchedTable(@Param("query") String query, 
      @Param("businessDate") LocalDate businessDate, Pageable pageable);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2019-04-19
    • 2018-10-28
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多