对于多表联查需要使用springdata jpa的@Query标注实现,例如最代码的我的私信列表的查询:public static final String POSTREPOSITORY_FINDALLBYTYPEANDGROUPBYUSERID = "select id from (select id,target_id,case when user_id=?1 and type=?2 then 'sender' else 'receiver' end flag from javaniu_post where user_id=?1 and type=?2 union select id,user_id,case when user_id=?1then 'sender' else 'receiver' end flag from javaniu_post where target_id=?1 and type=?2 order by id desc) as ret group by target_id order by id desc";@Query(value = RepositoryQuery.POSTREPOSITORY_FINDALLBYTYPEANDGROUPBYUSERID, nativeQuery = true)
List findAllByTypeAndGroupByUserId(long userId, int type);
调用:List _ids = postRepository.findAllByTypeAndGroupByUserId(userId, type);
这样就获取到所有其他用户和我的私信最新的列表
参考