public void saveToElasticsearch() { LambdaQueryWrapper<RecommendationModel> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select(RecommendationModel::getId, RecommendationModel::getName, RecommendationModel::getTelephone, RecommendationModel::getEmil, RecommendationModel::getMessage); //queryWrapper.eq(RecommendationModel::getId, 1); List<RecommendationModel> companyModelList = recDao.selectList(queryWrapper); elasticsearchRestTemplate.save(companyModelList); logger.info("看这里的token: {}", "更新一次数据"); }
时间: 2024-04-20 08:26:47 浏览: 20
这段代码是用于将数据保存到Elasticsearch中的方法。它首先创建了一个LambdaQueryWrapper对象,用于构建查询条件。然后通过select方法指定需要查询的字段。接下来,通过recDao.selectList方法执行查询,并将结果保存到companyModelList列表中。最后,使用elasticsearchRestTemplate.save方法将companyModelList中的数据保存到Elasticsearch中。日志记录了保存数据的操作。
相关问题
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); } 这段代码怎么改为不会被插入脏数据的
为了避免插入脏数据,你可以使用数据库的事务来确保数据的一致性。在这段代码中,你可以在插入或更新操作之前开启一个事务,在操作完成后提交事务。如果发生异常,可以回滚事务以保持数据的一致性。
下面是将代码改为使用事务的示例:
```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`。在插入和更新的方法中,分别提取出来作为单独的方法,以便在事务中进行操作。
这样,当发生异常时,事务会回滚,确保数据的一致性。如果操作成功完成,事务会被提交,数据会被持久化到数据库中。
public void updateBisTask(Integer id, String projectName) { // 查询BisTask对象 BisTask bisTask = bisTaskMapper.selectById(id); if (bisTask == null) { throw new RuntimeException("BisTask不存在"); } // 查询BisProject对象 BisProject bisProject = bisProjectMapper.selectOne(new QueryWrapper<BisProject>().eq("project_name", projectName)); if (bisProject == null) { throw new RuntimeException("项目名称不存在"); } // 查询BisTower对象 BisTower bisTower = bisTowerMapper.selectOne(new QueryWrapper<BisTower>().eq("project_id", bisProject.getId())); if (bisTower == null) { throw new RuntimeException("塔名称不存在"); } // 更新BisTaskTower对象 BisTaskTower bisTaskTower = bisTaskTowerMapper.selectOne(new QueryWrapper<BisTaskTower>().eq("task_id", id)); if (bisTaskTower == null) { throw new RuntimeException("没有关联上塔站"); } bisTaskTower.setTowerId(bisTower.getId()); bisTaskTowerMapper.updateById(bisTaskTower); // 更新BisTask对象 bisTask.setProjectName(projectName); bisTaskMapper.updateById(bisTask); } 将Integer id 改为BisTaskVO bisTaskVO,能通过BisTaskVO bisTaskVO修改bis_task表的数据
可以对方法进行修改,如下所示:
```
public void updateBisTask(BisTaskVO bisTaskVO, String projectName) {
// 查询BisTask对象
BisTask bisTask = bisTaskMapper.selectById(bisTaskVO.getId());
if (bisTask == null) {
throw new RuntimeException("BisTask不存在");
}
// 查询BisProject对象
BisProject bisProject = bisProjectMapper.selectOne(new QueryWrapper<BisProject>().eq("project_name", projectName));
if (bisProject == null) {
throw new RuntimeException("项目名称不存在");
}
// 查询BisTower对象
BisTower bisTower = bisTowerMapper.selectOne(new QueryWrapper<BisTower>().eq("project_id", bisProject.getId()));
if (bisTower == null) {
throw new RuntimeException("塔名称不存在");
}
// 更新BisTaskTower对象
BisTaskTower bisTaskTower = bisTaskTowerMapper.selectOne(new QueryWrapper<BisTaskTower>().eq("task_id", bisTaskVO.getId()));
if (bisTaskTower == null) {
throw new RuntimeException("没有关联上塔站");
}
bisTaskTower.setTowerId(bisTower.getId());
bisTaskTowerMapper.updateById(bisTaskTower);
// 更新BisTask对象
bisTask.setProjectName(projectName);
bisTaskMapper.updateById(bisTask);
}
```
这里将 `Integer id` 改为 `BisTaskVO bisTaskVO`,并在方法中使用 `bisTaskVO.getId()` 获取任务 ID。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)