其他分享
首页 > 其他分享> > SpringDataJPA之@Query with LIKE(JPA模糊查询) @Param

SpringDataJPA之@Query with LIKE(JPA模糊查询) @Param

作者:互联网

直接上代码

 

//使用:name这种方式必须加,切@Param是org.springframework.data.repository.query.Param包下面的注解
    @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);

    //使用?1这种方式不需要加@Param,加了也不影响
    @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);

 

测试如下:

 

希望可以帮助到您,谢谢!

标签:SpringDataJPA,LIKE,JPA,age,dog,Param,String,Query,name
来源: https://blog.csdn.net/weixin_42767099/article/details/114097868