广告表(advert): 字段名 类型 描述 id int 广告ID,主键 name varchar 广告名称 position_id int 广告位置ID media_id int 广告图片ID start_date 开始日期 end_date 结束日期 link varchar 广告链接 status int 状态 广告位置表(ad_position): 字段名 类型 描述 id int 广告位置ID,主键 name varchar 位置名称 width int 广告位置宽度 height int 广告位置高度 desc varchar 位置描述 广告媒体文件表(media_file): 字段名 类型 描述 id int 广告图片ID,主键 name varchar 媒体文件名称 type tinyint 媒体文件类型:0-图片,1-视频 link varchar 媒体文件URL地址 其中ad_position和media_file中的主键id分别对应advert中的外键position_id和media_id,在springboot项目中对广告进行添加和修改操作时,用注解该如何实现,请详细展示javadaim
时间: 2024-02-03 21:13:14 浏览: 112
在Spring Boot项目中,可以使用注解来实现对广告的添加和修改操作。具体实现步骤如下:
1. 定义实体类
首先需要定义三个实体类,分别对应广告、广告位置和媒体文件:
```java
@Entity
@Table(name = "advert")
public class Advert {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name = "position_id")
private AdPosition position;
@ManyToOne
@JoinColumn(name = "media_id")
private MediaFile mediaFile;
@Column(name = "start_date")
private Date startDate;
@Column(name = "end_date")
private Date endDate;
private String link;
private Integer status;
// 省略 getter 和 setter 方法
}
@Entity
@Table(name = "ad_position")
public class AdPosition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer width;
private Integer height;
private String desc;
// 省略 getter 和 setter 方法
}
@Entity
@Table(name = "media_file")
public class MediaFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer type;
private String link;
// 省略 getter 和 setter 方法
}
```
2. 定义控制器
接下来需要定义控制器来处理添加和修改广告的请求:
```java
@RestController
@RequestMapping("/adverts")
public class AdvertController {
@Autowired
private AdvertRepository advertRepository;
@Autowired
private AdPositionRepository adPositionRepository;
@Autowired
private MediaFileRepository mediaFileRepository;
@PostMapping
public Advert addAdvert(@RequestBody AdvertDto advertDto) {
Advert advert = new Advert();
advert.setName(advertDto.getName());
advert.setStartDate(advertDto.getStartDate());
advert.setEndDate(advertDto.getEndDate());
advert.setLink(advertDto.getLink());
advert.setStatus(advertDto.getStatus());
AdPosition position = adPositionRepository.findById(advertDto.getPositionId())
.orElseThrow(() -> new RuntimeException("Ad position not found"));
advert.setPosition(position);
MediaFile mediaFile = mediaFileRepository.findById(advertDto.getMediaId())
.orElseThrow(() -> new RuntimeException("Media file not found"));
advert.setMediaFile(mediaFile);
return advertRepository.save(advert);
}
@PutMapping("/{id}")
public Advert updateAdvert(@PathVariable Integer id, @RequestBody AdvertDto advertDto) {
Advert advert = advertRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Advert not found"));
advert.setName(advertDto.getName());
advert.setStartDate(advertDto.getStartDate());
advert.setEndDate(advertDto.getEndDate());
advert.setLink(advertDto.getLink());
advert.setStatus(advertDto.getStatus());
AdPosition position = adPositionRepository.findById(advertDto.getPositionId())
.orElseThrow(() -> new RuntimeException("Ad position not found"));
advert.setPosition(position);
MediaFile mediaFile = mediaFileRepository.findById(advertDto.getMediaId())
.orElseThrow(() -> new RuntimeException("Media file not found"));
advert.setMediaFile(mediaFile);
return advertRepository.save(advert);
}
}
```
其中,`AdvertDto`是一个数据传输对象,用于接收客户端传来的广告信息:
```java
public class AdvertDto {
private String name;
private Integer positionId;
private Integer mediaId;
private Date startDate;
private Date endDate;
private String link;
private Integer status;
// 省略 getter 和 setter 方法
}
```
3. 定义数据访问层
最后需要定义数据访问层,用于对广告、广告位置和媒体文件进行增删改查操作:
```java
public interface AdvertRepository extends JpaRepository<Advert, Integer> {
}
public interface AdPositionRepository extends JpaRepository<AdPosition, Integer> {
}
public interface MediaFileRepository extends JpaRepository<MediaFile, Integer> {
}
```
至此,就实现了对广告的添加和修改操作。在客户端发送请求时,只需要将广告信息放入`AdvertDto`中发送给服务端即可。
阅读全文