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); }
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);
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); }