spring data jpa 动态查询 @Query注解实现
·
如果利用@Query就行分页主要用的属性有
nativeQuery
value
countQuery
@Query(nativeQuery = true
,value = "你的sql"
, countQuery = "你的sql ")
Page<实体类> queryUser(@Param("vo") VO vo, @Param("pageable") Pageable pageable);
这里需要注意 nativeQuery countQuery 中的sql条件需要一样
看下简单的例子
@Query(nativeQuery = true
,value = "select u.id,user.name,g.goodsName from user u left join goods g on g.user_id = u.id where g.name=:#{#vo.goodsName}"
, countQuery = "select count(u.id) from user u left join goods g on g.user_id = u.id where g.name=:#{#vo.goodsName}")
Page<实体类> queryUser(@Param("vo") VO vo, @Param("pageable") Pageable pageable);
如果你需要动态查询条件使用sql的 if 函数 if(condition, value_if_true, value_if_false)
例如: 可以像下面这么写条件
where 1=1 and if(:#{#vo.name !='',g.name=:#{#vo.goodsName},1=1)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)