简体   繁体   English

使用子句和类似列表将 sql 转换为 JPA

[英]Convert sql to JPA with a in-clause and a list of like

I have a sql statement as below and want to convert it to JPA我有如下 sql 语句,并希望将其转换为 JPA

SELECT * FROM employee
where user_id in ('id1', 'id2', 'id3')
AND (
    first_name like '%name1%' OR last_name like '%name1%'
    OR
    first_name like '%name2%' OR last_name like '%name2%'
    )
ORDER BY user_id ASC;


Input parameter输入参数

userIdList - a list of string to exact match user_id of size [1,100] userIdList - 与大小 [1,100] 的 user_id 完全匹配的字符串列表

nameList - a list of string to like-wise match first_name or last_name of size [1,100] in character [A-Za-z0-9-_ ] nameList - 在字符 [A-Za-z0-9-_ ] 中同样匹配大小为 [1,100] 的 first_name 或 last_name 的字符串列表

example for name list: ['Joe', 'Peter', 'White', 'X Y']名单示例:['Joe', 'Peter', 'White', 'XY']


I have no idea about how to handle the List of like part and the closest I have is without the list of like我不知道如何处理类似部分的列表,而我拥有的最接近的是没有类似列表

@Modifying
@Query(value = "select * from employee where user_id IN :ids ORDER BY user_id",
            nativeQuery = true)
List<UserGroupTable> findAllByUserIdList(@Param("ids") List<String> userIdList);

In clause reference: %Like% Query in spring JpaRepository在子句参考: spring JpaRepository 中的 %Like% 查询

Assuming the intention is for name1 and name2 to also be parameters, you only need to finish out your JPA query:假设意图是name1name2也是参数,您只需要完成您的 JPA 查询:

select *
from employee
where user_id IN :ids and (
      first_name like :name1 or last_name like :name1 or
      first_name like :name2 or last_name like :name2)
order by user_id

Your updated Java method:您更新的 Java 方法:

@Modifying
@Query(value = "select * from employee where user_id IN :ids and (first_name like :name1 or last_name like :name1 or first_name like :name2 or last_name like :name2) order by user_id",
    nativeQuery = true)
List<UserGroupTable> findAllByUserIdList(@Param("ids") List<String> userIdList, @Param("name1") String name1, @Param("name2") String name2);

Note carefully here that you bind to :name1 and :name2 the actual wildcard expression.请注意,您绑定到:name1:name2实际的通配符表达式。 Eg for a name of John Doe , you would bind %John% and %Doe% to the two placeholders.例如,对于John Doe的名称,您可以将%John%%Doe%绑定到两个占位符。

A regex query could be one way to achieve this, as described here: Searching multiple colums with a Contains query with a list of Items in Spring-Data JPA正则表达式查询可能是实现此目的的一种方法,如下所述: Searching multiple colums with a Contains query with a list of Items in Spring-Data JPA

Sample for MariaDB / MySQL : MariaDB / MySQL样品:

@Query(nativeQuery = true, value = "SELECT * FROM my_entity WHERE "
      + "first_name REGEXP CONCAT_WS('|', :nameslist) " + "OR "
      +  "last_name REGEXP CONCAT_WS('|', :nameslist) ")
    List<MyEntity> findByNames( // (method name does not matter)
                     @Param("nameslist") Collection<String> nameslist);

( CONCAT_WS joins the provided list, eg ('val1', 'val2', 'val3') to the regex 'val1|val2|val3' , which would match eg 'complete val2 xxx'.) CONCAT_WS加入提供的列表,例如('val1', 'val2', 'val3')到正则表达式'val1|val2|val3' ,这将匹配例如 'complete val2 xxx'。)

Note that the exact REGEXP / CONCAT_WS functions depend on the used DB.请注意,确切的 REGEXP / CONCAT_WS 函数取决于使用的 DB。

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

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