使用Java编程和springboot框架,连接mysql数据库(所需的三个表分别为奖品表名为a,活动表名为b,两者关联的表为c)实现下面两个功能 :1.创建活动并关联奖品,一个活动必须关联8个奖品,总概率为100% 2.修改活动 活动名称不能重复 并更新关联奖品。给出具体程序,包括给出相应的controller层,service层,service实现类,dao层及mapper.xml文件
时间: 2024-03-16 21:46:37 浏览: 57
Java项目练习 java+springboot+mysql
好的,下面是具体的程序实现。
1. 首先是数据库表的创建:
奖品表a:
```
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL COMMENT '奖品名称',
`probability` double NOT NULL COMMENT '中奖概率',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
活动表b:
```
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL COMMENT '活动名称',
`start_time` datetime NOT NULL COMMENT '开始时间',
`end_time` datetime NOT NULL COMMENT '结束时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
关联表c:
```
CREATE TABLE `c` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`activity_id` int(11) NOT NULL COMMENT '活动id',
`prize_id` int(11) NOT NULL COMMENT '奖品id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
```
2. 接下来是实体类的创建:
奖品实体类Prize:
```java
public class Prize {
private Integer id;
private String name;
private Double probability;
// 省略getter和setter方法
}
```
活动实体类Activity:
```java
public class Activity {
private Integer id;
private String name;
private Date startTime;
private Date endTime;
private List<Prize> prizes;
// 省略getter和setter方法
}
```
3. DAO层的实现:
奖品DAO接口PrizeDao:
```java
public interface PrizeDao {
Prize selectById(Integer id);
}
```
奖品DAO实现类PrizeDaoImpl:
```java
@Repository
public class PrizeDaoImpl implements PrizeDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Prize selectById(Integer id) {
String sql = "SELECT * FROM a WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[] {id}, new BeanPropertyRowMapper<>(Prize.class));
}
}
```
活动DAO接口ActivityDao:
```java
public interface ActivityDao {
Activity selectById(Integer id);
Activity selectByName(String name);
void insert(Activity activity);
void update(Activity activity);
void deleteById(Integer id);
}
```
活动DAO实现类ActivityDaoImpl:
```java
@Repository
public class ActivityDaoImpl implements ActivityDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Activity selectById(Integer id) {
String sql = "SELECT * FROM b WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[] {id}, new BeanPropertyRowMapper<>(Activity.class));
}
@Override
public Activity selectByName(String name) {
String sql = "SELECT * FROM b WHERE name = ?";
return jdbcTemplate.queryForObject(sql, new Object[] {name}, new BeanPropertyRowMapper<>(Activity.class));
}
@Override
public void insert(Activity activity) {
String sql = "INSERT INTO b (name, start_time, end_time) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, activity.getName(), activity.getStartTime(), activity.getEndTime());
// 插入关联表
for (Prize prize : activity.getPrizes()) {
sql = "INSERT INTO c (activity_id, prize_id) VALUES (?, ?)";
jdbcTemplate.update(sql, activity.getId(), prize.getId());
}
}
@Override
public void update(Activity activity) {
String sql = "UPDATE b SET name = ?, start_time = ?, end_time = ? WHERE id = ?";
jdbcTemplate.update(sql, activity.getName(), activity.getStartTime(), activity.getEndTime(), activity.getId());
// 先删除关联表
sql = "DELETE FROM c WHERE activity_id = ?";
jdbcTemplate.update(sql, activity.getId());
// 再插入关联表
for (Prize prize : activity.getPrizes()) {
sql = "INSERT INTO c (activity_id, prize_id) VALUES (?, ?)";
jdbcTemplate.update(sql, activity.getId(), prize.getId());
}
}
@Override
public void deleteById(Integer id) {
String sql = "DELETE FROM b WHERE id = ?";
jdbcTemplate.update(sql, id);
// 删除关联表
sql = "DELETE FROM c WHERE activity_id = ?";
jdbcTemplate.update(sql, id);
}
}
```
4. Service层的实现:
奖品Service接口PrizeService:
```java
public interface PrizeService {
Prize getById(Integer id);
}
```
奖品Service实现类PrizeServiceImpl:
```java
@Service
public class PrizeServiceImpl implements PrizeService {
@Autowired
private PrizeDao prizeDao;
@Override
public Prize getById(Integer id) {
return prizeDao.selectById(id);
}
}
```
活动Service接口ActivityService:
```java
public interface ActivityService {
Activity getById(Integer id);
Activity getByName(String name);
void createActivity(Activity activity) throws Exception;
void updateActivity(Activity activity) throws Exception;
void deleteById(Integer id);
}
```
活动Service实现类ActivityServiceImpl:
```java
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private ActivityDao activityDao;
@Autowired
private PrizeService prizeService;
@Override
public Activity getById(Integer id) {
return activityDao.selectById(id);
}
@Override
public Activity getByName(String name) {
return activityDao.selectByName(name);
}
@Override
public void createActivity(Activity activity) throws Exception {
// 检查活动名称是否重复
if (getByName(activity.getName()) != null) {
throw new Exception("活动名称重复");
}
// 检查关联的奖品数量是否正确
if (activity.getPrizes().size() != 8) {
throw new Exception("关联的奖品数量必须为8");
}
// 检查关联的奖品概率是否总和为100%
Double sum = 0.0;
for (Prize prize : activity.getPrizes()) {
sum += prize.getProbability();
}
if (sum != 1.0) {
throw new Exception("关联的奖品概率总和必须为100%");
}
activityDao.insert(activity);
}
@Override
public void updateActivity(Activity activity) throws Exception {
// 检查活动名称是否重复
Activity existActivity = getByName(activity.getName());
if (existActivity != null && !existActivity.getId().equals(activity.getId())) {
throw new Exception("活动名称重复");
}
// 检查关联的奖品数量是否正确
if (activity.getPrizes().size() != 8) {
throw new Exception("关联的奖品数量必须为8");
}
// 检查关联的奖品概率是否总和为100%
Double sum = 0.0;
for (Prize prize : activity.getPrizes()) {
sum += prize.getProbability();
}
if (sum != 1.0) {
throw new Exception("关联的奖品概率总和必须为100%");
}
activityDao.update(activity);
}
@Override
public void deleteById(Integer id) {
activityDao.deleteById(id);
}
}
```
5. Controller层的实现:
ActivityController:
```java
@RestController
@RequestMapping("/activity")
public class ActivityController {
@Autowired
private ActivityService activityService;
@Autowired
private PrizeService prizeService;
@GetMapping("/{id}")
public Activity getById(@PathVariable Integer id) {
return activityService.getById(id);
}
@PostMapping
public void createActivity(@RequestBody Activity activity) throws Exception {
for (Prize prize : activity.getPrizes()) {
prize.setId(prizeService.getById(prize.getId()).getId());
}
activityService.createActivity(activity);
}
@PutMapping
public void updateActivity(@RequestBody Activity activity) throws Exception {
for (Prize prize : activity.getPrizes()) {
prize.setId(prizeService.getById(prize.getId()).getId());
}
activityService.updateActivity(activity);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Integer id) {
activityService.deleteById(id);
}
}
```
至此,我们完成了连接mysql数据库,实现了创建活动并关联奖品,以及修改活动的功能,具体的程序实现包括了controller层,service层,service实现类,dao层及mapper.xml文件。
阅读全文