分布实现 开发社区首页,显示前10个帖子 开发分页组件,分页和显示所有帖子 开发步骤: 1 创建帖子实体类
package com.now.jiancommunity.entity;
import java.util.Date;
public class DiscussPost {
private int userId;
private String title;
private String content;
private int discussType;
private int status;
private Date CreateTime;
private int commnetCount;
private int score;
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 getDiscussType() {
return discussType;
}
public void setDiscussType(int discussType) {
this.discussType = discussType;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getCreateTime() {
return CreateTime;
}
public void setCreateTime(Date createTime) {
CreateTime = createTime;
}
public int getCommnetCount() {
return commnetCount;
}
public void setCommnetCount(int commnetCount) {
this.commnetCount = commnetCount;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "DiscussPost{" +
"userId=" + userId +
", title='" + title + '\'' +
", content='" + content + '\'' +
", discussType=" + discussType +
", status=" + status +
", CreateTime=" + CreateTime +
", commnetCount=" + commnetCount +
", score=" + score +
'}';
}
}
根据请求执行过程分布开发 2 建立DiscussPostMapper接口
@Mapper
public interface DiscussPostMapper {
List<DiscussPost> selectDiscussPosts(int userId,int offset,int limit);
int selectDiscussRows(@Param("userId") int userId);
}
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.now.jiancommunity.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="selectDiscussRows" resultType="int">
select count(id)
from discuss_post
where status != 2
<if test="userId != 0">
and user_id=#{userId}
</if>
</select>
</mapper>
3 服务层建立DiscussPostService
@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.selectDiscussRows(userId);
}
}
4 控制层建立HomeController
@Controller
public class HomeController {
@Autowired
private DiscussPostService discussPostService;
@Autowired
private UserService userService;
@RequestMapping(path= "/index" ,method = RequestMethod.GET)
public String getIndex(Model model, Page page){
page.setPath("/index");
page.setRows(discussPostService.findDiscussPostRows(0));
List<DiscussPost> discussPostList = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
ArrayList<Map<String,Object>> discussPosts=new ArrayList<>();
if(discussPostList != null) {
for(DiscussPost post:discussPostList){
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";
}
}
5 修改index.html界面 遍历discussPosts结合,得到post帖子map
th:each="map:${discussPosts}"
修改帖子列表 头像
th:src="${map.user.headerUrl}"
帖子主题
th:utext="${map.post.title}"
帖子状态
th:if="${map.post.status==1}"
帖子类型
th:if="${map.post.discussType==1}"
帖子姓名
th:utext="${map.user.username}"
创建时间
th:text="${#dates.format(map.post.createTime,'yyyy-MM-dd HH:mm:ss')}"
分页
<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>
|