@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-01-28 08:05:20 浏览: 60
可以使用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不存在,需要先进行添加操作。
相关问题
@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对象
保存一个`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);
```
@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad") public class Ad { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @ManyToOne @JoinColumn(name = "position_id") private AdPosition position; @Column(name = "start_date") private LocalDateTime startDate; @Column(name = "end_date") private LocalDateTime endDate; private String link; }和@Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "ad_image") public class AdImage { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String url; @OneToOne(mappedBy = "image") private Ad ad; },为什么 public Ad add(@RequestBody AdDto adDto) { AdPosition position = adPositionRepository.getOne(adDto.getPositionId()); AdImage image = new AdImage(); image.setUrl(adDto.getImageUrl()); adImageRepository.save(image); Ad ad = new Ad(); ad.setName(adDto.getName()); ad.setPosition(position); ad.setStartDate(adDto.getStartDate()); ad.setEndDate(adDto.getEndDate()); ad.setLink(adDto.getLink()); ad.setImage(image); return adRepository.save(ad); }报错Cannot resolve method 'setImage' in 'Advert'
报错信息提示无法解析方法 'setImage',这是因为 Ad 类中没有名为 setImage 的方法。根据你提供的代码,Ad 类中只有一个名为 setImage 的属性,而不是方法。为了解决这个问题,你需要在 Ad 类中创建一个名为 setImage 的方法,该方法应该接收一个 AdImage 对象作为参数,并将其设置为 Ad 对象的 image 属性,例如:
```
public void setImage(AdImage image) {
this.image = image;
}
```
然后在 add 方法中,将 Ad 对象的 image 属性设置为你已经保存的 AdImage 对象,如下所示:
```
Ad ad = new Ad();
ad.setName(adDto.getName());
ad.setPosition(position);
ad.setStartDate(adDto.getStartDate());
ad.setEndDate(adDto.getEndDate());
ad.setLink(adDto.getLink());
AdImage image = new AdImage();
image.setUrl(adDto.getImageUrl());
adImageRepository.save(image);
ad.setImage(image);
return adRepository.save(ad);
```
这样就能够成功将 AdImage 对象和 Ad 对象关联起来了。
阅读全文