how to pass parameter value into query in spring boot?

I made a custom query in JPA spring boot and I want to pass parameters as dynamic to the query, but it doesn’t work well.

this is the query:

public interface CustomerRepository extends JpaRepository<Customer, Integer>{      @Query(value = "select * from customer.customer_tbl where firstname like '%:keyword%';", nativeQuery = true)     List<Customer> findByKeyword(@Param("keyword") String keyword); } 
Add Comment
2 Answer(s)

Here '%:keyword%' is a string literal so param inside is not replaced.

Use CONCAT('%',:keyword,'%')

@Query(value = "select * from customer.customer_tbl where firstname like CONCAT('%',:keyword,'%');", nativeQuery = true) List<Customer> findByKeyword(@Param("keyword") String keyword); 
Answered on July 16, 2020.
Add Comment

Try this.

public interface CustomerRepository extends JpaRepository<Customer, Integer>{          @Query(value = "select * from customer.customer_tbl where firstname like CONCAT('%', :keyword, '%');", nativeQuery = true)         List<Customer> findByKeyword(@Param("keyword") String keyword);     } 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.