【问题标题】:%Like% Query in spring JpaRepositorySpring JpaRepository 中的 %Like% 查询
【发布时间】:2014-10-11 07:42:42
【问题描述】:

我想在JpaRepository 中写一个类似的查询,但它没有返回任何内容:

LIKE '%place%'-它不工作。

LIKE 'place' 完美运行。

这是我的代码:

@Repository("registerUserRepository")
public interface RegisterUserRepository extendsJpaRepository<Registration,Long> {

    @Query("Select c from Registration c where c.place like :place")
     List<Registration> findByPlaceContaining(@Param("place")String place);
}

【问题讨论】:

    标签: java jpa spring-data-jpa


    【解决方案1】:

    可以有多种方法。正如许多人的回答中提到的,如果可能的话,您可以使用 JPA 预定义的模板查询。

    List<Registration> findByPlaceContainingIgnoreCase(String place);
    

    另外,你可以在调用上述方法之前在java层附加'%'。

    如果查询比较复杂,一般可以使用@Query one

    @Query("Select r from Registration r where r.place like '%' || :place || '%'")
    

    为了可读性,你可以使用下面的一个

    @Query("Select r from Registration r where r.place like CONCAT('%', :place, '%'")
    

    【讨论】:

      【解决方案2】:

      我们是这样的

      @Query("来自 CasFhgDeviceView where deviceGroupName like concat(concat('%CSGW%', :usid), '%') ")

      【讨论】:

      • 此答案是否提供比其他答案更多的信息?如果是这样,请edit 它并添加有关您的答案有助于解决问题的信息。如果不是,请删除您的答案。谢谢。
      【解决方案3】:

      您可以在参数后简单地说“Like”关键字..

      List<Employee> findAllByNameLike(String name);
      

      【讨论】:

        【解决方案4】:

        对于您的情况,您可以直接使用 JPA 方法。该代码如下所示:

        包含:select ... like %:place%

        List<Registration> findByPlaceContainingIgnoreCase(String place);
        

        这里,IgnoreCase将帮助您在忽略大小写的情况下搜索项目。

        在 JPQL 中使用 @Query

        @Query("Select registration from Registration registration where 
        registration.place LIKE  %?1%")
        List<Registration> findByPlaceContainingIgnoreCase(String place);
        

        下面是一些相关的方法:

        1. 点赞 findByPlaceLike

          ... x.place like ?1 的位置

        2. StartingWith findByPlaceStartingWith

          ... x.place like ?1 (参数与附加 % 绑定)

        3. EndingWith findByPlaceEndingWith

          ... x.place like ?1 (参数与前置 % 绑定)

        4. 包含 findByPlaceContaining

          ...其中 x.place like ?1(以 % 包裹的参数绑定)

        更多信息,查看this linkthis linkthis

        希望对你有帮助:)

        【讨论】:

        • 如何针对视图执行此操作?
        【解决方案5】:

        您实际上根本不需要@Query 注释。

        你可以使用下面的

            @Repository("registerUserRepository")
            public interface RegisterUserRepository extends JpaRepository<Registration,Long>{
            
            List<Registration> findByPlaceIgnoreCaseContaining(String place);
        
            }
        

        【讨论】:

        • 对我来说这是最好的解决方案。我不知道您可以将 IgnoreCase 与 Containing 结合使用,它不在文档中。
        • 我想使用多列的情况怎么办? - 我相信@Query() 是必须的,对吧?使用这种方法,我将不得不制作 or 这实际上不是一个非常适合这种情况的解决方案:findByField1OrField2Containg(String phraseForField1, String phraseForField2)
        【解决方案6】:

        我用这个:

        @Query("Select c from Registration c where lower(c.place) like lower(concat('%', concat(:place, '%')))")
        

        lower() 类似于 String 中的 toLowerCase,因此结果不区分大小写。

        【讨论】:

        • 我使用相同的方法,但是当下部函数中的变量为空时,我收到以下错误,如何添加空检查?函数 lower(bytea) 不存在
        【解决方案7】:

        找到没有@Query 的解决方案(实际上我尝试了“接受”的解决方案。但是,它没有用)。

        必须返回Page&lt;Entity&gt; 而不是List&lt;Entity&gt;

        public interface EmployeeRepository 
                                  extends PagingAndSortingRepository<Employee, Integer> {
            Page<Employee> findAllByNameIgnoreCaseStartsWith(String name, Pageable pageable);
        }
        

        IgnoreCase 部分对于实现这一目标至关重要!

        【讨论】:

          【解决方案8】:

          试试这个。

          @Query("Select c from Registration c where c.place like '%'||:place||'%'")
          

          【讨论】:

          • 如果我们在查询中使用 something=:placeanother like %:place% 会很有帮助。
          【解决方案9】:

          答案将是

          -->` @Query("select u from Category u where u.categoryName like %:input%") 列出 findAllByInput(@Param("input") 字符串输入);

          【讨论】:

            【解决方案10】:

            当调用函数时,我使用: findByPlaceContaining("%" + place);

            或: findByPlaceContaining(place + "%");

            或: findByPlaceContaining("%" + place + "%");

            【讨论】:

              【解决方案11】:

              您还可以使用 Spring Data JPA 支持的关键字 "Containing" 实现类似查询。

              List<Registration> findByPlaceContaining(String place);
              

              【讨论】:

                【解决方案12】:

                您可以使用占位符的另一种方法:

                @Query("Select c from Registration c where c.place LIKE  %?1%")
                List<Registration> findPlaceContainingKeywordAnywhere(String place);
                

                【讨论】:

                  【解决方案13】:

                  spring 数据 JPA 查询需要 "%" 字符以及查询中 like 后面的空格字符,如

                  @Query("Select c from Registration c where c.place like %:place%")

                  参照。 http://docs.spring.io/spring-data/jpa/docs/current/reference/html.

                  您可能希望完全摆脱@Queryannotation,因为它似乎类似于标准查询(由弹簧数据代理自动实现);即使用单行

                  List<Registration> findByPlaceContaining(String place);
                  

                  就足够了。

                  【讨论】:

                  • 忽略查询的大小写也可能很有用:findByPlaceIgnoreCaseContaining(String place);
                  • 作为旁注 - 您不能在同一个查询中混合和匹配 %:param% 和 %param%。否则应用程序将无法启动。
                  • 谢谢。我以前用'like '%:place%',根本没有结果,因为Hibernate自己把's加到字符串中。
                  • @Query("Select c from Registration c where c.place like %:place%").在 Spring Boot 版本 2.3.3 中不起作用。你能告诉我为什么我可以在使用 2.3.3 时让它工作
                  • @SubhaBhowmik 哪个版本不工作?您说 2.3.3 可以正常工作,但不能正常工作。我也在使用 Spring Boot 2.4.0 与 %:param% 苦苦挣扎,但发现你不能在 SQL 函数中使用 %:param%:F.e.在 LOWER('%:param%') 中, :param 由于引号而未被替换。希望对你有帮助。
                  猜你喜欢
                  • 2021-03-24
                  • 2019-06-16
                  • 2016-03-16
                  • 2021-10-13
                  • 2018-05-01
                  • 2018-07-14
                  • 2021-08-21
                  • 2018-10-30
                  • 1970-01-01
                  相关资源
                  最近更新 更多