LambdaQueryWrapper<TrnUserCourseTrainDO> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(TrnUserCourseTrainDO::getUserId, userId) .eq(TrnUserCourseTrainDO::getCourseId, courseId) .eq(TrnUserCourseTrainDO::getTrainId, createReqVO.getTrainId()) .last("for update"); TrnUserCourseTrainDO trnUserCourseTrainDO = trnUserCourseTrainMapper.selectOne(queryWrapper); if (trnUserCourseTrainDO == null) { TrnUserCourseTrainDO trainDO = new TrnUserCourseTrainDO(); BeanUtils.copyProperties(createReqVO, trainDO); trainDO.setDayTime(now); trainDO.setUserId(userId); //视频时长 Integer videoDuration = trnCourseDO.getVideoDuration(); trainDO.setVideoTime(videoDuration); String examPercentage = decimalFormat.format(((float) courseDurationStudy / videoDuration) * 100) + "%"; trainDO.setStudySchedule(examPercentage); trainDO.setCourseDurationStudy(courseDurationStudy); // 插入 trnUserCourseTrainMapper.insert(trainDO); } else { TrnUserCourseTrainDO trainDO = new TrnUserCourseTrainDO(); BeanUtils.copyProperties(createReqVO, trainDO); trainDO.setDayTime(now); trainDO.setUserId(userId); //视频时长 Integer videoDuration = trnCourseDO.getVideoDuration(); trainDO.setVideoTime(videoDuration); //已存在的学习进度 Integer doCourseDurationStudy = trnUserCourseTrainDO.getCourseDurationStudy(); if (courseDurationStudy > doCourseDurationStudy) { String examPercentage = decimalFormat.format(((float) courseDurationStudy / videoDuration) * 100) + "%"; trainDO.setStudySchedule(examPercentage); trainDO.setCourseDurationStudy(courseDurationStudy); } else { trainDO.setStudySchedule(trnUserCourseTrainDO.getStudySchedule()); trainDO.setCourseDurationStudy(doCourseDurationStudy); } trainDO.setId(trnUserCourseTrainDO.getId()); trnUserCourseTrainMapper.updateById(trainDO); } 这段代码怎么改为不会被插入脏数据的
时间: 2024-02-14 09:26:42 浏览: 48
为了避免插入脏数据,你可以使用数据库的事务来确保数据的一致性。在这段代码中,你可以在插入或更新操作之前开启一个事务,在操作完成后提交事务。如果发生异常,可以回滚事务以保持数据的一致性。
下面是将代码改为使用事务的示例:
```java
import org.springframework.transaction.annotation.Transactional;
@Transactional(rollbackFor = Exception.class)
public void saveOrUpdateTrnUserCourseTrainDO(Long userId, Long courseId, CreateReqVO createReqVO, TrnCourseDO trnCourseDO) {
LambdaQueryWrapper<TrnUserCourseTrainDO> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TrnUserCourseTrainDO::getUserId, userId)
.eq(TrnUserCourseTrainDO::getCourseId, courseId)
.eq(TrnUserCourseTrainDO::getTrainId, createReqVO.getTrainId())
.last("for update");
TrnUserCourseTrainDO trnUserCourseTrainDO = trnUserCourseTrainMapper.selectOne(queryWrapper);
if (trnUserCourseTrainDO == null) {
// 插入
insertTrnUserCourseTrain(userId, createReqVO, trnCourseDO);
} else {
updateTrnUserCourseTrain(trnUserCourseTrainDO, createReqVO, trnCourseDO);
}
}
private void insertTrnUserCourseTrain(Long userId, CreateReqVO createReqVO, TrnCourseDO trnCourseDO) {
TrnUserCourseTrainDO trainDO = new TrnUserCourseTrainDO();
BeanUtils.copyProperties(createReqVO, trainDO);
trainDO.setDayTime(now);
trainDO.setUserId(userId);
// 视频时长
Integer videoDuration = trnCourseDO.getVideoDuration();
trainDO.setVideoTime(videoDuration);
String examPercentage = decimalFormat.format(((float) courseDurationStudy / videoDuration) * 100) + "%";
trainDO.setStudySchedule(examPercentage);
trainDO.setCourseDurationStudy(courseDurationStudy);
trnUserCourseTrainMapper.insert(trainDO);
}
private void updateTrnUserCourseTrain(TrnUserCourseTrainDO trnUserCourseTrainDO, CreateReqVO createReqVO, TrnCourseDO trnCourseDO) {
TrnUserCourseTrainDO trainDO = new TrnUserCourseTrainDO();
BeanUtils.copyProperties(createReqVO, trainDO);
trainDO.setDayTime(now);
trainDO.setUserId(userId);
// 视频时长
Integer videoDuration = trnCourseDO.getVideoDuration();
trainDO.setVideoTime(videoDuration);
// 已存在的学习进度
Integer doCourseDurationStudy = trnUserCourseTrainDO.getCourseDurationStudy();
if (courseDurationStudy > doCourseDurationStudy) {
String examPercentage = decimalFormat.format(((float) courseDurationStudy / videoDuration) * 100) + "%";
trainDO.setStudySchedule(examPercentage);
trainDO.setCourseDurationStudy(courseDurationStudy);
} else {
trainDO.setStudySchedule(trnUserCourseTrainDO.getStudySchedule());
trainDO.setCourseDurationStudy(doCourseDurationStudy);
}
trainDO.setId(trnUserCourseTrainDO.getId());
trnUserCourseTrainMapper.updateById(trainDO);
}
```
在这个示例中,我们在`saveOrUpdateTrnUserCourseTrainDO`方法上使用了`@Transactional`注解来开启事务,并指定了回滚策略为`Exception.class`。在插入和更新的方法中,分别提取出来作为单独的方法,以便在事务中进行操作。
这样,当发生异常时,事务会回滚,确保数据的一致性。如果操作成功完成,事务会被提交,数据会被持久化到数据库中。
阅读全文