限制 Java Spring Boot 仅接受唯一的月年组合
P粉676588738
P粉676588738 2023-09-03 10:11:31
[MySQL讨论组]
<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;">{ &quot;month&quot;: &quot;March&quot;, &quot;year&quot;: &quot;2023&quot;, &quot;readingTime&quot;: &quot;1&quot; }</pre> <p>该代码将起作用,因为列表中没有 2023 年,但只要他们尝试输入</p> <pre class="brush:php;toolbar:false;">{ &quot;month&quot;: &quot;May&quot;, &quot;year&quot;: &quot;2023&quot;, &quot;readingTime&quot;: &quot;1&quot; }</pre> <p>它不会工作,因为年份已经存在,即使它应该工作,因为数据库中没有 2023 年 5 月的组合。我尝试使用布尔值来检查组合是否存在,但这对我来说没有任何意义。所以请大家帮忙:) 这是我的代码。</p> <p>RecordController.java</p> <pre class="brush:php;toolbar:false;">@PostMapping(&quot;/{client_id}&quot;) public Record add(@PathVariable Long client_id, @RequestBody Record record){ return recordService.add(client_id, record); }</pre> <p>RecordService.java</p> <pre class="brush:php;toolbar:false;">public Record add(Long client_id, Record record){ List&lt;String&gt; monthList = months(); List&lt;Integer&gt; yearList = years(); Record recordSave = new Record(); recordSave.setId(client_id); recordSave.setYear(record.getYear()); recordSave.setMonth(record.getMonth()); recordSave.setReadingTime(record.getReadingTime()); recordSave.setReadingValue(randomGenerate()); //Does not work if (!yearList.contains(recordSave.getYear())){ yearList.add(recordSave.getYear()); } //Does not work if (monthList.contains(recordSave.getMonth()) &amp;&amp; yearList.contains(recordSave.getYear())){ throw new RuntimeException(); } else { recordRepository.save(recordSave); } return null; }</pre> <p>记录.java</p> <pre class="brush:php;toolbar:false;">@Data @Entity @Table(name = &quot;record&quot;) @JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;,&quot;handler&quot;,&quot;device&quot;}) public class Record implements Serializable { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long id; String month; Integer year; String readingTime; Float readingValue; boolean isPresent; }</pre> <p>RandomGenerateValue 和月年函数</p> <pre class="brush:php;toolbar:false;">public Float randomGenerate(){ int max = 105, min = 14; float random_float = (float)Math.floor(Math.random()*(max-min+1)+min); return random_float; } public List&lt;String&gt; months(){ List&lt;String&gt; monthList = new ArrayList&lt;&gt;(); monthList.add(&quot;January&quot;); monthList.add(&quot;February&quot;); monthList.add(&quot;March&quot;); monthList.add(&quot;April&quot;); monthList.add(&quot;May&quot;); monthList.add(&quot;June&quot;); monthList.add(&quot;July&quot;); monthList.add(&quot;August&quot;); monthList.add(&quot;September&quot;); monthList.add(&quot;October&quot;); monthList.add(&quot;November&quot;); monthList.add(&quot;December&quot;); return monthList; } public List&lt;Integer&gt; years(){ List&lt;Integer&gt; yearList = new ArrayList&lt;&gt;(); yearList.add(2020); yearList.add(2021); return yearList; }</pre></p>
P粉676588738
P粉676588738

全部回复(1)
P粉285587590

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

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

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"
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号