Tag Archives: Springdatajpa

Springdatajpa @query with like @param

Direct to the code

//Using :name this way must be added, cut @Param is org.springframework.data.repository.query.Param package below the note
    @Query(nativeQuery = true, value = "select a.id,a.name,a.code,a.age,b.age as sub_age from dog a inner join dog b  on a.age = b.age where a.name like concat('%',:name ,'%')")
    List<Dog> findAllByAgeAfter2(@Param("name") String name);

    //The use of ?1 does not require the addition of @Param, and the addition of @Param does not affect
    @Query(nativeQuery = true, value = "select a.id,a.name,a.code,a.age,b.age as sub_age from dog a inner join dog b  on a.age = b.age where a.name like  CONCAT('%',?1,'%')")
    List<Dog> findAllByAgeAfter(String name);

The test is as follows:

I hope I can help you, thank you!