乐趣区

关于springboot:Spring-Boot分页

第一步:定义方法

@PostMapping("list")
    public Object list(@RequestBody PageParam pageParam) {
        // 以后页码
        int current = (int)pageParam.getPageNum();
        // 每页条数
        int size = (int)pageParam.getPageSize();

        // 构建 分页结构器
        IPage<User> page = new Page(current, size);
        // 构建 条件结构器

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        userMapper.selectPage(page, wrapper);

        List<User> records = page.getRecords();// 当前页数据
        long total = page.getTotal();// 总条数
        long pages = page.getPages();// 总页数

        records.forEach(System.out::println);

        System.out.println("以后数据总共有:"+total);
        System.out.println("共"+pages+"页");
        System.out.println("当前页数据:"+records);

        return Result.suc(records, total);
    }

第二步:定义 UserMapper.java

package com.example.demo12.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo12.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {List<User> listAll();
}

第三步:UserMapper.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.example.demo12.mapper.UserMapper">
    <select id = "listAll" resultType="com.example.demo12.entity.User">
        select * from user
    </select>
    <select id = "userCount" resultType="java.lang.Integer">
        select count(1) from user
    </select>
    <select id = "listTest" resultType="com.example.demo12.entity.User">
        select * from user
    </select>
    <select id = "testUserCount" resultType="java.lang.Integer">
        select count(1) from user
    </select>
</mapper>
退出移动版