【问题标题】:creating Spring Repository Select using multiple "like" and Pageable使用多个“喜欢”和可分页创建 Spring Repository Select
【发布时间】:2017-09-28 19:39:24
【问题描述】:

我正在使用 H2-Database 和 Spring-Data PagingRepository。我喜欢使用分页并从数据库中选择所有 articles 字段 html 包含 '@''[at]' em> 或 '(at)''mail' ...

所以没有分页的原生查询看起来像

SELECT * FROM article  WHERE (html LIKE '%@%' or html LIKE '%(at)%' or html LIKE '%[at]%' or ...)

我试着这样解决它:

Page<Article> findByHtmlContainingOrderByDateDesc(String[] string,  Pageable pageable);

但是没有/空的结果。

我尝试的第二种方法是使用本机查询:

@Query(value = "SELECT * FROM article  WHERE (html LIKE '%@%' or html LIKE '%(at)%') ORDER BY DATE desc \n#pageable\n" , 
nativeQuery = true,
countQuery = "SELECT count(*) FROM article  WHERE (html LIKE '%@%' or html LIKE '%(at)%')")
Page<Article> findAll(@Param("pageable") Pageable pageable);

但我遇到了异常:

嵌套异常是 org.hibernate.exception.SQLGrammarException: could not prepare statement] 根本原因

非常感谢您的帮助!

【问题讨论】:

标签: java sql spring-data h2


【解决方案1】:

对我有用的解决方案是将\n-- #pageable\n 添加到自定义查询中。所以我最终在

@Query(value = "SELECT * FROM article  WHERE (html LIKE '%@%' or html LIKE '%(at)%') ORDER BY DATE desc \n-- #pageable\n" , nativeQuery = true,
    countQuery = "SELECT count(*) FROM article  WHERE (html LIKE '%@%' or html LIKE '%(at)%')")
Page<Article> findAll(@Param("pageable") Pageable pageable);

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 2012-09-26
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2012-09-15
    • 2023-03-27
    相关资源
    最近更新 更多