spring data jpa 多表UNION ALL查询按条件排序分页处理:未搜到方法,解决后记录:2018年11月13日15:22:00

需求:Mysql数据库 有不同属性的两张表,需要进行按某个条件查询,结果合并排序分页。

让产品把两个表分成两段展示各查各的,分开来。

产品经理说能实现:产品写sql 联合查询, A UNION ALL B 。

那开发就处理后续排序分页了。

dao层

@Query(nativeQuery = true,value = "SELECT re.id,re.i_amount,re.i_share,re.create_time,re.type FROM ( " +
        "SELECT id AS id, i_amount AS i_amount, i_share AS i_share, create_time AS create_time , 1 AS type " +
        "FROM f_project where project_id = :projectId " +
        "UNION ALL " +
        "SELECT id AS id, i_amount AS i_amount, i_share AS i_share, create_time AS create_time ,2 AS type " +
        "FROM l_project  where project_id = :projectId  ) re"
        ,countQuery = "SELECT count(re.id) FROM ( SELECT id  FROM f_project  where project_id = :projectId UNION ALL SELECT id  FROM l_project  where project_id = :projectId  )  as re")
Page> find(@Param("projectId") long projectId, Pageable pageable);

service层调用 

Pageable pageable = PageRequest.of(pageQueryDto.getPage() - 1, pageQueryDto.getSize(), Sort.Direction.fromString(pageQueryDto.getDirection()), propertie);
Page> page = projectRepository.find(projectId, pageable);

 

你可能感兴趣的:(springboot,jpa)