用分页插件实现分页查询

分页插件

<!-- 分页 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>

service层

/**
     * 分页
     * @return
     */
    PageInfo<User> queryUserList(int index);

 serviceImpl层

 public PageInfo<User> queryUserList(int index) {
        PageHelper.startPage(index,3);
        return new PageInfo<User>(userMapper.queryUser());
    }

 controller层

@RequestMapping("/pageUserList")
    @ResponseBody
    public JsonPojo pageUserList(int index){
        JsonPojo jsonPojo=new JsonPojo();
        jsonPojo.setStatu(200);
        jsonPojo.setMsg("成功");
        PageInfo<User> pageInfo=userService.queryUserList(index);
        jsonPojo.setData(pageInfo);
        return jsonPojo;
    }

 前端

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script th:src="@{/jquery.js}"></script>
    <script th:src="@{/vue.js}"></script>
    <script th:src="@{/axios.js}"></script>
</head>
<body>
<div id="div1">
    <h1>分页</h1>
    <table border="solid" width="20%">
        <tr>
            <td>id</td>
            <td>姓名</td>
            <td>年龄</td>
        </tr>
        <tr v-for="user in pageInfo.list">
            <td>{{user.id}}</td>
            <td>{{user.name}}</td>
            <td>{{user.pwd}}</td>
        </tr>
    </table>
    {{pageInfo.pageNam}}  {{pageInfo.pages}} {{pageInfo.isLastPage}}
    <a href="#" @click="showPageUser(-1)">上一页</a>
    <a href="#" @click="showPageUser(1)">下一页</a>

    <br>
</div>
<script>
    var vm=new Vue({
        el: "#div1",
        data: {
            pageInfo: {},
            index: 1,
        },
        methods:{
            showPageUser(number) {
                this.index=this.index+number;
                if(this.index >= this.pageInfo.pages){
                    this.index = this.pageInfo.pages;
                }
                if(this.index<= 0){
                    this.index=1;
                }
                axios.get("/tan/a/pageUserList?index="+this.index)
                    .then(response => {
                        if (response.data.statu == 200) {
                            this.pageInfo = response.data.data;
                        }
                    }).catch(function (error) {
                    alert(error)
                });
            },
        mounted: function () {
            this.showPageUser(0);
        }
    })
</script>
</body>
</html>

工具类

package com.example.pojo;

public class JsonPojo<T> {

    private int  statu;
    private String  msg;
    private T data;


    public int getStatu() {
        return statu;
    }

    public void setStatu(int statu) {
        this.statu = statu;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

假设你使用的是 Java Spring Boot 框架,可以使用 MyBatis 分页插件进行分页查询。 首先,在 pom.xml 中添加 MyBatis 分页插件的依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 然后,在需要进行分页查询的方法中,调用 PageHelper.startPage() 方法开启分页功能,并传入当前页码和每页显示的数据条数,如下所示: ```java import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public PageInfo<User> getUsers(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> users = userMapper.getUsers(); return new PageInfo<>(users); } } ``` 在上面的代码中,我们调用了 PageHelper.startPage() 方法开启分页功能,并传入了当前页码和每页显示的数据条数。然后,我们调用 UserMapper 接口中的 getUsers() 方法来获取所有用户数据,并将其封装成 PageInfo 对象返回。 最后,我们可以在控制器中调用 getUsers() 方法来获取分页数据,并将其返回给前端页面显示,如下所示: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public PageInfo<User> getUsers(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { return userService.getUsers(pageNum, pageSize); } } ``` 在上面的代码中,我们使用 @GetMapping 注解来处理 GET 请求,并通过 @RequestParam 注解来获取请求参数中的当前页码和每页显示的数据条数。然后,我们调用 UserService 接口中的 getUsers() 方法来获取分页数据,并将其返回给前端页面显示。 至此,我们就完成了后端使用分页插件实现分页查询的代码编写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值