Database ( DB )/JPA, Querydsl

JPA에서 like 사용하는 방법

노루아부지 2023. 4. 3. 23:08
반응형

1. @Query에 아래와 같이 LIKE CONCAT을 사용합니다.

@Query("SELECT u.username FROM User u WHERE u.username LIKE CONCAT('%',:username,'%')")
List<String> findUsersWithPartOfName(@Param("username") String username);

 

 

2. 다음은 JPA문법을 사용한 방법입니다.

Using Like: select ... like :username

List<User> findByUsernameLike(String username);


StartingWith: select ... like :username%

List<User> findByUsernameStartingWith(String username);



EndingWith: select ... like %:username

List<User> findByUsernameEndingWith(String username);


Containing: select ... like %:username%

List<User> findByUsernameContaining(String username);

 

 

 

출처: https://stackoverflow.com/questions/21456494/spring-jpa-query-with-like

 

Spring JPA @Query with LIKE

I'm trying to make a method in CrudRepository that will be able to give me list of users, whose usernames are LIKE the input parameter(not only begin with, but also contains it). I tried to use met...

stackoverflow.com

 

728x90
반응형
loading