使用Java编程和springboot框架,连接mysql数据库(奖品表名为a,活动表名为b,两者关联的表为c)实现 1.创建活动并关联奖品,一个活动必须关联8个奖品,总概率为100% 2.修改活动 活动名称不能重复 并更新关联奖品,给出具体程序,包括给出相应的controller层,service层,service实现类,dao层及mapper.xml文件
时间: 2024-03-16 12:46:21 浏览: 117
JAVA连接数据库进行相应的操作
好的,首先需要创建一个Spring Boot项目,并且添加相关依赖,包括 Spring Boot、MyBatis、MySQL Connector 等。
接下来,我们需要在 MySQL 中创建两个表,分别为 a 和 b,以及一个关联表 c。表结构如下:
```
-- 奖品表 a
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`probability` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
-- 活动表 b
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
-- 关联表 c
CREATE TABLE `c` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`aid` int(11) NOT NULL,
`bid` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_aid_bid` (`aid`,`bid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
然后,我们需要定义实体类 Prize 和 Activity,以及它们的关联类 PrizeActivity。
```
// Prize.java
public class Prize {
private Integer id;
private String name;
private Integer probability;
// 省略 getter 和 setter
}
// Activity.java
public class Activity {
private Integer id;
private String name;
private List<Prize> prizes;
// 省略 getter 和 setter
}
// PrizeActivity.java
public class PrizeActivity {
private Integer id;
private Integer aid;
private Integer bid;
// 省略 getter 和 setter
}
```
在 DAO 层中,我们需要定义三个接口:PrizeMapper、ActivityMapper 和 PrizeActivityMapper。
```
// PrizeMapper.java
@Mapper
public interface PrizeMapper {
@Select("SELECT * FROM a")
List<Prize> getAllPrizes();
}
// ActivityMapper.java
@Mapper
public interface ActivityMapper {
@Select("SELECT * FROM b WHERE id = #{id}")
Activity getActivityById(Integer id);
@Select("SELECT * FROM b WHERE name = #{name}")
Activity getActivityByName(String name);
@Insert("INSERT INTO b (name) VALUES (#{name})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int createActivity(Activity activity);
@Update("UPDATE b SET name = #{name} WHERE id = #{id}")
void updateActivity(Activity activity);
}
// PrizeActivityMapper.java
@Mapper
public interface PrizeActivityMapper {
@Insert("INSERT INTO c (aid, bid) VALUES (#{aid}, #{bid})")
void createPrizeActivity(PrizeActivity prizeActivity);
@Delete("DELETE FROM c WHERE bid = #{bid}")
void deletePrizeActivityByActivityId(Integer bid);
}
```
接下来,我们需要在 Service 层中定义三个接口:PrizeService、ActivityService 和 PrizeActivityService,并且实现它们的方法。
```
// PrizeService.java
public interface PrizeService {
List<Prize> getAllPrizes();
}
// ActivityService.java
public interface ActivityService {
Activity getActivityById(Integer id);
Activity getActivityByName(String name);
Activity createActivity(Activity activity);
void updateActivity(Activity activity);
}
// PrizeActivityService.java
public interface PrizeActivityService {
void createPrizeActivity(Integer aid, Integer bid);
void deletePrizeActivityByActivityId(Integer bid);
}
```
```
// PrizeServiceImpl.java
@Service
public class PrizeServiceImpl implements PrizeService {
private final PrizeMapper prizeMapper;
public PrizeServiceImpl(PrizeMapper prizeMapper) {
this.prizeMapper = prizeMapper;
}
@Override
public List<Prize> getAllPrizes() {
return prizeMapper.getAllPrizes();
}
}
// ActivityServiceImpl.java
@Service
public class ActivityServiceImpl implements ActivityService {
private final ActivityMapper activityMapper;
private final PrizeActivityService prizeActivityService;
public ActivityServiceImpl(ActivityMapper activityMapper, PrizeActivityService prizeActivityService) {
this.activityMapper = activityMapper;
this.prizeActivityService = prizeActivityService;
}
@Override
public Activity getActivityById(Integer id) {
return activityMapper.getActivityById(id);
}
@Override
public Activity getActivityByName(String name) {
return activityMapper.getActivityByName(name);
}
@Override
public Activity createActivity(Activity activity) {
activityMapper.createActivity(activity);
for (Prize prize : activity.getPrizes()) {
prizeActivityService.createPrizeActivity(prize.getId(), activity.getId());
}
return activity;
}
@Override
public void updateActivity(Activity activity) {
activityMapper.updateActivity(activity);
prizeActivityService.deletePrizeActivityByActivityId(activity.getId());
for (Prize prize : activity.getPrizes()) {
prizeActivityService.createPrizeActivity(prize.getId(), activity.getId());
}
}
}
// PrizeActivityServiceImpl.java
@Service
public class PrizeActivityServiceImpl implements PrizeActivityService {
private final PrizeActivityMapper prizeActivityMapper;
public PrizeActivityServiceImpl(PrizeActivityMapper prizeActivityMapper) {
this.prizeActivityMapper = prizeActivityMapper;
}
@Override
public void createPrizeActivity(Integer aid, Integer bid) {
PrizeActivity prizeActivity = new PrizeActivity();
prizeActivity.setAid(aid);
prizeActivity.setBid(bid);
prizeActivityMapper.createPrizeActivity(prizeActivity);
}
@Override
public void deletePrizeActivityByActivityId(Integer bid) {
prizeActivityMapper.deletePrizeActivityByActivityId(bid);
}
}
```
最后,在 Controller 层中定义 ActivityController,并且注入 ActivityService。
```
@RestController
@RequestMapping("/activity")
public class ActivityController {
private final ActivityService activityService;
public ActivityController(ActivityService activityService) {
this.activityService = activityService;
}
@GetMapping("/{id}")
public Activity getActivityById(@PathVariable("id") Integer id) {
return activityService.getActivityById(id);
}
@GetMapping("/name/{name}")
public Activity getActivityByName(@PathVariable("name") String name) {
return activityService.getActivityByName(name);
}
@PostMapping("")
public Activity createActivity(@RequestBody Activity activity) {
return activityService.createActivity(activity);
}
@PutMapping("/{id}")
public void updateActivity(@PathVariable("id") Integer id, @RequestBody Activity activity) {
if (!id.equals(activity.getId())) {
throw new IllegalArgumentException("The id is not matched.");
}
activityService.updateActivity(activity);
}
}
```
好了,以上就是实现的全部内容。如果需要更详细的代码实现可以参考:https://github.com/shadowice0904/spring-boot-mybatis-example
阅读全文