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 {

    @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. 需求描述

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

你可能感兴趣的:(SpringBoot,spring,boot,java,后端)