IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA sort排序方法使用方式, 添加关联表的 order by

1、sort可以直接添加在命名格式的字段中

1 List<BomMain> findAllByDeleted(Integer deleted, Sort sort);
View Code

2、可以作为pageable的一个参数使用

1 Page<Originals> selectBomSeriesList(Pageable pageable);
View Code

向sort传参的方式

1、装入Pageable使用

1 Sort sort = new Sort(Sort.Direction.ASC, "serieName");
2 
3         Pageable pageable = PageRequest.of(0, 10, sort);
4 
5         Page<Originals> list = bomSeriesRepository.selectBomSeriesList(pageable);
6 
7         System.out.println(list.getTotalPages());
View Code

2、自行使用

1 Sort sort = new Sort(Sort.Direction.ASC, "sequence");
2 
3         List<BomMain> bomMainList = bomMainRepository.findAllByDeleted(1, sort);
4 
5         log.info("列表" + gson.toJson(bomMainList));
View Code

3、多个条件表关联使用

 1   List<Sort.Order> listOrder = new ArrayList<>();
 2 
 3         listOrder.add(new Sort.Order(Sort.Direction.ASC, "bomMain.sequence"));
 4 
 5         listOrder.add(new Sort.Order(Sort.Direction.ASC, "sequence"));
 6 
 7         Sort sort = Sort.by(listOrder);
 8 
 9         List<BomSub> bomSubList = bomSubRepository.findAllByDeletedAndBomMainMainId(1, "1", sort);
10 
11         log.info("列表" + gson.toJson(bomSubList));
View Code

生成SQL

 1 SELECT
 2     bomsub0_.subId AS subId1_9_,
 3     bomsub0_.mainId AS mainId8_9_,
 4     bomsub0_.deleted AS deleted2_9_,
 5     bomsub0_.engName AS engName3_9_,
 6     bomsub0_.fullName AS fullName4_9_,
 7     bomsub0_.isOnShelf AS isOnShel5_9_,
 8     bomsub0_.sequence AS sequence6_9_,
 9     bomsub0_.subName AS subName7_9_ 
10 FROM
11     bomsub bomsub0_
12     LEFT OUTER JOIN bommain bommain1_ ON bomsub0_.mainId = bommain1_.mainId 
13 WHERE
14     bomsub0_.deleted =? 
15     AND bommain1_.mainId =? 
16 ORDER BY
17     bommain1_.sequence ASC,
18     bomsub0_.sequence ASC
View Code
原文地址:https://www.cnblogs.com/liuyangfirst/p/9856254.html