实现分页功能
1.创建Page实体类
package com.yao.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page<T> {
private int page;//分页起始页
private int size;//每页记录数
private List<T> rows;//返回的记录集合
private long total;//总记录条数
}
2.创建查询接口
#在userMapper中添加
List<user> selectPage(@Param("currentPage")int currentPage,@Param("pageSize") int pageSize);
int getTotal();
#在userService
public Page<user> getPage(int currentPage, int pageSize, String search);
public List<user> selectPage(int currentPage, int pageSize);
int getTotal();
3.实现查询功能
#在userMapper.xml中实现
<select id="selectPage" resultMap="userResultMap">
select * from user limit #{currentPage},#{pageSize}
</select>
<select id="getTotal" resultType="int">
select count(1) from user
</select>
4.实现分页功能
//获取总共数据条数
@Override
public int getTotal() {
return userMapper.getTotal();
}
//获取分页存入Page对象
@Override
public Page<user> getPage(int currentPage, int pageSize, String Search) {
List<user> users = selectPage((currentPage-1)*pageSize, pageSize);
return new Page<user>(currentPage,pageSize,users,getTotal());
}
//获取分页数据
@Override
public List<user> selectPage(int currentPage, int pageSize) {
return userMapper.selectPage(currentPage,pageSize);
}
|