@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-29 21:03:08 浏览: 141
要对Advert类进行添加操作,可以使用AdvertMapper接口中继承的MyBaseMapper<T>中的insertSelective方法。insertSelective方法会将传入的Advert对象中非空的字段映射到数据库中,同时生成对应的主键。示例代码如下:
```java
@Autowired
private AdvertMapper advertMapper;
// 创建一个新的Advert对象
Advert advert = Advert.builder()
.name("广告名称")
.position(new AdPosition(1)) // 假设AdPosition表中id为1的记录是需要的位置
.media(new MediaFile(1)) // 假设MediaFile表中id为1的记录是需要的媒体文件
.build();
// 调用insertSelective方法插入数据
advertMapper.insertSelective(advert);
```
需要注意的是,如果AdPosition和MediaFile对象不存在,需要先进行插入或者查询操作,将对应的记录插入到数据库中或者查询出对应的记录,然后再将其赋值给Advert对象的position和media属性。
相关问题
@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'
这个错误是因为您的Ad类中没有定义setImage()方法。在Ad类中,您定义了一个AdImage对象,并使用@OneToOne注释将其映射到AdImage表中的Ad列。要设置AdImage对象,您应该使用set方法而不是setImage()方法。因此,您应该在Ad类中为AdImage对象定义一个set方法,然后使用set方法设置AdImage对象,就像这样:
```
public void setImage(AdImage image) {
this.image = image;
}
```
然后在您的add()方法中,将以下行:
```
ad.setImage(image);
```
更改为:
```
ad.setimage(image);
```
这样您的代码就不会再出现这个错误了。
阅读全文