简体   繁体   中英

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 method "findUserByUsernameLike(@Param("username") String username)" but as it is told in Spring documentation, this method is equal to " where user.username like?1 ". It is not good for me, as I already told that I'm trying to get all users whose username contains...

I wrote a queryto the method but it even doesn't deploy.

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

@Query("select u from user u where u.username like '%username%'")
List<User> findUserByUsernameLike(@Param("username") String username);
}

Can anybody help me with this?

Try to use the following approach (it works for me):

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

Notice: The table name in JPQL must start with a capital letter.

Using Query creation from method names , check table 4 where they explain some keywords.

  1. Using Like: select ... like :username

     List<User> findByUsernameLike(String username);
  2. StartingWith: select ... like :username%

     List<User> findByUsernameStartingWith(String username);
  3. EndingWith: select ... like %:username

     List<User> findByUsernameEndingWith(String username);
  4. Containing: select ... like %:username%

     List<User> findByUsernameContaining(String username);

Notice that the answer that you are looking for is number 4 . You don't have to use @Query

Another way: instead CONCAT function we can use double pipe: :lastname || '%'

@Query("select c from Customer c where c.lastName LIKE :lastname||'%'")
List<Customer> findCustomByLastName( @Param("lastname") String lastName);

You can put anywhere, prefix, suffix or both

:lastname ||'%'  
'%' || :lastname  
'%' || :lastname || '%'  

List<User> findByUsernameContainingIgnoreCase(String username);

为了忽略案例问题

Easy to use following (no need use CONCAT or ||):

@Query("from Service s where s.category.typeAsString like :parent%")
List<Service> findAll(@Param("parent") String parent);

Documented in: http://docs.spring.io/spring-data/jpa/docs/current/reference/html .

For your case, you can directly use JPA methods. That is like bellow:

Containing: select ... like %:username%

List<User> findByUsernameContainingIgnoreCase(String username);

here, IgnoreCase will help you to search item with ignoring the case.

Here are some related methods:

  1. Like findByFirstnameLike

    … where x.firstname like ?1

  2. StartingWith findByFirstnameStartingWith

    … where x.firstname like ?1 (parameter bound with appended %)

  3. EndingWith findByFirstnameEndingWith

    … where x.firstname like ?1 (parameter bound with prepended %)

  4. Containing findByFirstnameContaining

    … where x.firstname like ?1 (parameter bound wrapped in %)

More info , view this link and this link

Hope this will help you :)

This way works for me, (using Spring Boot version 2.0.1. RELEASE):

@Query("SELECT u.username FROM User u WHERE u.username LIKE %?1%")
List<String> findUsersWithPartOfName(@Param("username") String username);

Explaining: The ?1, ?2, ?3 etc. are place holders the first, second, third parameters, etc. In this case is enough to have the parameter is surrounded by % as if it was a standard SQL query but without the single quotes.

You Missed a colon(:) before the username parameter. therefore your code must change from:

@Query("select u from user u where u.username like '%username%'")

to :

@Query("select u from user u where u.username like '%:username%'")

this @Query("select u from user u where u.username like '%:username%'") does not work in some cases!

working solution for me was

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

If you are using Spring-Data-Jpa +2.7 there is a bug with LIKE parameter using %:param% in queries. It will be fixed in version 3.

https://github.com/spring-projects/spring-data-jpa/issues/2548

@Query("select b.equipSealRegisterId from EquipSealRegister b where b.sealName like %?1% and b.deleteFlag = '0'" )
    List<String>findBySeal(String sealname);

I have tried this code and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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