如何使用 JPA 命名查询在查询参数中设置类似值

我有一个 JPA 查询如下


 public interface MyRepository extends JpaRepository<User, String> {

  @Query(value = "select * from User where code like 'PER%'", 

  nativeQuery= true)

  public List<User> findAllUsers(String param);

 }

如何将“PER%”替换为 ?1


提前致谢。


慕的地8271018
浏览 87回答 2
2回答

收到一只叮咚

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

万千封印

您可以将查询参数与索引?1 查询创建和Spring Data JPA一起使用@Query(value = "select * from User where code like ?1%", nativeQuery= true)public List<User> findAllUsers(String param);或者通过名称传递参数。@Query(value = "select * from User where code like :param%", nativeQuery= true)public List<User> findAllUsers(@Param("param")  String param);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java