繁体   English   中英

Trim() Function 与 Spring JPA 类似查询方法

[英]Trim() Function with Spring JPA like Query Methods

我正在制作一个 rest 服务,该服务在内部使用 JPA 来查找带有前缀的数据(例如:信息前缀)。

我使用 Spring 数据的查询方法如下所示:

List<Entity> findAllByFieldStartsWithIgnoreCase(String field);

一切正常,但是,数据库中的数据具有 Information 前缀,但也有空格,如下例所示:

场地:

Information 1
    Information 2
 Information 3
Information 4 

就像您在上面的示例中看到的那样,信息 1 没有一些空白,但信息 2 和 3 有一个或多个空白。

I am looking the way to ignore these spaces, I know that sql has the trim function to make it, but in JPA repository with Spring , with query methods, does not exist:

我想找一个 function 来键入这样的查询方法:

List<Entity> findAllByField  Trim   StartsWithIgnoreCase(String field);

为了在 StartWith 之前执行修剪 function。

我会感谢你的帮助,问候。

您可以使用@Query注释在此处使用 JPQL 查询。 例如,下面的代码片段应该给你一个修剪列的前缀搜索

@Query("select e from Entity e where trim(e.field) like concat(:field,'%')")
List<Entity> findAllByFieldStartsWithIgnoreCase(@Param("field") String field);

这是有关如何将本机/JPQL 查询与 Hibernate 一起使用的资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM