@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对象
时间: 2024-02-27 10:53:45 浏览: 75
保存一个`Advert`对象可以通过使用`AdvertMapper`中的方法来实现,具体步骤如下:
1. 创建一个`Advert`对象,并设置其属性值:
```
Advert advert = new Advert();
advert.setName("广告名称");
// 设置关联属性
AdPosition position = new AdPosition();
position.setId(1); // 假设要关联的AdPosition的id为1
advert.setPosition(position);
MediaFile media = new MediaFile();
media.setId(2); // 假设要关联的MediaFile的id为2
advert.setMedia(media);
```
2. 调用`AdvertMapper`中的保存方法进行保存:
```
@Autowired
private AdvertMapper advertMapper;
// 保存Advert对象
advertMapper.insert(advert);
```
在保存时,由于`AdPosition`和`MediaFile`实体类中的`adverts`属性都使用了`mappedBy`属性,因此不需要再手动设置关联关系的另一侧。
另外,如果需要同时保存多个`Advert`对象,可以将它们放在一个`List<Advert>`中,然后使用`MyInsertListMapper`中的`batchInsert`方法来进行批量保存,例如:
```
List<Advert> adverts = new ArrayList<>();
// 添加多个Advert对象到adverts列表中
advertMapper.batchInsert(adverts);
```
阅读全文