Spring Data-多列搜索 [英] Spring Data - Multi-column searches

查看:63
本文介绍了Spring Data-多列搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data进行分页和排序.但是,我想执行多列搜索.

I am using Spring Data for the paging and the sorting. However, I would like to perform multi-columns searches.

现在,我正在像这样的存储库界面中使用注释 @Query :

Now, I am using the annotation @Query in my repository interface like this:

public interface MyRepository extends PagingAndSortingRepository<Item,Long> {

    @Query(value="select mt from MY_TABLE mt where mt.field1 = %searchtext% or mt.field2 = %searchtext% or mt.field3 = %searchtext%")    
    Page<Item> findByAllColumns(@Param("searchtext") String searchtext, Pageable pageable);

}

edit:此解决方案中的问题在于@Query注释的where子句,因为我们必须为要搜索的每一列重复完全相同的searchtext参数(问题的澄清)在布兰登·奥克利(Brandon Oakley)评论后)

edit: The problem in this solution is in the where clause of the @Query annotation because we have to repeat the exact same searchtext parameter for every column we want to search on (clarification of the question after the comment of Brandon Oakley)

我想知道是否还有另一种方法可以执行,因为表中的列数可能很高.

I would like to know if there is another way to do because the number of columns in a table can be high.

感谢您的帮助.

推荐答案

您可以使用

You could use specifications. That also gives you more flexibility. You can have one method, but use multiple specifications for a query:

Page<Item> findAll(Specification<T> spec, Pageable pageable);

myRepository.findAll(textInAllColumns(searchText), pageable);

这篇关于Spring Data-多列搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆