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

news/2024/7/7 20:21:55

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 {

    @Autowired
    private 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{

    @Autowired
    private TypeMapper typeMapper;


    @Override
    public 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
      @Override
        public 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); //阅读量+1
            headline.setVersion((Integer) headlineMap.get("version")); //设置版本
            headlineMapper.updateById(headline);

            return Result.ok(headlineMap);
        }
  1. mapper
/**
 * 分页查询头条详情
 * @param hid
 * @return
 */
Map selectDetailMap(Integer hid);
      mapperxml:
<!--    Map selectDetailMap(Integer hid);-->
<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 h
                        left join news_type t on h.type = t.tid
                                left join news_user u  on h.publisher = u.uid
                                            where hid = #{hid}
</select>

http://lihuaxi.xjx100.cn/news/1984331.html

相关文章

什么叫特征分解?

特征分解&#xff08;Eigenvalue Decomposition&#xff09;是将一个方阵分解为特征向量和特征值的过程。对于一个 nn 的方阵A&#xff0c;其特征向量&#xff08;Eigenvector&#xff09;v 和特征值&#xff08;Eigenvalue&#xff09; λ 满足以下关系&#xff1a; 这可以写…

攸信UMS工业配送AMR F1引领智能物流发展,侨智大会瞩目焦点

近日&#xff0c;由中国侨联、福建省人民政府共同主办的第一届中国侨智发展大会在福州开幕。本次大会以“五洲聚‘福’汇侨智&#xff0c;同心共圆中国梦”为主题&#xff0c;立足福建、服务全国、面向海外&#xff0c;吸引了来自37个国家和地区的一千余名海内外嘉宾参会。 01|…

获取数组中的第一个、第二个、第三个......元素

常规操作可以直接使用索引&#xff08;下标&#xff09;获取&#xff1a; const arr [5,8,6,9,10] const first arr[0] //5 const second arr[1] //8 const third arr[2] //6 不使用索引&#xff0c;如何获取&#xff1a; const [first] [5,8,6,9,10] //…

访问服务器上的 Jupyter Notebook

文章目录 1、生成秘钥2、修改配置3、启动 Jupyter 安装和基本使用方法可见&#xff1a; https://blog.csdn.net/lovechris00/article/details/123458990 1、生成秘钥 jupyter notebook password输入秘钥后&#xff0c;将生成秘钥文件&#xff0c;进入文件&#xff0c;复制那一…

二、docker的常用命令(持续补充img)

目录 一、启动相关1.设置容器开机启动 二、查询相关1.查询所有容器&#xff08;包括停止的&#xff09; 三、修改相关1.指定容器开机自启动 一、启动相关 1.设置容器开机启动 在我们使用镜像run一个容器的时候&#xff0c;希望这个容器随着docker的启动而启动&#xff08;我的…

【算法与数据结构】322、LeetCode零钱兑换

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;本题可以抽象成一个完全背包问题。 第一步&#xff0c; d p [ j ] dp[j] dp[j]的含义。 d p [ j ] dp…

NVIDIA 再现逆天刀法,千元级 RTX 新卡曝光

这年头&#xff0c;价格 1 字开头入门甜品显卡几乎真就成 N、A 两家弃子了。 以 NVIDIA RTX 4060 与 AMD RX 7600 为例&#xff0c;这两块显卡同为两家新品中最低端型号&#xff0c;其价格直接来到 2K 左右起步。 要知道目前很多普通用户选购一台日常办公、游戏电脑主机&#…

用ul和li模拟一个可以点击的日历

用ul和li模拟一个可以点击的日历,因为需求是如果某天有事件则需要自动标红,效果图如下 注意!!!这处因为需要和时间选择器联动 ,所以把这个页面封装成一个组件,代码 <template><div class"dates"><div id"calendar"><!-- 年份 月份 -…