在操作书库的时候,总是不可避免的遇到分页。在此记录一下mybatis-plus分页配置
分页配置类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult implements Serializable {
private Long counts = 0l;//总记录数
private Integer pagesize = 10;//页大小
private Long pages = 0l;//总页数
private Integer page = 1;//当前页码
private List<?> items = Collections.emptyList(); //列表
public PageResult(Integer page, Integer pagesize, Long counts, List list) {
this.page = page;
this.pagesize = pagesize;
this.items = list;
this.counts = counts;
this.pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1;
}
}
YAL配置
mybatis-plus:
global-config:
db-config:
table-prefix: tb_ # 表名前缀
id-type: auto # id策略为自增长
引导类配置
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
测试调用 Mapper
public interface BlackListMapper extends BaseMapper<BlackList> {
@Select("select * from tb_user_info where id in( \n" +
"select black_user_id from tb_black_list where user_id = #{userId} \n" +
") ")
IPage<UserInfo> findBlackList(Page pages, Long userId);
}
测试调用 Server
@Override
public IPage<UserInfo> blacklist(Integer page, Integer pagesize, Long userId) {
//1、构建分页参数对象Page
Page pages = new Page(page, pagesize);
//2、调用方法分页(自定义编写 分页参数Page,sql条件参数)
return blackListMapper.findBlackList(pages, userId);
}
测试调用 Controller
public PageResult blacklist(Integer page, Integer pagesize) {
//获取用户id
Long userId = TokenThreadLocal.getUserId();
//查询用户黑名单列表
IPage<UserInfo> iPage = userInfoApi.blacklist(page, pagesize, userId);
//3、对象转化,将查询的Ipage对象的内容封装到PageResult中
PageResult pageResult = new PageResult(page, pagesize, iPage.getTotal(), iPage.getRecords());
return pageResult;
}
版权声明:本文为packge原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。