学习笔记:基于SpringBoot的牛客网社区项目实现(四)之社区主页实现
创始人
2024-06-02 15:39:32
0

一、创建主页实体类与数据库中对应,新建帖子Mapper接口

@Mapper
public interface DiscussPostMapper {List selectDiscussPosts(int userId, int offset, int limit);// @Param注解用于给参数取别名,// 如果只有一个参数,并且在里使用,则必须加别名.int selectDiscussPostRows(@Param("userId") int userId);}

userId为了以后选择个人主页发布的帖子,offset每页起始行的行号,limit每页显示多少条数据

业务层的方法写在Service层,加@Service注解。

@Service
public class DiscussPostService {@Autowiredprivate DiscussPostMapper discussPostMapper;public List findDiscussPosts(int userId, int offset, int limit) {return discussPostMapper.selectDiscussPosts(userId, offset, limit);}public int findDiscussPostRows(int userId) {return discussPostMapper.selectDiscussPostRows(userId);}}

在Controller层写对浏览器的操作,接收数据以及返回数据

@RequestMapping(path = "/index", method = RequestMethod.GET)
public String getIndexPage(Model model, Page page) {// 方法调用钱,SpringMVC会自动实例化Model和Page,并将Page注入Model.// 所以,在thymeleaf中可以直接访问Page对象中的数据.page.setRows(discussPostService.findDiscussPostRows(0));page.setPath("/index");List list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());List> discussPosts = new ArrayList<>();if (list != null) {for (DiscussPost post : list) {Map map = new HashMap<>();map.put("post", post);User user = userService.findUserById(post.getUserId());map.put("user", user);discussPosts.add(map);}}model.addAttribute("discussPosts", discussPosts);//前端可以从model中取数据return "/index";
}

相关内容

热门资讯

修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
【前端】‘??‘与‘||‘有什... 0 问题 经常写const data = res.data.a ?? ''或者const d...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
牛客计算器的改良(Python... 文章目录1.题目描述2.输入描述:3.输出描述:4.示例15.分析6.代码7.结语 链接࿱...
正大杯|市调大赛|2023备赛... 关键信息 同时随着精细化养宠趋势的深入,宠物消费类目日渐丰富。 本报告通过 Niuco...
QGIS下载在线地图(Goog... 前言 国内外有很多在线地图下载软件,但功能单一,基本上只能下载数据&#x...