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

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

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

问题

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

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?

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

答案1

得分: 0

不需要为此创建流因为Spring Data JPA已经有重载的方法允许排序您只需要将参数转换为必要的类型例如

    //在这里传递要按其排序的字段的名称,而不是实际值
    public List<BillToPay> findWithOrderBy(String fieldName, String direction) {
        Sort.Direction dir = direction.equalsIgnoreCase("asc") ? Sort.Direction.ASC 
                : Sort.Direction.DESC;
        List<BillToPay> listBillToPay = this.repository.findAll(Sort.by(dir, fieldName));
        return listBillToPay;
    }

JPA方法接受一个可变的字符串参数因此假设您想按相同方向排序多个字段不是ASC/DESC混合),您可以轻松实现它并几乎不改变任何内容

    //方向必须首先出现,因为变量参数必须出现在签名的最后
    public List<BillToPay> findWithOrderBy(String direction, String...fieldNames) {
        Sort.Direction dir = direction.equalsIgnoreCase("asc") ? Sort.Direction.ASC 
                : Sort.Direction.DESC;
        List<BillToPay> listBillToPay = this.repository.findAll(Sort.by(dir, fieldNames));
        return listBillToPay;
    }
英文:

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:

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

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.

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

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:

确定