Mapper
List<DiscussPost> queryAll(int userId, int offset, int limit);
Mapper.xml
<sql id="selectFields">
id, user_id, title, content, type, status, create_time, comment_count, score
</sql>
<select id="queryAll" 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>
ServiceImpl
@Override
public List<DiscussPost> queryAll(int userId, int offset, int limit) {
return discussPostMapper.queryAll(userId, offset, limit);
}
Controller
@GetMapping({"/","/index"})
public String getIndexPage(Model model, Page page) {
/**
* 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model
* 所以,在thymeleaf中可以直接访问Page对象中的数据
*/
page.setRows(discussPostService.queryRows(0));
page.setPath("/index");
List<DiscussPost> list = discussPostService.queryAll(0, page.getOffset(), page.getLimit());
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.queryById(post.getUserId());
map.put("user", user);
discussPosts.add(map);
}
}
model.addAttribute("discussPosts", discussPosts);
return "/index";
}
前端界面
<!--footer:分页条-->
<div class="ui right floated pagination menu" th:if="${page.rows > 0}" style="margin-top: 2px;">
<a th:href="@{${page.path}(current=1)}" class="icon item">
<i class="paper plane icon"></i>
</a>
<a th:href="@{${page.path}(current=${page.current-1})}" th:class="|icon item ${page.current==1? 'disabled':''}|">
<i class="left chevron icon"></i>
</a>
<span th:class="|item ${i == page.current? 'active': ''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
<a th:href="@{${page.path}(current = ${i})}" th:text="${i}">1</a>
</span>
<a th:href="@{${page.path}(current=${page.current+1})}" th:class="|icon item ${page.current==page.total? 'disabled':''}|">
<i class="right chevron icon"></i>
</a>
<a th:href="@{${page.path}(current=${page.total})}" class="icon item">
<i class="paper plane outline icon"></i>
</a>
</div>
domain
/**
* Created by Monologue_zsj Luna on 2020/11/12 16:48
* Description:封装分页相关的组件
*/
@NoArgsConstructor
@AllArgsConstructor
public class Page {
//当前页码
private Integer current = 1;
//显示上限
private Integer limit = 10;
//数据总数(用于计算总页数)
private Integer rows;
//查询路径(用于复用分页的连接)
private String path;
public Integer getCurrent() {
return current;
}
public void setCurrent(Integer current) {
if (current >= 1){
this.current = current;
}
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
if (limit >= 1 && limit <= 100) {
this.limit = limit;
}
}
public Integer getRows() {
return rows;
}
public void setRows(Integer 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() {
if (rows % limit == 0) {
return rows / limit;
}else {
return rows / limit + 1;
}
}
/**
* 获取起始页码
* @return
*/
public int getFrom() {
int from = current -2;
return from < 1 ? 1 : from;
}
/**
* 获取终止页码
* @return
*/
public int getTo() {
int to = current + 2;
int total = getTotal();
return to > total ? total : to;
}
@Override
public String toString() {
return "Page{" +
"current=" + current +
", limit=" + limit +
", rows=" + rows +
", path='" + path + '\'' +
'}';
}
}