首页  >  问答  >  正文

限制 Java Spring Boot 仅接受唯一的月年组合

<p>如何在 Java Spring Boot 中仅允许输入一个月-一年的组合。我需要确保用户(邮递员客户端)可以在当年每月输入一个记录(随机生成的值)。例如:月份:二月 -> 年:2020 -> generatedValue = <em>随机的</em>。当用户再次输入 2020 年 2 月时,它会抛出异常。我尝试过将年份和月份存储在单独的列表中,以检查数据库中是否已存在该年份的月份,但无济于事。首先在 RecordService 中,如果我想查看是否没有用户输入的当前年份。例如,如果没有 2021 年,那么“if”应该将该年份添加到列表中,以便我可以检查月年组合。它有效,但不是我需要的,因为第二个 if 总是抛出 RuntimeException() ,除非我输入尚未输入的年份(例如有 2020 年、2021 年和 2022 年,但没有 2023 年) ,因此当用户添加时:</p> <pre class="brush:php;toolbar:false;">{ "month": "March", "year": "2023", "readingTime": "1" }</pre> <p>该代码将起作用,因为列表中没有 2023 年,但只要他们尝试输入</p> <pre class="brush:php;toolbar:false;">{ "month": "May", "year": "2023", "readingTime": "1" }</pre> <p>它不会工作,因为年份已经存在,即使它应该工作,因为数据库中没有 2023 年 5 月的组合。我尝试使用布尔值来检查组合是否存在,但这对我来说没有任何意义。所以请大家帮忙:) 这是我的代码。</p> <p>RecordController.java</p>
@PostMapping("/{client_id}")
    公共记录添加(@PathVariable Long client_id,@RequestBody记录记录){
        return recordService.add(client_id, record);
    }</pre>
<p>RecordService.java</p>
公共记录添加(长client_id,记录记录){

       列表<字符串>月份列表=月份();
       列表<整数>年列表=年();

        记录recordSave = new Record();
        recordSave.setId(client_id);
        recordSave.setYear(record.getYear());
        recordSave.setMonth(record.getMonth());
        recordSave.setReadingTime(record.getReadingTime());
        recordSave.setReadingValue(randomGenerate());
        
        

        //不起作用
        if (!yearList.contains(recordSave.getYear())){
            yearList.add(recordSave.getYear());
        }


        //不起作用
        if (monthList.contains(recordSave.getMonth()) &&yearList.contains(recordSave.getYear())){
                抛出新的运行时异常();
        }
        别的 {
                recordRepository.save(recordSave);
        }
        返回空值;
    }</pre>
<p>记录.java</p>
@Data
@实体
@Table(名称=“记录”)
@JsonIgnoreProperties({"hibernateLazyInitializer","处理程序","设备"})
公共类记录实现可序列化{
    @ID
    @GenerateValue(策略= GenerationType.IDENTITY)
    私人长ID;

    字符串月;
    整数年份;
    字符串读取时间;
    浮点数读数值;

    布尔值是否存在;
}</pre>
<p>RandomGenerateValue 和月年函数</p>
public Float randomGenerate(){
        整数最大值 = 105,最小值 = 14;

        float random_float = (float)Math.floor(Math.random()*(max-min+1)+min);

        返回随机浮点数;
    }

    公共列表月(){
        列表<字符串> MonthList = new ArrayList<>();

        MonthList.add(“一月”); MonthList.add(“二月”); MonthList.add(“三月”); MonthList.add(“四月”);
        MonthList.add(“五月”); MonthList.add(“六月”); MonthList.add(“七月”); MonthList.add(“八月”);
        MonthList.add(“九月”); MonthList.add(“十月”);  MonthList.add(“十一月”); MonthList.add(“十二月”);

        返回月份列表;
    }公共列表<整数>年(){
        列表<整数> YearList = new ArrayList<>();

        年列表.add(2020);
        年列表.add(2021);
        返回年份列表;
    }</pre></p>
P粉676588738P粉676588738382 天前393

全部回复(1)我来回复

  • P粉285587590

    P粉2855875902023-09-04 00:07:58

    将年份和月份保留在一起,作为单个值

    您的问题似乎源于将年份和月份视为单独的值。相反,将它们结合起来。以年月为单位进行思考。

    YearMonth

    Java 包含一个用于此概念的类。它的名称很合适:YearMonth,位于java.time包中。

    YearMonth ym = YearMonth.of ( 2023 , 3 ) ;

    或者使用Month枚举来确保有效值。

    YearMonth ym = YearMonth.of ( 2023 , Month.MARCH ) ;

    您可以使用 地图。使用 NavigableMap 使键保持排序顺序。

    NavigableMap< YearMonth , UUID > map = new TreeMap<>() ;
    map.put( 
        YearMonth.of ( 2023 , Month.MARCH ) , 
        UUID.randomUUID()
    );
    map.put( 
        YearMonth.of ( 2023 , Month.FEBRUARY ) , 
        UUID.randomUUID()
    );

    测试是否已输入值。

    boolean alreadyPut2023March = map.containsKey( YearMonth.of ( 2023 , Month.MARCH ) ) ;

    ISO 8601

    出于文本目的,请使用默认的 ISO 8601 格式:YYYY-MM

    java.time 类在解析/生成文本时默认使用 ISO 8601 格式。

    String iso8601Output = YearMonth.of ( 2023 , Month.MARCH ).toString() ;

    假设您的数据库不提供年月数据类型,这样的字符串可用于存储在您的数据库中。我不知道有哪个数据库提供这种数据类型。所以文字就足够了。

    请注意,当按字母顺序排序时,值也会按时间顺序排序。

    你的 JSON 看起来像这样:

    {
        "year-month": "2023-03",
        "readingTime": "1"
    }
    

    回复
    0
  • 取消回复