java - 来自Spring Boot中函数参数的Concat @Query值
问题描述
我们可以通过 QUERY 从字符串中追加 Query 的值吗?如果没有,还有其他选择吗?
注意:需要在页面中返回数据
@Query(nativeQuery = true, value = "从 1=1 的表实体中选择实体" + query + "AND date =:date")
Page getSearchedTable(String query, @Param("date") LocalDate businessDate, Pageable pageable);
解决方案
您可以使用以下查询来获取可分页数据。记住几点
- 您必须在等号和参数之间留出空格,例如 (data = :date)
- 不需要给nativeQuery = true,可以使用实体字段
- 您不能在 Spring Boot 的本机查询中使用“查询”作为变量。您必须作为参数从服务传递。
- 只需在所需查询中传递您的查询参数,如 select 或根据您的请求。下面的例子。
@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);
推荐阅读
- c# - 如何在 C# 中的用户帐户控制提示后调用方法?
- python-3.x - 如何将树的所有dfs路径存储在列表中?
- json - 将 JSON 字符串作为值传递给 Spring 数据 REST 端点
- wordpress - 当购物车总金额 = 0 时,应显示货到付款
- c# - 该类型不包括任何可访问的构造函数 WPF - 无窗口应用程序
- c++ - 使用线性探测插入哈希表
- phpword - 使用 PHPWord 添加纸张大小
- html - 如何在 dart 中使用 beautifulsoup scrape 从网页中获取特定元素
- java - 如何表示 Optional 类型的属性
在 OpenAPI 3 上 - sql - H2 嵌入式 DB LIMIT + OFFSET> Integer.MAX_VALUE 导致错误结果