【springboot+html实现分页效果】以图书列表为例

数据库

INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (1, '活着', '余华', 29, 22.00, '北京文艺出版社', 1, '2025-03-19 19:16:16', '2025-03-19 19:16:16');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (2, '平凡的世界', '路遥', 5, 98.56, '北京十月文艺出版社', 1, '2025-03-19 19:16:16', '2025-03-19 19:16:16');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (3, '三体', '刘慈欣', 9, 102.67, '重庆出版社', 1, '2025-03-19 19:16:16', '2025-03-19 19:16:16');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (4, '金字塔原理', '麦肯锡', 16, 178.00, '民主与建设出版社', 1, '2025-03-19 19:16:16', '2025-03-19 19:16:16');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (5, 'TIM', 'Mans', 1, 188.90, '瑞典王室出版社', 1, '2025-03-19 21:41:37', '2025-03-19 21:41:37');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (6, 'The Great Gatsby', 'F. Scott Fitzgerald', 5, 10.99, 'Scribner', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (7, 'To Kill a Mockingbird', 'Harper Lee', 3, 8.99, 'J.B. Lippincott & Co.', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (8, '1984', 'George Orwell', 7, 7.99, 'Secker & Warburg', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (9, 'Pride and Prejudice', 'Jane Austen', 4, 6.99, 'T. Egerton, Whitehall', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (10, 'The Catcher in the Rye', 'J.D. Salinger', 2, 9.99, 'Little, Brown and Company', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (11, 'Brave New World', 'Aldous Huxley', 6, 12.99, 'Chatto & Windus', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (12, 'Moby Dick', 'Herman Melville', 8, 11.99, 'Harper & Brothers', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (13, 'War and Peace', 'Leo Tolstoy', 3, 15.99, 'The Russian Messenger', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (14, 'Crime and Punishment', 'Fyodor Dostoevsky', 4, 13.99, 'The Russian Messenger', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (15, 'The Hobbit', 'J.R.R. Tolkien', 5, 14.99, 'Allen & Unwin', 1, '2025-03-19 21:46:25', '2025-03-19 21:46:25');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (16, 'The Lord of the Rings', 'J.R.R. Tolkien', 5, 20.99, 'Allen & Unwin', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (17, 'Animal Farm', 'George Orwell', 6, 7.49, 'Secker & Warburg', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (18, 'Fahrenheit 451', 'Ray Bradbury', 4, 8.99, 'Ballantine Books', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (19, 'The Catcher in the Rye', 'J.D. Salinger', 3, 9.99, 'Little, Brown and Company', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (20, 'The Adventures of Sherlock Holmes', 'Arthur Conan Doyle', 7, 6.99, 'George Newnes Ltd.', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (21, 'The Picture of Dorian Gray', 'Oscar Wilde', 2, 9.49, 'Ward, Lock and Company', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (22, 'Jane Eyre', 'Charlotte Brontë', 4, 7.99, 'Smith, Elder & Co.', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (23, 'Wuthering Heights', 'Emily Brontë', 3, 8.49, 'Thomas Cautley Newby', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (24, 'Great Expectations', 'Charles Dickens', 5, 9.99, 'Chapman & Hall', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (25, 'The Odyssey', 'Homer', 4, 12.99, 'Various', 1, '2025-03-19 21:49:08', '2025-03-19 21:49:08');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (26, 'The Divine Comedy', 'Dante Alighieri', 3, 14.99, 'Bonagiunta Urbicciani', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (27, 'Les Misérables', 'Victor Hugo', 5, 16.99, 'Pagnerre', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (28, 'The Brothers Karamazov', 'Fyodor Dostoevsky', 4, 13.99, 'The Russian Messenger', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (29, 'One Hundred Years of Solitude', 'Gabriel García Márquez', 3, 12.99, 'Editorial Sudamericana', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (30, 'The Grapes of Wrath', 'John Steinbeck', 6, 10.99, 'Viking Press', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (31, 'The Road', 'Cormac McCarthy', 2, 9.99, 'Knopf', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (32, 'Beloved', 'Toni Morrison', 4, 11.99, 'Alfred A. Knopf', 1, '2025-03-19 21:52:30', '2025-03-19 21:52:30');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (33, 'The Bell Jar', 'Sylvia Plath', 3, 8.99, 'Heinemann', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (34, 'The Stranger', 'Albert Camus', 5, 7.99, 'Gallimard', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (35, 'The Trial', 'Franz Kafka', 4, 10.99, 'Verlag Die Schmiede', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (36, 'The Metamorphosis', 'Franz Kafka', 3, 8.49, 'Kurt Wolff Verlag', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (37, 'Don Quixote', 'Miguel de Cervantes', 6, 12.99, 'Francisco de Robles', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (38, 'War and Peace', 'Leo Tolstoy', 5, 15.99, 'The Russian Messenger', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (39, 'Anna Karenina', 'Leo Tolstoy', 4, 14.99, 'The Russian Messenger', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (40, 'Madame Bovary', 'Gustave Flaubert', 3, 11.99, 'Michel Lévy Frères', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (41, 'The Idiot', 'Fyodor Dostoevsky', 4, 13.99, 'The Russian Messenger', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (42, 'The Unbearable Lightness of Being', 'Milan Kundera', 2, 10.99, 'Éditions Gallimard', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (43, 'The Name of the Rose', 'Umberto Eco', 5, 12.99, 'Bompiani', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (44, 'In Search of Lost Time', 'Marcel Proust', 3, 19.99, 'Grasset', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (45, 'The Magic Mountain', 'Thomas Mann', 4, 14.99, 'S. Fischer Verlag', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (46, 'Lolita', 'Vladimir Nabokov', 3, 11.99, 'Olympia Press', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (47, 'The Sound and the Fury', 'William Faulkner', 5, 10.99, 'Jonathan Cape and Harrison Smith', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (48, 'To the Lighthouse', 'Virginia Woolf', 4, 9.99, 'Hogarth Press', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (49, 'A Portrait of the Artist as a Young Man', 'James Joyce', 3, 8.99, 'B. W. Huebsch', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (50, 'The Sun Also Rises', 'Ernest Hemingway', 6, 11.99, 'Scribner', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (51, 'For Whom the Bell Tolls', 'Ernest Hemingway', 5, 12.99, 'Scribner', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (52, 'The Old Man and the Sea', 'Ernest Hemingway', 4, 9.99, 'Charles Scribner\'s Sons', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (53, 'The Catcher in the Rye', 'J.D. Salinger', 3, 9.99, 'Little, Brown and Company', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (54, 'Brave New World', 'Aldous Huxley', 6, 12.99, 'Chatto & Windus', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');
INSERT INTO `book_info` (`id`, `book_name`, `author`, `count`, `price`, `publish`, `status`, `create_time`, `update_time`) VALUES (55, 'The Great Gatsby', 'F. Scott Fitzgerald', 5, 10.99, 'Scribner', 1, '2025-03-19 21:52:31', '2025-03-19 21:52:31');

实体类定义

BookInfo

@Data
public class BookInfo {
    private Integer id;
    private String bookName;
    private String author;
    private Integer count;
    private BigDecimal price;
    private String publish;
    private Integer status;
    private String statusCN;
    private Date createTime;
    private Date updateTime;
}

PageRequest

@Data
public class PageRequest {
    private int currentPage = 1;
    private int pageSize = 10;
    private int offset;

    public int getOffset() {
        return (currentPage - 1) * pageSize;
    }

}

这个类是用来接收前端发来的参数,包含当前页的信息,每一页需要显示多少数据
offset是指偏移量,用于计算传给sql语句limit的第一个参数

PageResult类

@Data
public class PageResult<T> {
    private Integer total;
    private List<T> records;// 当前页的数据
    private PageRequest pageResult;

    public PageResult(Integer total, List<T> records,PageRequest pageRequest) {
        this.total = total;
        this.records = records;
        this.pageResult = pageRequest;
    }
}

这里的结果类包含了总共有多少条数据以及每一页数据的具体内容
这里还加了一个请求类是用于给前端分页代码传递当前是第几页

mapper类

BookMapper

@Mapper
public interface BookMapper {
    @Insert("insert into book_info (book_name, author, count, price, publish) VALUES (#{bookInfo.bookName},#{bookInfo.author},#{bookInfo.count},#{bookInfo.price},#{bookInfo.publish})")
    void addBook(@Param("bookInfo") BookInfo bookInfo);

    @Select("select count(1) from book_info where status != 0")
    Integer count();

    @Select("select * from book_info where status != 0 order by id asc limit #{pageRequest.offset},#{pageRequest.pageSize}")
    List<BookInfo> queryBookListByPage(@Param("pageRequest")PageRequest pageRequest);
}

服务类

BookService

@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;
    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) throws JsonProcessingException {
        Integer count = bookMapper.count();
        List<BookInfo> books = bookMapper.queryBookListByPage(pageRequest);
        for(BookInfo book : books) {
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()).getName());
        }
        PageResult<BookInfo> ret = new PageResult<>(count,books,pageRequest);
        System.out.println("返回的数据为:" + ret);
        System.out.println(ret.getClass().getName());
        return ret;
    }
}

控制类

BookController

    @RequestMapping("/getListByPage")
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest, HttpServletResponse response) throws JsonProcessingException {
        response.setContentType("application/json");
        System.out.println("[getListByPage]" + bookService.getBookListByPage(pageRequest));
        return bookService.getBookListByPage(pageRequest);
    }

前端分页组件

分页组件链接

$(document).ready(function () {
                // 解析URL中的查询参数
                var urlParams = new URLSearchParams(window.location.search);
                var currentPage = urlParams.get('currentPage');
                getBookList(currentPage); // 使用从URL获取的currentPage值
            });

            // getBookList();
            function getBookList(currentPage) {
                currentPage = currentPage || 1; // 如果未提供,则默认为第一页
                $.ajax({
                    type: 'post',
                    url: '/book/getListByPage?currentPage=' + currentPage,
                    success: function (ret) {
                        console.log(ret);
                        var finalHtml = "";
                        if (ret != null) {
                            for (var book of ret.records) {
                                console.log(book);
                                finalHtml += '<tr>';
                                finalHtml += '<td><input type="checkbox" name="selectBook" value="1" id="selectBook class="book-select"></td>';
                                finalHtml += '<td>' + book.id + '</td>';
                                finalHtml += '<td>' + book.bookName + '</td>';
                                finalHtml += '<td>' + book.author + '</td>';
                                finalHtml += '<td>' + book.count + '</td>';
                                finalHtml += '<td>' + book.price + '</td>';
                                finalHtml += '<td>' + book.publish + '</td>';
                                finalHtml += '<td>' + book.statusCN + '</td>';
                                finalHtml += '<td>';
                                finalHtml += '<div class="op">';
                                finalHtml += '<a href="book_update.html?bookId=' + book.id + '">修改</a>';
                                finalHtml += '<a href="javascript:void(0) onclick="deleteBook(' + book.id + ')">删除</a>';
                                finalHtml += '</div>';
                                finalHtml += '</td>';
                                finalHtml += '</tr>';
                            }
                        }
                        $("tbody").html(finalHtml);
                        // console.log(finalHtml);
                        //翻页信息
                        $("#pageContainer").jqPaginator({
                            totalCounts: ret.total, //总记录数
                            pageSize: 10,    //每页的个数
                            visiblePages: 5, //可视页数
                            currentPage: ret.pageResult.currentPage,  //当前页码
                            first: '<li class="page-item"><a class="page-link">首页</a></li>',
                            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页<\/a><\/li>',
                            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页<\/a><\/li>',
                            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页<\/a><\/li>',
                            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}<\/a><\/li>',
                            //页面初始化和页码点击时都会执行
                            onPageChange: function (page, type) {
                                console.log("第" + page + "页, 类型:" + type);
                                if (type != 'init') {
                                    location.href = "book_list.html?currentPage=" + page;
                                }
                            }
                        });
                    }
                });
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿梦Anmory

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值