使用Java编程和springboot框架,Spring MVC框架,mysql数据库实现实现 1.创建活动并关联奖品,一个活动必须关联8个奖品,总概率为100% 2.修改活动 活动名称不能重复 并更新关联奖品,给出具体程序
时间: 2024-03-16 11:40:55 浏览: 62
好的,我会尽力回答您的问题。首先,我们需要创建一个活动和奖品的实体类:
```java
@Entity
@Table(name = "activity")
public class Activity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "活动名称不能为空")
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "activity", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Prize> prizes = new ArrayList<>();
// 省略 getter 和 setter 方法
}
@Entity
@Table(name = "prize")
public class Prize {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "奖品名称不能为空")
@Column(name = "name")
private String name;
@NotNull(message = "概率不能为空")
@Min(value = 1, message = "概率必须大于0")
@Max(value = 100, message = "概率必须小于等于100")
@Column(name = "probability")
private Integer probability;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "activity_id")
private Activity activity;
// 省略 getter 和 setter 方法
}
```
接下来,我们需要创建活动和奖品的服务类,用于处理业务逻辑和数据库操作:
```java
@Service
public class ActivityService {
@Autowired
private ActivityRepository activityRepository;
public Activity createActivity(Activity activity) {
if (activity.getPrizes().size() != 8) {
throw new IllegalArgumentException("一个活动必须关联8个奖品");
}
int totalProbability = 0;
for (Prize prize : activity.getPrizes()) {
totalProbability += prize.getProbability();
}
if (totalProbability != 100) {
throw new IllegalArgumentException("总概率必须为100%");
}
return activityRepository.save(activity);
}
public Activity updateActivity(Long activityId, Activity activity) {
Activity oldActivity = activityRepository.findById(activityId)
.orElseThrow(() -> new IllegalArgumentException("活动不存在"));
if (!activity.getName().equals(oldActivity.getName())) {
if (activityRepository.findByName(activity.getName()).isPresent()) {
throw new IllegalArgumentException("活动名称已存在");
}
}
oldActivity.setName(activity.getName());
oldActivity.getPrizes().clear();
oldActivity.getPrizes().addAll(activity.getPrizes());
return activityRepository.save(oldActivity);
}
}
@Service
public class PrizeService {
@Autowired
private PrizeRepository prizeRepository;
public Prize createPrize(Prize prize) {
return prizeRepository.save(prize);
}
public Prize updatePrize(Long prizeId, Prize prize) {
Prize oldPrize = prizeRepository.findById(prizeId)
.orElseThrow(() -> new IllegalArgumentException("奖品不存在"));
oldPrize.setName(prize.getName());
oldPrize.setProbability(prize.getProbability());
return prizeRepository.save(oldPrize);
}
}
```
最后,我们需要创建控制器类,用于接收请求和返回响应:
```java
@RestController
@RequestMapping("/api/activity")
public class ActivityController {
@Autowired
private ActivityService activityService;
@PostMapping
public Activity createActivity(@RequestBody @Valid Activity activity) {
return activityService.createActivity(activity);
}
@PutMapping("/{activityId}")
public Activity updateActivity(@PathVariable Long activityId, @RequestBody @Valid Activity activity) {
return activityService.updateActivity(activityId, activity);
}
}
@RestController
@RequestMapping("/api/prize")
public class PrizeController {
@Autowired
private PrizeService prizeService;
@PostMapping
public Prize createPrize(@RequestBody @Valid Prize prize) {
return prizeService.createPrize(prize);
}
@PutMapping("/{prizeId}")
public Prize updatePrize(@PathVariable Long prizeId, @RequestBody @Valid Prize prize) {
return prizeService.updatePrize(prizeId, prize);
}
}
```
至此,我们就完成了这个应用程序的开发。当然,这只是一个简单的示例,还有很多细节需要考虑,比如数据库的设计和初始化、异常处理、安全性等等。如果您有任何问题或建议,欢迎随时提出。
阅读全文