简体   繁体   中英

How to set like value in query parameter with JPA named query

I have a JPA query as below

 public interface MyRepository extends JpaRepository<User, String> {
  @Query(value = "select * from User where code like 'PER%'", 
  nativeQuery= true)
  public List<User> findAllUsers(String param);
 }

How do I replace the 'PER%' with?1

Thanks in advance.

public interface MyRepository extends JpaRepository<User, String> {
  @Query(value = "select * from User where code like CONCAT(:param,'%')", 
  nativeQuery= true)
  public List<User> findAllUsers(@Param("param") String param);
 }

You can use the query parameter with index ?1 Query Creation and Spring Data JPA

@Query(value = "select * from User where code like ?1%", 
nativeQuery= true)
public List<User> findAllUsers(String param);

OR passing parameters via name.

@Query(value = "select * from User where code like :param%", 
nativeQuery= true)
public List<User> findAllUsers(@Param("param")  String param);

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