我有一个 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提前致谢。
2 回答

收到一只叮咚
TA贡献1821条经验 获得超4个赞
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);
}

万千封印
TA贡献1891条经验 获得超3个赞
您可以将查询参数与索引?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);
添加回答
举报
0/150
提交
取消