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

03 SpringBoot实战 -微头条之首页门户模块(跳转某页面自动展示所有信息+根据hid查询文章全文并用乐观锁修改阅读量)

1.1 自动展示所有信息

  1. 需求描述: 进入新闻首页portal/findAllType, 自动返回所有栏目名称和id
    在这里插入图片描述

  2. 接口描述

    url地址:portal/findAllTypes

    请求方式:get

    请求参数:无

    响应数据:

    成功

{"code":"200","message":"OK""data":{[{"tid":"1","tname":"新闻"},{"tid":"2","tname":"体育"},{"tid":"3","tname":"娱乐"},{"tid":"4","tname":"科技"},{"tid":"5","tname":"其他"}]}
}
  1. 代码编写
    PortalController :
package com.sunsplanter.controller;import com.sunsplanter.service.TypeService;
import com.sunsplanter.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("portal")
public class PortalController {@Autowiredprivate TypeService typeService;@GetMapping("findAllType")public Result findAllTypes(){Result result = typeService.findAllTypes();return result;}
}

TypeService:

package com.sunsplanter.service;import com.sunsplanter.pojo.Type;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sunsplanter.utils.Result;public interface TypeService extends IService<Type>{Result findAllTypes();
}

TypeServiceImpl:

package com.sunsplanter.service.impl;import com.sunsplanter.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunsplanter.mapper.TypeMapper;
import com.sunsplanter.pojo.Type;
import com.sunsplanter.service.TypeService;
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService{@Autowiredprivate TypeMapper typeMapper;@Overridepublic Result findAllTypes() {//不传条件构造器,即查询全部List<Type> types = typeMapper.selectList(null);return Result.ok(types);}
}

达到的效果是,不需要任何参数, 只要访问portal/findAllType, 就返回news_type表中的所有数据(version和is_deleted除外, 因为已在实体类中注解为版本和逻辑删除)

1.2 - 查询头条详情

  1. 需求描述

在这里插入图片描述
- 用户点击"查看全文"时,向服务端发送新闻id
- 后端根据新闻id查询完整新闻文章信息并返回
- 后端要同时让新闻的浏览量+1

  1. 接口描述

url地址:portal/showHeadlineDetail

请求方式:post

请求参数: Param传参hid

响应数据:

成功则

{"code":"200","message":"success","data":{"headline":{"hid":"1",                     // 新闻id "title":"马斯克宣布 ... ...",   // 新闻标题"article":"... ..."            // 新闻正文"type":"1",                    // 新闻所属类别编号"typeName":"科技",             // 新闻所属类别"pageViews":"40",              // 新闻浏览量"pastHours":"3" ,              // 发布时间已过小时数"publisher":"1" ,              // 发布用户ID"author":"张三"                 // 新闻作者}}
}
  1. 代码实现
    1. controller
      @Overridepublic Result showHeadlineDetail(Integer hid) {/**注意响应的数据是双层嵌套,即data包裹headline,headline包含查询到的属性参数* 先用一个名为dataMap的Map以键值对的形式存储返回的属性参数* 再将名为data的Map是为一个值,搭配上名为headline的键* 存储进一个名为headlineMap的Map中,最终将Map作为参数传入Result,返回Result*/Map dataMap = headlineMapper.queryDetailMap(hid);Map headlineMap = new HashMap<>();headlineMap.put("headline",dataMap);/*乐观锁修改阅读量+1*上面已经通过hid查到了所有信息,包括当时的版本号,假设是2* 将2直接赋值到新建的headline的Version中* 在最后一句update中,MP会帮我们检查,如果此时该条记录的版本号仍为2,* 则说明这段时间没有人修改过这条记录,可以正常修改*/Headline headline = new Headline();headline.setHid(hid);headline.setPageViews((Integer) headlineMap.get("pageViews")+1); //阅读量+1headline.setVersion((Integer) headlineMap.get("version")); //设置版本headlineMapper.updateById(headline);return Result.ok(headlineMap);}
  1. HeadlineMapper.java接口
/*** 分页查询头条详情* @param hid* @return*/
Map selectDetailMap(Integer hid);
      mapperxml:
<!--    Map selectDetailMap(Integer hid);
查询目标(三表拼接):"hid":"1",                     // 新闻id "title":"马斯克宣布 ... ...",   // 新闻标题"article":"... ..."            // 新闻正文"type":"1",                    // 新闻所属类别编号"typeName":"科技",             // 新闻所属类别"pageViews":"40",              // 新闻浏览量"pastHours":"3" ,              // 发布时间已过小时数"publisher":"1" ,              // 发布用户ID"author":"张三"                 // 新闻作者-->/*
left join news_type t on h.type = t.tid: 这是一个左连接,将 "news_headline" 表与 "news_type" 表连接。
它的条件是 "news_headline" 表的 "type" 字段与 "news_type" 表的 "tid" 字段相匹配。
news_type中tid匹配的行会右拼接在headline表中left join news_user u on h.publisher = u.uid: 这也是一个左连接,将 "news_headline" 表与 "news_user" 表连接。
连接条件是 "news_headline" 表的 "publisher" 字段与 "news_user" 表的 "uid" 字段相匹配。
news_user中tid匹配的行会右拼接在headline表中(headline先拼type,再拼user)左连接确保左表保留所有信息,右表仅提取符合条件的元素匹配左表
*/
<select id="selectDetailMap" resultType="map">select hid,title,article,type, h.version ,tname typeName ,page_views pageViews,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher,nick_name author from news_headline hleft join news_type t on h.type = t.tidleft join news_user u  on h.publisher = u.uidwhere hid = #{hid}
</select>
http://www.lryc.cn/news/286981.html

相关文章:

  • YOCTO基础 - 创建meta层与bb文件
  • 网络电视盒子哪个好?博主分享超高性价比网络电视盒子推荐
  • leetcode 刷题2
  • 2-SAT问题相关理论和算法
  • 【大数据精讲】全量同步与CDC增量同步方案对比
  • 自定义通用返回对象
  • 从0开始python学习-51.pytest之接口加密封装
  • c++的命名空间
  • 阿富汗塔利班兴起时的比赛代码3475:练85.3 删数问题(Noip1994)
  • 大数据平台红蓝对抗 - 磨利刃,淬精兵!
  • 【2024-01-22】某极验3流程分析-滑块验证码
  • Laya2.13.3接入FGUI
  • 短视频账号矩阵系统+无人直播系统源码技术开发
  • C语言或C++通过IShellLinkA创建或解析lnk快捷方式(使用char字符数组)
  • Spring源码学习-Spring流程概述(一)
  • Figma怎么设置中文,Figma有中文版吗?
  • 智慧文旅一机游:科技与文化的完美结合,引领智慧文旅新潮流,智慧旅游未来已来
  • 多维时序 | Matlab实现CNN-LSTM-Mutilhead-Attention卷积长短期记忆神经网络融合多头注意力机制多变量时间序列预测
  • 软件工程实验报告(完整)
  • Java零基础学习20:集合的练习
  • 【latex】在Overleaf的IEEE会议模板中,快速插入参考文献
  • java反射之Field用法(获取对象的字段名和属性值)
  • Java Web(三)--CSS
  • 天津大数据培训班推荐,数据分析过程的常见错误
  • 【笔记】Helm-3 主题-17 弃用的Kubernetes API
  • 麒麟系统—— openKylin 安装 java
  • HTML学习笔记——07:其他嵌入技术
  • 【UE】在控件蓝图中通过时间轴控制材质参数变化
  • linux C语言socket函数send
  • Django(八)