如何使用Spring JPA在请求中订购接收参数的流

huangapple go评论158阅读模式
英文:

How to order a stream receiving parameter in the request using spring JPA

问题

  1. public List<BillToPay> findWithOrderBy(String orderName, String orderDir) {
  2. List<BillToPay> listBillToPay = this.repository.findAll(Sort.by(orderDir.equals("ASC") ? Sort.Direction.ASC : Sort.Direction.DESC, orderName));
  3. }
英文:

I need to order the stream from the parameters received above, how can I do this safely and that it works in an ASC and DESC way?

  1. public List&lt;BillToPay&gt; findWithOrderBy(String orderName, String orderDir) {
  2. List&lt;BillToPay&gt; listBillToPay = this.repository.findAll();
  3. }

答案1

得分: 0

  1. 不需要为此创建流因为Spring Data JPA已经有重载的方法允许排序您只需要将参数转换为必要的类型例如
  2. //在这里传递要按其排序的字段的名称,而不是实际值
  3. public List<BillToPay> findWithOrderBy(String fieldName, String direction) {
  4. Sort.Direction dir = direction.equalsIgnoreCase("asc") ? Sort.Direction.ASC
  5. : Sort.Direction.DESC;
  6. List<BillToPay> listBillToPay = this.repository.findAll(Sort.by(dir, fieldName));
  7. return listBillToPay;
  8. }
  9. JPA方法接受一个可变的字符串参数因此假设您想按相同方向排序多个字段不是ASC/DESC混合),您可以轻松实现它并几乎不改变任何内容
  10. //方向必须首先出现,因为变量参数必须出现在签名的最后
  11. public List<BillToPay> findWithOrderBy(String direction, String...fieldNames) {
  12. Sort.Direction dir = direction.equalsIgnoreCase("asc") ? Sort.Direction.ASC
  13. : Sort.Direction.DESC;
  14. List<BillToPay> listBillToPay = this.repository.findAll(Sort.by(dir, fieldNames));
  15. return listBillToPay;
  16. }
英文:

You don't need a stream for this as Spring Data JPA already has overloaded methods that allow sorting. You just need to convert your arguments to the necessary type such as:

  1. //Pass the name of the field you want to sort by here, not the actual value
  2. public List&lt;BillToPay&gt; findWithOrderBy(String fieldName, String direction) {
  3. Sort.Direction dir = direction.equalsIgnoreCase(&quot;asc&quot;) ? Sort.Direction.ASC
  4. : Sort.Direction.DESC;
  5. List&lt;BillToPay&gt; listBillToPay = this.repository.findAll(Sort.by(dir, fieldName));
  6. return listBillToPay;
  7. }

The JPA method accepts a variable String argument, so assuming you want to sort by multiple fields all in the same direction (not a mix of ASC/DESC) you could easily implement it and barely change anything.

  1. //Direction needs to come first as variable parameter must come last in the signature
  2. public List&lt;BillToPay&gt; findWithOrderBy(String direction, String...fieldNames) {
  3. Sort.Direction dir = direction.equalsIgnoreCase(&quot;asc&quot;) ? Sort.Direction.ASC
  4. : Sort.Direction.DESC;
  5. List&lt;BillToPay&gt; listBillToPay = this.repository.findAll(Sort.by(dir, fieldNames));
  6. return listBillToPay;
  7. }

huangapple
  • 本文由 发表于 2023年3月7日 22:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定