@NotBlank(groups = {RequiredGroup.class})注解的作用
时间: 2024-06-01 10:13:24 浏览: 195
@NotBlank(groups = {RequiredGroup.class})是一个注解,用于校验字符串类型的字段是否为空或空格。它的作用是确保特定字段中的值不为空,否则会触发校验错误。此注解可以在使用Java Bean Validation(JSR-380)进行数据验证时使用。@NotBlank(groups = {RequiredGroup.class})还可以与其他校验注解结合使用,例如@Size、@Pattern等。它们可以帮助开发人员更轻松地确保数据的准确性和完整性。
相关问题
@NotBlank(groups = {RequiredGroup.class})注解中groups = {RequiredGroup.class}的详细解释
@NotBlank(groups = {RequiredGroup.class})注解中groups = {RequiredGroup.class}是指这个注解只在 RequiredGroup 这个校验组中生效,只有在使用了 RequiredGroup 这个校验组时才会对被注解的字段进行非空校验。这个注解通常用于校验表单等必填项的合法性。
@Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "advert") public class Advert { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "广告唯一id") private Integer id; @ApiModelProperty(value = "广告名称") @NotBlank(message = "广告名称不可为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "position_id") private AdPosition position; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "media_id") private MediaFile media; } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "ad_position") public class AdPosition { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "位置名称") @NotBlank(message = "位置名称不可为空", groups = {AddGroup.class}) private String name; @OneToMany(mappedBy = "AdPosition", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } @Data @AllArgsConstructor @NoArgsConstructor @Builder @Table(name = "media_file") public class MediaFile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @ApiModelProperty(value = "媒体文件名称") private String fileName; @ApiModelProperty(value = "媒体文件类型") private Integer fileType; @ApiModelProperty(value = "媒体文件URL地址") private String fileUrl; @OneToMany(mappedBy = "MediaFile", cascade = CascadeType.ALL) private List<Advert> adverts = new ArrayList<>(); } public interface MyBaseMapper<T> extends Mapper<T>, MySqlMapper<T>,MyInsertListMapper<T> {} public interface AdvertMapper extends MyBaseMapper<Advert> {},如何对Advert类进行添加操作
可以使用AdvertMapper中的insert方法对Advert类进行添加操作。例如:
```
@Autowired
private AdvertMapper advertMapper;
Advert advert = Advert.builder()
.name("test advert")
.position(new AdPosition(1)) // 假设已经存在id为1的AdPosition
.media(new MediaFile(1)) // 假设已经存在id为1的MediaFile
.build();
advertMapper.insert(advert);
```
这样就可以将Advert对象添加到数据库中了。需要注意的是,如果AdPosition和MediaFile不存在,需要先进行添加操作。
阅读全文