创建 Spring 存储库 使用多个"like"和可分页选择



我正在使用H2数据库和Spring-Data PagingRepository。我喜欢使用分页,然后从数据库中选择所有 atricles field html 包含'@'@''[at]''(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#pageablen" , 
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:无法准备语句]

非常感谢您的帮助!

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

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

最新更新