springboot整合分页插件
1.增加分页配置
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description: 分页配置
* @author woniu
* @date 2023-09-18 14:53
**/
@Configuration
@Slf4j
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
final long limit = 500L;
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制)
paginationInterceptor.setLimit(limit);
log.info("注册分页插件,你的最大单页限制数量:"+limit);
return paginationInterceptor;
}
}
2.pom配置
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
3.分页工具类
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* @author woniu
* @date 2023-09-18
*/
public class PageUtil {
/**
* 构建page 由于表单分页是从0开始的,所有这里统一都从0开始
* @param pageNum
* @param pageSize
* @return
*/
public static <T> Page<T> buildPage(Integer pageNum,Integer pageSize){
pageNum = pageNum==null?0:pageNum;
pageSize = pageSize==null?25:pageSize;
return new Page<>(pageNum,pageSize);
}
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 分页实体类
*
* @author woniu
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {
private static final long serialVersionUID = -275582248840137389L;
/**
* 总数
*/
private Long count;
/**
* 当前页结果集
*/
private List<T> data;
public static <T> PageResult<T> empty() {
return new PageResult<>(0L, new ArrayList<>());
}
}
4.分页代码
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
@Override
public PageResult<UserRespVo> findList(UserReqVo reqVo) {
Page<UserRespVo> page = PageUtil.buildPage(reqVo.getPageNum(), reqVo.getPageSize());
List<UserRespVo> list = this.baseMapper.findList(page, reqVo);
return PageResult.<UserRespVo>builder()
.data(list)
.count(page.getTotal())
.build();
}
public interface UserMapper extends BaseMapper<UserEntity> {
/**
* 列表
*
* @param page
* @param reqVo
* @return
*/
List<UserRespVo> findList(Page<UserRespVo> page, @Param("vo") UserReqVo reqVo);
}
5.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.woniu.woniuuser.mapper.UserMapper">
<resultMap id="userRespVo" type="com.woniu.crm.user.resp.UserRespVo"></resultMap>
<select id="findList" resultMap="userRespVo">
select * from user
<where>
and del_flag = '009'
<if test="vo.userName != null and vo.userName != ''">
<bind name="likeUserName" value="'%' + vo.userName + '%'"/>
and user_name like #{likeUserName}
</if>
</where>
</select>
</mapper>