TableDataInfo文章来源:https://www.toymoban.com/news/detail-664806.html
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
/**
* 表格分页数据对象
*
* @author Lion Li
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TableDataInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 总记录数
*/
private long total;
/**
* 列表数据
*/
private List<T> rows;
/**
* 消息状态码
*/
private int code;
/**
* 消息内容
*/
private String msg;
/**
* 分页
*
* @param list 列表数据
* @param total 总记录数
*/
public TableDataInfo(List<T> list, long total) {
this.rows = list;
this.total = total;
}
public static <T> TableDataInfo<T> build(IPage<T> page) {
TableDataInfo<T> rspData = new TableDataInfo<>();
rspData.setCode(HttpStatus.HTTP_OK);
rspData.setMsg("查询成功");
rspData.setRows(page.getRecords());
rspData.setTotal(page.getTotal());
return rspData;
}
public static <T> TableDataInfo<T> build(List<T> list) {
TableDataInfo<T> rspData = new TableDataInfo<>();
rspData.setCode(HttpStatus.HTTP_OK);
rspData.setMsg("查询成功");
rspData.setRows(list);
rspData.setTotal(list.size());
return rspData;
}
public static <T> TableDataInfo<T> build(List<T> list, int total) {
TableDataInfo<T> rspData = new TableDataInfo<>();
rspData.setCode(HttpStatus.HTTP_OK);
rspData.setMsg("查询成功");
rspData.setRows(list);
rspData.setTotal(total);
return rspData;
}
public static <T> TableDataInfo<T> build() {
TableDataInfo<T> rspData = new TableDataInfo<>();
rspData.setCode(HttpStatus.HTTP_OK);
rspData.setMsg("查询成功");
return rspData;
}
}
serviceImpl.selectPageQueryUserList文章来源地址https://www.toymoban.com/news/detail-664806.html
@Override
public TableDataInfo<SysUser> selectPageQueryUserList(SysUserBo bo, PageQuery pageQuery) {
if (StrUtil.isNotEmpty(bo.getKey())) {
//如果key值为数字,则按照工号查
if (bo.getKey().matches("^[0-9]+$")) {
bo.setUserName(bo.getKey());
}
//如果key值为中文,则按照姓名查
if (bo.getKey().matches("^[\\u4e00-\\u9fa5]+$")) {
bo.setNickName(bo.getKey());
}
}
//根据公司编码查询该公司所有用户
List<SysUser> firmList = baseMapper.selectUsersByDeptCode(bo);
//如果key值为字母,则按照‘姓’的拼音查,大小写均可
if (StrUtil.isNotEmpty(bo.getKey()) && bo.getKey().matches("^[a-zA-Z]+$")){
//字母一律转换为小写
String name = bo.getKey().toLowerCase(Locale.ROOT);
//存放符合搜索条件的数据
List<SysUser> charNameList = new ArrayList<>();
for (SysUser sysUser:firmList){
//定义surname代表姓氏
char surname = 0;
//挑出nickName第一个字符
surname = sysUser.getNickName().charAt(0);
//挑出第一个字符的拼音
List<Pinyin> pinyins = HanLP.convertToPinyinList(String.valueOf(surname));
for (Pinyin pinyin : pinyins) {
// String s = String.valueOf(pinyin.getShengmu());
// String s1 = String.valueOf(pinyin.getFirstChar());
//获取纯字母的拼音
String s = String.valueOf(pinyin.getPinyinWithoutTone());
//进行匹配
if (s.contains(name)) {
charNameList.add(sysUser);
}
// else {
// continue;
// }
}
}
firmList = charNameList;
}
//如果bo.deptId不为空,则在firmPage(公司内)向下查询所有子部门用户
if (bo.getDeptId() != null) {
// 根据deptId查询所有子部门id
List<Long> firmDeptIdList = baseMapper.selectFirmDeptId(bo.getDeptId());
//根据子部门id筛选firmPage中符合条件的用户
firmList = firmList.stream()
.filter(user -> firmDeptIdList.contains(user.getDeptId()))
.collect(Collectors.toList());
}
//如果bo.roleId不为空,则在firmPage(公司内)查询所有与之关联用户
if (bo.getRoleId() != null) {
//根据roleId查询关联的用户id
List<Long> userIdList = baseMapper.selectUserIdByRoleId(bo.getRoleId());
//根据用户id筛选firmPage中符合条件的用户
firmList = firmList.stream()
.filter(user -> userIdList.contains(user.getUserId()))
.collect(Collectors.toList());
}
// 创建Page对象
Page<SysUser> page = new Page<>();
if (pageQuery.getPageNum() != null && pageQuery.getPageSize() != null) {
page.setCurrent(pageQuery.getPageNum());
page.setSize(pageQuery.getPageSize());
// 手动分页,获取当前页的数据列表
int startIndex = (int) ((page.getCurrent() - 1) * page.getSize());
int endIndex = (int) Math.min(startIndex + page.getSize(), firmList.size());
List<SysUser> currentList = firmList.subList(startIndex, endIndex);
// 设置总记录数和当前页数据列表
page.setTotal(firmList.size());
page.setRecords(currentList);
} else {
page.setRecords(firmList);
page.setTotal(firmList.size());
}
return TableDataInfo.build(page);
}
到了这里,关于Mybatis-Plus手动分页的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!