SpringBoot开发五-社区首页开发
需求介绍—社区首页
根据之前的学习,我们一般都是先按照DAO->Service->Controller这个顺序去开发
分布实现:
开发社区首页,显示前十个帖子。
开发分页组件,分页显示所有的帖子
代码实现
首先我们要知道贴子我们是放在discuss_post这个表里面,所以我们的操作都是根据这个表来操作。
那第一步来写一下DiscussPost实体类对应这个表里面的字段。
package com.nowcoder.community.entity;
import java.util.Date;
public class DiscussPost {
private int id;
private int userId;
private String title;
private String content;
private int type;
private int status;
private Date createTime;
private int commentCount;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public int getCommentCount() {
return commentCount;
}
public void setCommentCount(int commentCount) {
this.commentCount = commentCount;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "DiscussPost{" +
"id=" + id +
", userId=" + userId +
", title='" + title + '\'' +
", content='" + content + '\'' +
", type=" + type +
", status=" + status +
", createTime=" + createTime +
", commentCount=" + commentCount +
", score=" + score +
'}';
}
}
那么我们对应去开发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 { /* 查到的事多个帖子,所以是集合,这个userId是为了开发我的主页的时候才需要,但是首页的时候就不传,
得到的是0,那么对应的就是一个动态的sql*/
List<DiscussPost> selectDiscussPosts(int userId, int offset, int limit);
// @Param 用来为参数取别名
//如果需要动态的拼一个条件,在<if>里面使用,并且这个方法有且只有一个参数,这个参数前面必须取别名
// 这个userId和上面是一样的功能
int selectDiscussPostRows(@Param("userId") int userId); // int insertDiscussPost(DiscussPost discussPost); }
那么函数声明了,就要去discusspost-mapper.xml文件里面写实现的了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//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> <select id="selectDiscussPosts" resultType="DiscussPost">
select <include refid="selectFields"></include>
from discuss_post
where status != 2
<if test="userId!=0">
and user_id = #{userId}
</if>
order by type desc, create_time desc
limit #{offset}, #{limit}
</select> <select id="selectDiscussPostRows" resultType="int">
select count(id)
from discuss_post
where status != 2
<if test="userId!=0">
and user_id = #{userId}
</if>
</select> </mapper>
至此数据访问层就开发完了,那么要继续开发业务组件DiscussPostService。
package com.nowcoder.community.service; import com.nowcoder.community.dao.DiscussPostMapper;
import com.nowcoder.community.entity.DiscussPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class DiscussPostService { @Autowired
private DiscussPostMapper discussPostMapper; public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit) {
return discussPostMapper.selectDiscussPosts(userId, offset, limit);
} public int findDiscussPostRows(int userId) {
return discussPostMapper.selectDiscussPostRows(userId);
} }
但是现在有个问题就是在平时的时候我们在页面上显示的肯定都是显示username而不是userId所以我们需要写个方法把两者关联起来,因为这个只涉及到User那么我们可以对写UserService对其进行操作
package com.nowcoder.community.service; import com.nowcoder.community.dao.UserMapper;
import com.nowcoder.community.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserService { @Autowired
private UserMapper userMapper; public User findUserById(int id) {
return userMapper.selectById(id);
}
}
那么至此业务层的逻辑我们就写完了,现在就开始写表现层DiscussController:
我们先查前10条的帖子
package com.nowcoder.community.controller; import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; // 初始化界面以及分页功能点实现
@Controller
public class HomeController implements CommunityConstant { @Autowired
private DiscussPostService discussPostService; @Autowired
private UserService userService; @RequestMapping(path = "/index", method = RequestMethod.GET)
public String getIndexPage(Model model) {
// 方法调用之前,SpringMVC会自动实例化Model和Page,并将Page注入到Model中
// 所以,在thymeleaf中可以直接访问Page对象中的数据。
List<DiscussPost> list = discussPostService.findDiscussPosts(0,0,10);
// 把上面这个集合遍历一遍,但是它们的数据是不完整的,比方说我们查到的只是id不是userId
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来确定分页的一些条件
package com.nowcoder.community.entity; /**
* 封装分页相关信息
*/
public class Page { // 页面传给服务端的数据
// 当前页码
private int current = 1; // 显示上限
private int limit = 10; // 服务端查到的
// 数据总数(用于计算总页数)
private int rows; // 查询路径(用于复用分页链接)
private String path; public int getCurrent() {
return current;
} public void setCurrent(int current) {
if (current >= 1) {
this.current = current;
}
} public int getLimit() {
return limit;
} public void setLimit(int limit) {
if (limit >= 1 && limit <= 100) {
this.limit = limit;
}
} public int getRows() {
return rows;
} public void setRows(int rows) {
if (rows >= 0) {
this.rows = rows;
}
} public String getPath() {
return path;
} public void setPath(String path) {
this.path = path;
} /**
* 获取当前页的起始行
*
* @return
*/
public int getOffset() {
// current * limit - limit
return (current - 1) * limit;
} /**
* 获取总页数
*
* @return
*/
public int getTotal() {
// rows / limit
if (rows % limit == 0) {
return rows / limit;
} else {
return rows / limit + 1;
}
} /**
* 获取起始页码
*/
public int getFrom() {
int from = current - 2;
return from < 1 ? 1 : from;
} /**
* 获取结束页码
*
* @return
*/
public int getTo() {
int to = current + 2;
return to > getTotal() ? getTotal() : to;
}
}
那么要改造HomeController来实现分页:
package com.nowcoder.community.controller; import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; @Controller
public class HomeController {
@Autowired
private DiscussPostService discussPostService; @Autowired
private UserService userService; @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<DiscussPost> list = discussPostService.findDiscussPosts(0,page.getOffset(),page.getLimit());
// 把上面这个集合遍历一遍,但是它们的数据是不完整的,比方说我们查到的只是id不是userId
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";
} }
那么还要在页面上动态的配置好对应的链接
<nav class="mt-5" th:if="${page.rows>0}" th:fragment="pagination">
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" th:href="@{${page.path}(current=1)}">首页</a>
</li>
<li th:class="|page-item ${page.current==1?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${page.current-1})}">上一页</a></li>
<li th:class="|page-item ${i==page.current?'active':''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
<a class="page-link" href="#" th:text="${i}">1</a>
</li>
<li th:class="|page-item ${page.current==page.total?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${page.current+1})}">下一页</a>
</li>
<li class="page-item">
<a class="page-link" th:href="@{${page.path}(current=${page.total})}">末页</a>
</li>
</ul>
</nav>

SpringBoot开发五-社区首页开发的更多相关文章
- Swift3.0服务端开发(五) 记事本的开发(iOS端+服务端)
前边以及陆陆续续的介绍了使用Swift3.0开发的服务端应用程序的Perfect框架.本篇博客就做一个阶段性的总结,做一个完整的实例,其实这个实例在<Swift3.0服务端开发(一)>这篇 ...
- 【SpringBoot】Spring Boot,开发社区讨论交流网站首页。
初识Spring Boot,开发社区讨论交流网站首页. 文章目录 初识Spring Boot,开发社区讨论交流网站首页. 1.项目简介 2. 搭建开发环境 JDK Apache Maven Intel ...
- asp.net mvc开发的社区产品相关开发文档分享
分享一款基于asp.net mvc框架开发的社区产品--近乎.目前可以在官网免费下载,下载地址:http://www.jinhusns.com/Products/Download?type=whp 1 ...
- springboot 使用webflux响应式开发教程(二)
本篇是对springboot 使用webflux响应式开发教程(一)的进一步学习. 分三个部分: 数据库操作webservicewebsocket 创建项目,artifactId = trading- ...
- java web 学习五(servlet开发1)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- 使用hubuild,mui开发微信app—首页(一)
写在前面 本系列文章我将介绍一下从零开始利用hubuild,mui实现微信app的开发,该系列是个人学习记录,所以在每篇文章中,都是从怎么去实现开始讲解,然后再把实例中涉及知识点做一个概述. 创建一个 ...
- Vue 旅游网首页开发2 - 首页编写
Vue 旅游网首页开发2 - 首页编写 项目结构 首页开发 效果图 项目开发组件化 将页面的各个部分划分成不同的组件,有助于项目的开发和维护. 项目代码初始化 项目结构修改 1.删除整个 compin ...
- Springboot Application 集成 OSGI 框架开发
内容来源:https://www.ibm.com/developerworks/cn/java/j-springboot-application-integrated-osgi-framework-d ...
- go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE
go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE Go语言是谷歌2009发布的专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速 ...
随机推荐
- CentOS-Docker安装phpMyAdmin(MySQL的web客户端)
注:phpMyAdmin是一款MySQL的web客户端,还是很直观且好用的,所以推荐给你~ 下载镜像 $ docker pull phpmyadmin/phpmyadmin 运行镜像 1.无指定固定数 ...
- android开发相关知识笔记
1.xpage页面打开: openPage(TestFragment.class) openPage("标识") // 页面打开等待结果返回: openPageForResult( ...
- Kotlin Coroutine(协程): 四、+ Retrofit
@ 目录 前言 一.准备工作 二.开始使用 1.简单使用 2.DSL 3.扩展函数 4.请求发起 总结 前言 Retrofit 从 2.6.0 版本开始, 内置了对 Kotlin Coroutines ...
- 深入理解Java并发类——AQS
目录 什么是AQS 为什么需要AQS AQS的核心思想 AQS的内部数据和方法 如何利用AQS实现同步结构 ReentrantLock对AQS的利用 尝试获取锁 获取锁失败,排队竞争 参考 什么是AQ ...
- python使用笔记007-内置函数,匿名函数
1.匿名函数 匿名函数也是一个函数,是一个简单的函数,没有名字,只能实现一些简单的功能 1 #匿名函数也是一个函数,是一个简单的函数,没有名字,只能实现一些简单的功能 2 lambda x:x+1#入 ...
- Linux下面向TCP连接的C++ Socket编程实例
Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.即Socket提供了操作上述特殊文件的接口,使用这些接口可以实现网络编程. Socket通信流程图 TCP(Transmis ...
- 聊聊 Spring AOP 的不为常知的“秘事”
Spring AOP 在我们日常开发中扮演了一个非常重要的角色,对于如何使用 AOP 相信很多人已经不陌生,但其中有一些点却容易被我们忽视,本节我们结合一些"不为常知"的问题展开讨 ...
- IDEA上搭建spark开发
IDEA上搭建spark开发环境 我本地系统是windows10,首先IDEA上要安装了scala插件. 1.下载winutils.exe文件 winutils.exe是在Windows系统上需要的h ...
- Python基础之魔术方法(控制属性的访问和设置)
# 魔术方法--常规方法# 1. __int__ 构造函数# 2. __new__ 在类实例之前就创建了# 3. __iter__ 迭代器# 4. __del__ 析构方法,用来清除释放的对象内存# ...
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【三】
软件定时器:是基于系统Tick时钟中断且由软件来模拟的定时器,当经过设定的Tick时钟计数值后会触发用户定义的回调函数.定时精度与系统Tick时钟的周期有关. 定时器运行机制: cmsis_os2的A ...