长春新建火车站,郑州做景区网站建设公司,雄安 网站建设,wordpress带商城主题黑马头条#xff1a;app端文章查看 黑马头条#xff1a;app端文章查看文章列表加载1. 需求分析2. 表结构分析3. 导入文章数据库3.1 导入数据库3.2 导入对应的实体类 4. 实现思路5. 接口定义6. 功能实现6.1#xff1a;导入heima-leadnews-article微服务#xff0c;资料在当天… 黑马头条app端文章查看 黑马头条app端文章查看文章列表加载1. 需求分析2. 表结构分析3. 导入文章数据库3.1 导入数据库3.2 导入对应的实体类 4. 实现思路5. 接口定义6. 功能实现6.1导入heima-leadnews-article微服务资料在当天的文件夹中6.2定义接口6.3编写mapper文件6.4编写业务层代码6.5编写控制器代码6.6: swagger测试或前后端联调测试 黑马头条app端文章查看
文章列表加载
1. 需求分析
文章布局展示 2. 表结构分析
ap_article 文章基本信息表 ap_article_config 文章配置表 ap_article_content 文章内容表 三张表关系分析 3. 导入文章数据库
3.1 导入数据库
查看当天资料文件夹在数据库连接工具中执行leadnews_article.sql
3.2 导入对应的实体类
ap_article文章表对应实体
package com.heima.model.article.pojos;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;import java.io.Serializable;
import java.util.Date;/*** p* 文章信息表存储已发布的文章* /p** author itheima*/Data
TableName(ap_article)
public class ApArticle implements Serializable {TableId(value id,type IdType.ID_WORKER)private Long id;/*** 标题*/private String title;/*** 作者id*/TableField(author_id)private Long authorId;/*** 作者名称*/TableField(author_name)private String authorName;/*** 频道id*/TableField(channel_id)private Integer channelId;/*** 频道名称*/TableField(channel_name)private String channelName;/*** 文章布局 0 无图文章 1 单图文章 2 多图文章*/private Short layout;/*** 文章标记 0 普通文章 1 热点文章 2 置顶文章 3 精品文章 4 大V 文章*/private Byte flag;/*** 文章封面图片 多张逗号分隔*/private String images;/*** 标签*/private String labels;/*** 点赞数量*/private Integer likes;/*** 收藏数量*/private Integer collection;/*** 评论数量*/private Integer comment;/*** 阅读数量*/private Integer views;/*** 省市*/TableField(province_id)private Integer provinceId;/*** 市区*/TableField(city_id)private Integer cityId;/*** 区县*/TableField(county_id)private Integer countyId;/*** 创建时间*/TableField(created_time)private Date createdTime;/*** 发布时间*/TableField(publish_time)private Date publishTime;/*** 同步状态*/TableField(sync_status)private Boolean syncStatus;/*** 来源*/private Boolean origin;/*** 静态页面地址*/TableField(static_url)private String staticUrl;
}ap_article_config文章配置对应实体类
package com.heima.model.article.pojos;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;import java.io.Serializable;/*** p* APP已发布文章配置表* /p** author itheima*/Data
TableName(ap_article_config)
public class ApArticleConfig implements Serializable {TableId(value id,type IdType.ID_WORKER)private Long id;/*** 文章id*/TableField(article_id)private Long articleId;/*** 是否可评论* true: 可以评论 1* false: 不可评论 0*/TableField(is_comment)private Boolean isComment;/*** 是否转发* true: 可以转发 1* false: 不可转发 0*/TableField(is_forward)private Boolean isForward;/*** 是否下架* true: 下架 1* false: 没有下架 0*/TableField(is_down)private Boolean isDown;/*** 是否已删除* true: 删除 1* false: 没有删除 0*/TableField(is_delete)private Boolean isDelete;
}ap_article_content 文章内容对应的实体类
package com.heima.model.article.pojos;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;import java.io.Serializable;Data
TableName(ap_article_content)
public class ApArticleContent implements Serializable {TableId(value id,type IdType.ID_WORKER)private Long id;/*** 文章id*/TableField(article_id)private Long articleId;/*** 文章内容*/private String content;
}4. 实现思路 1,在默认频道展示10条文章信息
2,可以切换频道查看不同种类文章
3,当用户下拉可以加载最新的文章分页本页文章列表中发布时间为最大的时间为依据
4,当用户上拉可以加载更多的文章信息按照发布时间本页文章列表中发布时间最小的时间为依据
5如果是当前频道的首页前端传递默认参数 maxBehotTime0毫秒 minBehotTime20000000000000毫秒—2063年
5. 接口定义
加载首页加载更多加载最新接口路径/api/v1/article/load/api/v1/article/loadmore/api/v1/article/loadnew请求方式POSTPOSTPOST参数ArticleHomeDtoArticleHomeDtoArticleHomeDto响应结果ResponseResultResponseResultResponseResult
ArticleHomeDto
package com.heima.model.article.dtos;import lombok.Data;import java.util.Date;Data
public class ArticleHomeDto {// 最大时间Date maxBehotTime;// 最小时间Date minBehotTime;// 分页sizeInteger size;// 频道IDString tag;
}6. 功能实现
6.1导入heima-leadnews-article微服务资料在当天的文件夹中 注意需要在heima-leadnews-service的pom文件夹中添加子模块信息如下
modulesmoduleheima-leadnews-user/modulemoduleheima-leadnews-article/module
/modules在idea中的maven中更新一下如果工程还是灰色的需要在重新添加文章微服务的pom文件操作步骤如下 需要在nacos中添加对应的配置
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/leadnews_article?useUnicodetruecharacterEncodingUTF-8serverTimezoneUTCusername: rootpassword: root
# 设置Mapper接口所对应的XML文件位置如果你在Mapper接口中有自定义方法需要进行该配置
mybatis-plus:mapper-locations: classpath*:mapper/*.xml# 设置别名包扫描路径通过该属性可以给包中的类注册别名type-aliases-package: com.heima.model.article.pojos6.2定义接口
package com.heima.article.controller.v1;import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/api/v1/article)
public class ArticleHomeController {PostMapping(/load)public ResponseResult load(RequestBody ArticleHomeDto dto) {return null;}PostMapping(/loadmore)public ResponseResult loadMore(RequestBody ArticleHomeDto dto) {return null;}PostMapping(/loadnew)public ResponseResult loadNew(RequestBody ArticleHomeDto dto) {return null;}
}6.3编写mapper文件
package com.heima.article.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;Mapper
public interface ApArticleMapper extends BaseMapperApArticle {public ListApArticle loadArticleList(Param(dto) ArticleHomeDto dto, Param(type) Short type);}对应的映射文件
在resources中新建mapper/ApArticleMapper.xml 如下配置
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.heima.article.mapper.ApArticleMapperresultMap idresultMap typecom.heima.model.article.pojos.ApArticleid columnid propertyid/result columntitle propertytitle/result columnauthor_id propertyauthorId/result columnauthor_name propertyauthorName/result columnchannel_id propertychannelId/result columnchannel_name propertychannelName/result columnlayout propertylayout/result columnflag propertyflag/result columnimages propertyimages/result columnlabels propertylabels/result columnlikes propertylikes/result columncollection propertycollection/result columncomment propertycomment/result columnviews propertyviews/result columnprovince_id propertyprovinceId/result columncity_id propertycityId/result columncounty_id propertycountyId/result columncreated_time propertycreatedTime/result columnpublish_time propertypublishTime/result columnsync_status propertysyncStatus/result columnstatic_url propertystaticUrl//resultMapselect idloadArticleList resultMapresultMapSELECTaa.*FROMap_article aaLEFT JOIN ap_article_config aac ON aa.id aac.article_idwhereand aac.is_delete ! 1and aac.is_down ! 1!-- loadmore --if testtype ! null and type 1and aa.publish_time ![CDATA[]] #{dto.minBehotTime}/ifif testtype ! null and type 2and aa.publish_time ![CDATA[]] #{dto.maxBehotTime}/ifif testdto.tag ! __all__and aa.channel_id #{dto.tag}/if/whereorder by aa.publish_time desclimit #{dto.size}/select/mapper6.4编写业务层代码
package com.heima.article.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;import java.io.IOException;public interface ApArticleService extends IServiceApArticle {/*** 根据参数加载文章列表* param loadtype 1为加载更多 2为加载最新* param dto* return*/ResponseResult load(Short loadtype, ArticleHomeDto dto);}实现类
package com.heima.article.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.Date;
import java.util.List;Service
Transactional
Slf4j
public class ApArticleServiceImpl extends ServiceImplApArticleMapper, ApArticle implements ApArticleService {// 单页最大加载的数字private final static short MAX_PAGE_SIZE 50;Autowiredprivate ApArticleMapper apArticleMapper;/*** 根据参数加载文章列表* param loadtype 1为加载更多 2为加载最新* param dto* return*/Overridepublic ResponseResult load(Short loadtype, ArticleHomeDto dto) {//1.校验参数Integer size dto.getSize();if(size null || size 0){size 10;}size Math.min(size,MAX_PAGE_SIZE);dto.setSize(size);//类型参数检验if(!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_MORE)!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){loadtype ArticleConstants.LOADTYPE_LOAD_MORE;}//文章频道校验if(StringUtils.isEmpty(dto.getTag())){dto.setTag(ArticleConstants.DEFAULT_TAG);}//时间校验if(dto.getMaxBehotTime() null) dto.setMaxBehotTime(new Date());if(dto.getMinBehotTime() null) dto.setMinBehotTime(new Date());//2.查询数据ListApArticle apArticles apArticleMapper.loadArticleList(dto, loadtype);//3.结果封装ResponseResult responseResult ResponseResult.okResult(apArticles);return responseResult;}}定义常量类
package com.heima.common.constants;public class ArticleConstants {public static final Short LOADTYPE_LOAD_MORE 1;public static final Short LOADTYPE_LOAD_NEW 2;public static final String DEFAULT_TAG __all__;}6.5编写控制器代码
package com.heima.article.controller.v1;import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/api/v1/article)
public class ArticleHomeController {Autowiredprivate ApArticleService apArticleService;PostMapping(/load)public ResponseResult load(RequestBody ArticleHomeDto dto) {return apArticleService.load(ArticleConstants.LOADTYPE_LOAD_MORE,dto);}PostMapping(/loadmore)public ResponseResult loadMore(RequestBody ArticleHomeDto dto) {return apArticleService.load(ArticleConstants.LOADTYPE_LOAD_MORE,dto);}PostMapping(/loadnew)public ResponseResult loadNew(RequestBody ArticleHomeDto dto) {return apArticleService.load(ArticleConstants.LOADTYPE_LOAD_NEW,dto);}
}6.6: swagger测试或前后端联调测试
第一在app网关的微服务的nacos的配置中心添加文章微服务的路由完整配置如下
spring:cloud:gateway:globalcors:cors-configurations:[/**]: # 匹配所有请求allowedOrigins: * #跨域处理 允许所有的域allowedMethods: # 支持的方法- GET- POST- PUT- DELETEroutes:# 用户微服务- id: useruri: lb://leadnews-userpredicates:- Path/user/**filters:- StripPrefix 1# 文章微服务- id: articleuri: lb://leadnews-articlepredicates:- Path/article/**filters:- StripPrefix 1第二启动nginx直接使用前端项目测试启动文章微服务用户微服务、app网关微服务 高性能 作为高性能对象存储在标准硬件条件下它能达到55GB/s的读、35GB/s的写速率 可扩容 不同MinIO集群可以组成联邦并形成一个全局的命名空间并跨越多个数据中心 SDK支持 基于Minio轻量的特点它得到类似Java、Python或Go等语言的sdk支持 有操作页面 面向用户友好的简单操作界面非常方便的管理Bucket及里面的文件资源 功能简单 这一设计原则让MinIO不容易出错、更快启动 丰富的API 支持文件资源的分享连接及分享链接的过期策略、存储桶操作、文件列表访问及文件上传下载的基本功能等。 文件变化主动通知 存储桶Bucket如果发生改变,比如上传对象和删除对象可以使用存储桶事件通知机制进行监控并通过以下方式发布出去:AMQP、MQTT、Elasticsearch、Redis、NATS、MySQL、Kafka、Webhooks等。