第一步:定义方法
@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>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。