开发社区首页

  • 显示首页
    • 过程理解
    • Dao
    • Service
    • Controller
  • 实现分页
    • 过程理解
    • Controller

显示首页

过程理解

  • 目标:显示帖子
  • 原料:数据库内帖子数据
  • 步骤:
    • 按要求获取数据库内的帖子数据信息(Dao)
    • 提供Controller所需要的数据:帖子,总行数(Service)
    • 用Model存入数据,返回html(Controller)
    • 修改视图层模板

Dao

实体类对应数据库中的帖子表信息
Mapper接口获取数据(实体类的实例)
.xml Mapper接口对应的SQL查询语句

  • 实体类
// 实体类-entity-DiscussPost
package com.nowcoder.community.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.Date;
@Document(indexName = "discusspost", type = "_doc", shards = 6, replicas = 3)
public class DiscussPost {@Idprivate int id;@Field(type = FieldType.Integer)private int userId;public int getId() {return id;}public void setId(int id) {this.id = id;}//...@Overridepublic String toString() {return "DiscussPost{" +"id=" + id +", userId=" + userId +", title='" + title + '\'' +", content='" + content + '\'' +", type=" + type +", status=" + status +", createTime=" + createTime +", commentCount=" + commentCount +", score=" + score +'}';}
}
  • Mapper接口
// Mapper接口-dao-DiscussPostMapper
package com.nowcoder.community.dao;
import com.nowcoder.community.entity.DiscussPost;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface DiscussPostMapper {List<DiscussPost> selectDiscussPosts(int userId, int offset, int limit, int orderMode);// @Param注解用于给参数取别名,// 如果只有一个参数,并且在<if>里使用,则必须加别名.int selectDiscussPostRows(@Param("userId") int userId);int insertDiscussPost(DiscussPost discussPost);
}
  • 接口对应SQL语句
<!--映射对应的接口-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nowcoder.community.dao.DiscussPostMapper"><sql id="selectFields">id, user_id, title, content, type, status, create_time, comment_count, score</sql><sql id="insertFields">user_id, title, content, type, status, create_time, comment_count, score</sql><select id="selectDiscussPosts" resultType="DiscussPost">select <include refid="selectFields"></include>from discuss_postwhere status != 2<if test="userId!=0">and user_id = #{userId}</if><if test="orderMode==0">order by type desc, create_time desc</if><if test="orderMode==1">order by type desc, score desc, create_time desc</if>limit #{offset}, #{limit}</select><select id="selectDiscussPostRows" resultType="int">select count(id)from discuss_postwhere status != 2<if test="userId!=0">and user_id = #{userId}</if></select></mapper>

Service

向Controller提供帖子和总行数

// community-service-DiscussPostService
package com.nowcoder.community.service;
@Service
public class DiscussPostService {@Autowiredprivate DiscussPostMapper discussPostMapper;public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit, int orderMode) {return discussPostMapper.selectDiscussPosts(userId, offset, limit, orderMode);}public int findDiscussPostRows(int userId) {return discussPostMapper.selectDiscussPostRows(userId);}
}

Controller

数据装在Model中,修改模板,返回html

// community-controller-HomeController
package com.nowcoder.community.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class HomeController implements CommunityConstant {@Autowiredprivate DiscussPostService discussPostService;@Autowiredprivate UserService userService;@RequestMapping(path = "/index", method = RequestMethod.GET)public String getIndexPage(Model model) {List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(),orderMode);List<Map<String, Object>> discussPosts = new ArrayList<>();if (list != null) {for (DiscussPost post : list) {Map<String, Object> map = new HashMap<>();map.put("post", post);User user = userService.findUserById(post.getUserId());map.put("user", user);discussPosts.add(map);}}model.addAttribute("discussPosts", discussPosts);return "/index";}
}

实现分页

过程理解

  • 页码实体类,方便或许分页的页面信息
  • 将Page注入Model中,返回html
  • Page的信息,通过url中传过来

Controller

添加page的实体类
page的信息,通过url中传过来

<!-- /index?current=1&limit=5 -->
<a class="page-link" th:href="@{${page.path}(current=${page.current-1})}">上一页</a></li>
// community-controller-HomeController@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?orderMode="+orderMode);List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(),orderMode);List<Map<String, Object>> discussPosts = new ArrayList<>();if (list != null) {for (DiscussPost post : list) {Map<String, Object> map = new HashMap<>();map.put("post", post);User user = userService.findUserById(post.getUserId());map.put("user", user);discussPosts.add(map);}}model.addAttribute("discussPosts", discussPosts);return "/index";
}

仿牛客论坛-开发社区首页-3相关推荐

  1. 仿牛客论坛项目(上)

    代码仓库:https://gitee.com/qiuyusy/community 仿牛客论坛项目(下) 仿牛客论坛项目上 1. Spring 在测试类中使用Spring环境 @Primary的作用 @ ...

  2. 仿牛客论坛项目(4)

    仿牛客论坛项目 一.Elasticsearch入门 1.1 elasticsearch安装 1.2 修改config目录下的elasticsearch.yml配置文件 1.3 配置环境变量 1.4 下 ...

  3. 仿牛客论坛项目(下)

    代码仓库:https://gitee.com/qiuyusy/community 仿牛客论坛项目(上) 仿牛客论坛项目 15.kafka 1.阻塞队列 2.Kafka入门 简介 术语解释 下载 配置 ...

  4. 从零开始—仿牛客网讨论社区项目(一)

    主要技术架构: SpringBoot Spring SpringMVC MyBatis Redis Kakfa Elasticsearch Spring Security Spring Actator ...

  5. 从零开始—仿牛客网讨论社区项目(六)

    主要技术架构: SpringBoot Spring SpringMVC MyBatis Redis Kakfa Elasticsearch Spring Security Spring Actator ...

  6. 仿牛客论坛项目(3)

    仿牛客论坛项目 一.阻塞队列 1.1 测试 二.kafka入门 2.1 kafka下载 2.2 测试 三.Spring整合kafka 3.1 引入依赖 3.2 修改配置文件 3.3 测试 四.发布系统 ...

  7. 仿牛客论坛项目(5)

    仿牛客论坛项目 一.SpringSecurity入门案例 1.1 添加依赖 1.2 配置文件 1.3 工具类 CommunityUtil 1.4 配置类 SecurityConfig 1.5 实体类 ...

  8. 仿牛客网讨论社区项目—优化网站性能

    性能优化: 1.考虑加入缓存优化 优化热门帖子列表 GitHub中搜索caffeine 在Maven Repository搜索caffeine配置文件,在resources文件包内的pom.xml文件 ...

  9. (仿牛客论坛项目)01 - 开发社区首页

    文章目录 前言 1.做项目的步骤 2.开发社区首页功能分步 2.1 User 类 2.2 UserMapper 接口 2.3 UserMapper 映射文件 2.4 编写测试类 3.开发社区首页,显示 ...

最新文章

  1. CentOS7配置ntp服务
  2. 每日简单小妙招:使用python实现控制摄像头拍照并将其发送某某邮箱(仅供学习)
  3. rxjs pipe和filter组合的一个实际例子的单步调试
  4. AM2320 温湿度计 单总线读取数据
  5. stm32 PWM输入捕获
  6. 图像目标检测(Object Detection)原理与实现(三)
  7. 最硬核Visual AssistX 安装破解(2019最新 通用)内含破解原理
  8. AI考拉技术分享会--IDE 常用功能 for Node.js
  9. vue项目引入iconfont图标
  10. nuke linux 插件,NUKE插件-Pos工具包V1.2
  11. cad导入mysql_CAD插入一个数据库
  12. Hardhat 学习笔记
  13. K分查找时间复杂度推导
  14. 编译 ORB-SLAM2/3的ROS工程造成(You should double-check your ROS_PACKAGE_PATH...)
  15. vue3.2中ref高效的秘密:依赖收集错级位运算操作(bit/dep.w/dep.n)
  16. 网易web安全:课后问题-CSRF
  17. 语音朗读html的源码,在网页上通过JS实现文本的语音朗读
  18. 信息熵--硬币称重问题-详解
  19. 怎样选择双机热备软件
  20. 特征可视化技术(CAM)

热门文章

  1. python超级大脑游戏mooc_三人游超级大脑挑战
  2. Java面试补给站——Map一家亲
  3. 本题要求实现一个函数,对给定的一个字符串和两个字符,打印出给定字符串中从与第一个字符匹配的位置开始到与第二个字符匹配的位置之间的所有字符
  4. 易语言大漠多线程foobar在游戏多窗口中时时显示输出信息
  5. hⅰgh怎么读音发音英语_【h_i_gh】什么意思_英语h_i_gh的翻译_音标_读音_用法_例句_在线翻译_有道词典...
  6. Python中Request 使用socks5代理
  7. 2012第25周国内Android应用下载排行榜动态
  8. 阿里巴巴参谋长曾鸣全面深入阐释:何谓互联网的本质?
  9. Java,第一次作业——某天是星期几
  10. 2023 多签提币接口通道HTML源码