Spring分页功能

导读:本篇文章讲解 Spring分页功能,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、创建一个Page类,封装分页相关的信息

/**
 * 封装分页的相关信息:
 * 当前页码、显示上限、数据总数、查询路径
 * 获取当前页起始行、获取总页数、获取起始页码、获取结束页码
 */
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(){
        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;
        return to > getTotal() ? getTotal() : to;
    }
}

二、逐层实现分页功能

2.1 在数据层(Mapper)写分页查询的方法,并实现

@Mapper
public interface DiscussPostMapper {

    List<DiscussPost> selectDiscussPosts(int userId,int offset,int limit);

}

2.2 在业务层(Service)实现分页的操作

@Service
public class DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;

    public List<DiscussPost> findDiscussPosts(int userId,int offset,int limit){
        return discussPostMapper.selectDiscussPosts(userId,offset,limit);
    }
}

2.3 在视图层(Controller)中把Page对象作为参数传入方法中

@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());
        List<Map<String,Object>> discussPosts = new LinkedList<>();
        if(list != null){
            for (DiscussPost post : list) {
                User user = userService.findUserById(post.getUserId());
                Map<String,Object> map = new HashMap<>();
                map.put("post",post);
                map.put("user",user);
                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts",discussPosts);
        return "/index";
    }

三、在html页面实现动态分页功能

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<!-- 分页 -->
<nav class="mt-5" th:if="${page.rows>0}">
    <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 ${page.current==i?'active':''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
		    <a class="page-link" th:href="@{${page.path}(current=${i})}" 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>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/2178.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!