使用Java编程和springboot框架,连接mysql数据库(所需的三个表分别为奖品表名为a,活动表名为b,两者关联的表为c(中奖概率字段位于c表))实现下面两个功能 :1.创建活动并关联奖品,一个活动必须关联8个奖品,总概率为100% 2.修改活动 活动名称不能重复 并更新关联奖品。给出具体程序,包括给出相应的controller层,service层,service实现类,dao层及mapper.xml文件
时间: 2024-03-16 16:46:55 浏览: 127
好的,以下是Java编写的代码示例,包括controller、service、dao和mapper.xml文件。
Controller层:
```java
@RestController
@RequestMapping("/activity")
public class ActivityController {
@Autowired
private ActivityService activityService;
@PostMapping("/create")
public ResponseEntity createActivity(@RequestBody ActivityDto activityDto) {
activityService.createActivity(activityDto);
return ResponseEntity.ok("Create activity success");
}
@PutMapping("/update")
public ResponseEntity updateActivity(@RequestBody ActivityDto activityDto) {
activityService.updateActivity(activityDto);
return ResponseEntity.ok("Update activity success");
}
}
```
Service层:
```java
public interface ActivityService {
void createActivity(ActivityDto activityDto);
void updateActivity(ActivityDto activityDto);
}
```
Service实现类:
```java
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private ActivityMapper activityMapper;
@Autowired
private PrizeMapper prizeMapper;
@Autowired
private ActivityPrizeMapper activityPrizeMapper;
@Override
public void createActivity(ActivityDto activityDto) {
// 1. 创建活动
Activity activity = new Activity();
activity.setName(activityDto.getName());
activityMapper.insert(activity);
// 2. 关联奖品
List<PrizeDto> prizeDtos = activityDto.getPrizes();
List<ActivityPrize> activityPrizes = new ArrayList<>();
double totalProbability = 0;
for (PrizeDto prizeDto : prizeDtos) {
ActivityPrize activityPrize = new ActivityPrize();
activityPrize.setActivityId(activity.getId());
activityPrize.setPrizeId(prizeDto.getId());
activityPrize.setProbability(prizeDto.getProbability());
activityPrizes.add(activityPrize);
totalProbability += prizeDto.getProbability();
}
// 3. 校验概率是否为100%
if (totalProbability != 100) {
throw new RuntimeException("Total probability must be 100%");
}
// 4. 保存关联关系
activityPrizeMapper.batchInsert(activityPrizes);
}
@Override
public void updateActivity(ActivityDto activityDto) {
// 1. 查询活动
Activity activity = activityMapper.selectByPrimaryKey(activityDto.getId());
if (activity == null) {
throw new RuntimeException("Activity not exists");
}
// 2. 更新活动名称
if (!activity.getName().equals(activityDto.getName())) {
Activity activityByName = activityMapper.selectByName(activityDto.getName());
if (activityByName != null) {
throw new RuntimeException("Activity name already exists");
}
activity.setName(activityDto.getName());
activityMapper.updateByPrimaryKey(activity);
}
// 3. 更新关联奖品
List<PrizeDto> prizeDtos = activityDto.getPrizes();
List<ActivityPrize> activityPrizes = new ArrayList<>();
double totalProbability = 0;
for (PrizeDto prizeDto : prizeDtos) {
ActivityPrize activityPrize = new ActivityPrize();
activityPrize.setActivityId(activity.getId());
activityPrize.setPrizeId(prizeDto.getId());
activityPrize.setProbability(prizeDto.getProbability());
activityPrizes.add(activityPrize);
totalProbability += prizeDto.getProbability();
}
// 4. 校验概率是否为100%
if (totalProbability != 100) {
throw new RuntimeException("Total probability must be 100%");
}
// 5. 删除旧的关联关系,保存新的关联关系
activityPrizeMapper.deleteByActivityId(activity.getId());
activityPrizeMapper.batchInsert(activityPrizes);
}
}
```
DAO层:
```java
@Repository
public interface ActivityMapper extends BaseMapper<Activity> {
Activity selectByName(String name);
}
```
```java
@Repository
public interface PrizeMapper extends BaseMapper<Prize> {
}
```
```java
@Repository
public interface ActivityPrizeMapper extends BaseMapper<ActivityPrize> {
void deleteByActivityId(Integer activityId);
void batchInsert(List<ActivityPrize> activityPrizes);
}
```
Mapper.xml文件:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ActivityMapper">
<resultMap id="BaseResultMap" type="com.example.entity.Activity">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select id, name
from activity
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select id, name
from activity
where name = #{name,jdbcType=VARCHAR}
</select>
<insert id="insert" parameterType="com.example.entity.Activity">
insert into activity (name)
values (#{name,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.example.entity.Activity">
update activity
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.PrizeMapper">
<resultMap id="BaseResultMap" type="com.example.entity.Prize">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select id, name
from prize
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ActivityPrizeMapper">
<resultMap id="BaseResultMap" type="com.example.entity.ActivityPrize">
<result column="activity_id" jdbcType="INTEGER" property="activityId" />
<result column="prize_id" jdbcType="INTEGER" property="prizeId" />
<result column="probability" jdbcType="DOUBLE" property="probability" />
</resultMap>
<delete id="deleteByActivityId" parameterType="java.lang.Integer">
delete from activity_prize
where activity_id = #{activityId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.entity.ActivityPrize">
insert into activity_prize (activity_id, prize_id, probability)
values (#{activityId,jdbcType=INTEGER}, #{prizeId,jdbcType=INTEGER}, #{probability,jdbcType=DOUBLE})
</insert>
<insert id="batchInsert">
<foreach collection="list" item="item" index="index" separator=";">
insert into activity_prize (activity_id, prize_id, probability)
values (#{item.activityId,jdbcType=INTEGER}, #{item.prizeId,jdbcType=INTEGER}, #{item.probability,jdbcType=DOUBLE})
</foreach>
</insert>
</mapper>
```
以上是Java编写的实现,使用了SpringBoot和MyBatis框架,其中controller层提供了两个接口,分别用于创建和更新活动,service层提供了对应的接口实现,dao层提供了数据库操作的接口,mapper.xml文件提供了对应的SQL语句。
阅读全文