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++代码的速 ...
随机推荐
- Mybatis:Mybatis 逆向工程 generator配置
一.使用Maven方式引入Mybatis依赖Jar包(版本号自己改或定义)
- Spring:Spring项目多接口实现类报错找不到指定类
spring可以通过applicationContext.xml进行配置接口实现类 applicationContext.xml中可以添加如下配置: 在application.properties中添 ...
- pdfkit html转pdf
pdfkit的通用option选项 参考:https://cloud.tencent.com/developer/ask/202116https://www.cnblogs.com/taceywong ...
- <jsp:param>传递参数,出现乱码问题
今天在学习<jsp:forward>和<jsp:param>时,用<jsp:param>传递参数时,出现乱码问题,部分代码如下: 1 <jsp:forward ...
- netcore3.1 + vue (前后端分离) ElementUI多文件带参数上传
vue前端代码 前端主要使用了ElementUI的el-uploda插件,除去业务代码需要注意的是使用formdata存储片上传时所需的参数 <el-upload class="upl ...
- servlet核心技术2
一.Servet 与 JDBC 在Servlet中可以使用JDBC技术访问数据库,查询DB数据,然后生成显示页面,接收请求参数,然后对DB操作 为了方便重用和便于维护等目的,经常会采用DAO(Data ...
- 「CF527E」 Data Center Drama
「CF527E」 Data Center Drama 传送门 显然一个环肯定满足题目条件. 然后我就开始想:先整一棵 \(\texttt{DFS}\) 树,然后非树边从深度深的节点向深度浅的节点连边, ...
- python04篇 文件操作(二)、集合
一.文件操作(二) 1.1 利用with来打开文件 # with open ,python 会自动关闭文件 with open('a.txt', encoding='utf-8') as f: # f ...
- 管理员的基本防范措施 Linux系统安全及应用
系统安全及应用一.账号安全基本措施① 系统账号清理② 密码安全控制③ 命令历史限制④ 终端自动注销二.SU命令切换用户① 用途及用法② 验证密码③ 限制使用su命令的用户④ 查看su操作记录补充三.L ...
- SQLITE数据库不支持远程访问
SQLITE数据库不支持远程访问 import sqlite3 conn=sqlite3.connect("dailiaq.db") cur=conn.cursor() def c ...