springboot整合分页插件
1.增加分页配置
importcom.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
/**
* @Description: 分页配置
* @author woniu
* @date 2023-09-18 14:53
**/
@Configuration
@Slf4j
publicclassMybatisPlusConfig {
/**
* 分页插件
*/
@Bean
publicPaginationInterceptorpaginationInterceptor() {
finallonglimit =500L;
PaginationInterceptorpaginationInterceptor=newPaginationInterceptor();
//你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制)
paginationInterceptor.setLimit(limit);
log.info("注册分页插件,你的最大单页限制数量:"+limit);
returnpaginationInterceptor;
}
}
2.pom配置
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
3.分页工具类
importcom.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* @author woniu
* @date 2023-09-18
*/
publicclassPageUtil {
/**
* 构建page 由于表单分页是从0开始的,所有这里统一都从0开始
* @param pageNum
* @param pageSize
* @return
*/
publicstatic<T>Page<T>buildPage(IntegerpageNum,IntegerpageSize){
pageNum=pageNum==null?0:pageNum;
pageSize=pageSize==null?25:pageSize;
returnnewPage<>(pageNum,pageSize);
}
}
importlombok.AllArgsConstructor;
importlombok.Builder;
importlombok.Data;
importlombok.NoArgsConstructor;
importjava.io.Serializable;
importjava.util.ArrayList;
importjava.util.List;
/**
* 分页实体类
*
* @author woniu
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
publicclassPageResult<T>implementsSerializable {
privatestaticfinallongserialVersionUID=-275582248840137389L;
/**
* 总数
*/
privateLongcount;
/**
* 当前页结果集
*/
privateList<T>data;
publicstatic<T>PageResult<T>empty() {
returnnewPageResult<>(0L, newArrayList<>());
}
}
4.分页代码
@Service
publicclassUserServiceImplextendsServiceImpl<UserMapper, UserEntity>implementsUserService {
@Override
publicPageResult<UserRespVo>findList(UserReqVoreqVo) {
Page<UserRespVo>page=PageUtil.buildPage(reqVo.getPageNum(), reqVo.getPageSize());
List<UserRespVo>list=this.baseMapper.findList(page, reqVo);
returnPageResult.<UserRespVo>builder()
.data(list)
.count(page.getTotal())
.build();
}
publicinterfaceUserMapperextendsBaseMapper<UserEntity> {
/**
* 列表
*
* @param page
* @param reqVo
* @return
*/
List<UserRespVo>findList(Page<UserRespVo>page, @Param("vo") UserReqVoreqVo);
}
5.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.woniu.woniuuser.mapper.UserMapper">
<resultMapid="userRespVo"type="com.woniu.crm.user.resp.UserRespVo"></resultMap>
<selectid="findList"resultMap="userRespVo">
select*fromuser
<where>
anddel_flag='009'
<iftest="vo.userName != null and vo.userName != ''">
<bindname="likeUserName"value="'%' + vo.userName + '%'"/>
anduser_namelike#{likeUserName}
</if>
</where>
</select>
</mapper>