猿问

如何显示以特定数字开头的所有值

我正在创建一个搜索来过滤一些从 100 到 600 的整数值。这些值是Http Status Codes,所以我只想过滤它们。
搜索的工作方式如下:

  1. 用户输入搜索值,例如2并单击搜索

  2. 结果将是从200 到 299的所有值(因此所有值都以 2 开头)。

  3. 用户输入一个值,例如20,然后单击搜索

  4. 结果将是从200 到 209的所有值(因此所有值都以 20 开头)。

  5. 用户输入一个值,例如52,然后单击搜索

  6. 结果将是从520 到 529 的所有值(因此所有值都以 52 开头)

我写了一些非常多余的代码,但基本上解释了它应该如何工作:

   public void getHttpStatus(Integer httpStatus){

            if(httpStatus.equals(1)){

                messageRepository.findByHttpStatusBetween(100, 199);

            }

            else if(httpStatus.equals(2)){

                messageRepository.findByHttpStatusBetween(200, 299);

            }

            else if(httpStatus.equals(3)){

                messageRepository.findByHttpStatusBetween(300, 399);

            }

            else if(httpStatus.equals(4)){

                messageRepository.findByHttpStatusBetween(400, 499);

            }

            else if(httpStatus.equals(5)){

                messageRepository.findByHttpStatusBetween(500, 599);

            }

            else if(httpStatus.equals(10)){

                messageRepository.findByHttpStatusBetween(100, 109);

            }

            else if(httpStatus.equals(20)){

                messageRepository.findByHttpStatusBetween(200, 209);

            }

            else if(httpStatus.equals(30)){

                messageRepository.findByHttpStatusBetween(300, 309);

            }

            else if(httpStatus.equals(40)){

                messageRepository.findByHttpStatusBetween(400, 409);

            }

            else if(httpStatus.equals(50)){

                messageRepository.findByHttpStatusBetween(500, 509);

            }

            else if(httpStatus.equals(21)){

                messageRepository.findByHttpStatusBetween(210, 219);

            }

            ...

        }

有没有更简单的方法来做到这一点?或者是否有任何内置的 spring 方法可以自动执行此操作?


我将不胜感激任何建议。


哈士奇WWW
浏览 161回答 4
4回答

交互式爱情

假设您有一个包含所有状态代码的列表,您可以使用流过滤器,即List<Integer> httpCodes;String prefix = "5"List<Integer> filteredResults = httpCodes.stream().filter(value -> value.toString().startsWith(prefix)).collect(Collectors.toList());

长风秋雁

我可能会这样做:public void getHttpStatus(Integer httpStatus){&nbsp;int numberOfDigits = (int) (Math.log10(number) + 1);&nbsp;int minStatus = httpStatus * ((int) Math.pow(10, 3 - numberOfDigits));&nbsp;int maxStatus = (httpStatus + 1) * ((int) Math.pow(10, 3 - numberOfDigits)) - 1&nbsp;messageRepository.findByHttpStatusBetween(minStatus,maxStatus)}或者你可以这样做,String status=&nbsp; httpStatus.toString();String startIndex = status;String endIndex = status;if ( status.length() == 1 ){&nbsp; &nbsp; startIndex = status + "00";&nbsp; &nbsp; endIndex = status + "99";}else if ( status.length() == 2 ){&nbsp; &nbsp; startIndex = status + "0";&nbsp; &nbsp; endIndex = status + "9";}int sIndex = Integer.parseInt( startIndex );int eIndex = Integer.parseInt( endIndex );messageRepository.findByHttpStatusBetween(sIndex, eIndex);

动漫人物

将代码存储为字符串,编写自定义查询以查找“LIKE '50%'”等条目(LIKE '$1%')。

POPMUISE

public class Range {&nbsp; &nbsp; public int lb;&nbsp; &nbsp; public int ub;&nbsp; &nbsp; public Range(int lb, int ub) {&nbsp; &nbsp; &nbsp; &nbsp; this.lb = lb;&nbsp; &nbsp; &nbsp; &nbsp; this.ub = ub;&nbsp; &nbsp; }&nbsp; &nbsp; public Range(int statusCode) {&nbsp; &nbsp; &nbsp; &nbsp; int length = (int) (Math.log10(statusCode) + 1);&nbsp; &nbsp; &nbsp; &nbsp; if (length == 0 || length > 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Cant parse status code of invald length: " + length);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (length == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.lb = statusCode * 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.ub = ((statusCode + 1) * 100) - 1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.lb = statusCode * 10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.ub = ((statusCode + 1) * 10) - 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Range r1 = new Range(52);&nbsp; &nbsp; &nbsp; &nbsp; Range r2 = new Range(2);&nbsp; &nbsp; &nbsp; &nbsp; Range r3 = new Range(20);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Lowerbound: " + r1.lb);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Upperbound: " + r1.ub);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Lowerbound: " + r2.lb);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Upperbound: " + r2.ub);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Lowerbound: " + r3.lb);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Upperbound: " + r3.ub);&nbsp; &nbsp; }}输出如下:Lowerbound: 520Upperbound: 529Lowerbound: 200Upperbound: 299Lowerbound: 200Upperbound: 209不检查特殊值 0。然后你的函数可以重构为:public void getHttpStatus(Integer httpStatus){&nbsp; &nbsp; &nbsp; &nbsp; Range r = new Range(httpStatus.intValue());&nbsp; &nbsp; &nbsp; &nbsp; messageRepository.findByHttpStatusBetween(r.lb, r.ub);}
随时随地看视频慕课网APP

相关分类

Java
我要回答