当前位置: 首页 > news >正文

Mongodb:业务应用(2)

需求:

1、获取保存到mongodb库中的搜索记录列表

2、实现删除搜索记录接口

保存搜索记录数据参考上篇Mongodb:业务应用(1)_Success___的博客-CSDN博客

获取记录列表

 1、创建controller

package com.heima.search.controller.v1;import com.heima.model.common.dtos.ResponseResult;
import com.heima.search.service.ArticleSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/api/v1/history")
public class SearchHistoryController {@AutowiredArticleSearchService articleSearchService;@PostMapping("/load")public ResponseResult getHistory(){return articleSearchService.getHistory();}}

2、service实现类

    /**查询搜索历史@return*/@Overridepublic ResponseResult getHistory() {//查询当前用户下面的搜索记录Integer userId = AppThreadLocalUtil.getUser().getId();//构造条件Query query = Query.query(Criteria.where("userId").is(userId));//执行查询List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);//返回数据return ResponseResult.okResult(list);}

3、测试

        返回成功         

根据id删除搜索记录 

1、实现controller

    @PostMapping("/del")public ResponseResult delHistory(@RequestBody ApUserSearch userSearch){return articleSearchService.del(userSearch);}

2、service实现类

    /*根据id删除搜索记录*/@Overridepublic ResponseResult del(HistorySearchDto dto) {//检查参数if(dto == null){return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);}//检查登录状态Integer userId = AppThreadLocalUtil.getUser().getId();if(userId == null){return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);}//执行删除mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("id").is(dto.getId())),ApUserSearch.class);return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);}

3、测试

http://www.lryc.cn/news/119815.html

相关文章:

  • DSO学习笔记
  • 【Windows 常用工具系列 5 -- 如何在网页(CSDN)中实现右上角及右下角数字显示】
  • sql注入--报错注入
  • Nginx常用功能
  • 【Express.js】express-validator
  • 沁恒ch32V208处理器开发(三)GPIO控制
  • Jenkins 中 shell 脚本执行失败却不自行退出
  • 2021年12月 C/C++(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 计算机网络 网络层 IPv4数据报
  • 有哪些可以用于性能测试方法?【举例说明】
  • Linux进程管理命令
  • pytest 常用命令参数
  • 从安装 Seata 开始的分布式事务之旅 springboot集成seata
  • Laravel 使用mix引入本地样式文件 报错 Unable to locate Mix处理
  • QT学习笔记-QT安装oracle oci驱动
  • 【React学习】—类的基本知识(五)
  • 【AI】《动手学-深度学习-PyTorch版》笔记(十六):自定义网络层、保存/加载参数、使用GPU
  • 微软杀入Web3:打造基于区块链的AI产品
  • 聊聊51单片机
  • Linux yum 命令,Linux apt 命令
  • Vue+SpringBoot项目开发:登录页面美化,登录功能实现(三)
  • 2.若依前后端分离版第一个增删查改
  • javaSE_2.2——【方法的介绍】
  • 【02】基础知识:typescript数据类型
  • DIP: NAS(Neural Architecture Search)论文阅读与总结(双份快乐)
  • AI:02-基于深度学习的动物图像检索算法的研究
  • IDEA项目实践——Spring集成mybatis、spring当中的事务
  • 6-Ngnix配置反向代理
  • 构建 LVS-DR 群集、配置nginx负载均衡。
  • 【UE4的垃圾回收】