[Blog for front-end and back-end separation] Study notes 02 --- paging plug-in to realize paging function

Enable pagination plugin

/**
 * MybatisPlus配置
 *
 * @author DarkClouds
 * @date 2023/05/09
 */
@Configuration
@MapperScan("com.wuyun.mapper")
public class MyBatisPlusConfig {

    //开启分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
    
}

   

 

autofill

/**
 * MyBatis Plus自动填充
 *
 * @author DarkClouds
 * @date 2023/05/09
 */
@Log4j2
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("新增:自动填充createTime:" + LocalDateTime.now(ZoneId.of(SHANGHAI.getZone())));
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now(ZoneId.of(SHANGHAI.getZone())));
    }

    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("更新:自动填充updateTime:" + LocalDateTime.now(ZoneId.of(SHANGHAI.getZone())));
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now(ZoneId.of(SHANGHAI.getZone())));
    }
}

  

  

Configure pagination interceptor

Description: The interceptor intercepts all requests to determine whether the current request has a paging requirement. If the page number is obtained, it directly calls the paging tool class PageUtils to write the paging information. When operating the database, it directly calls PageUtils to obtain the page number and the number of entries for paging. Inquire

/**
 * 分页拦截器
 *
 * @author DarkClouds
 * @date 2023/05/10
 */
public class PageableInterceptor implements HandlerInterceptor {

    /**
     * 在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理
     *
     * @param request  请求
     * @param response 响应
     * @param handler  处理程序
     * @return boolean
     */
    @Override
    public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) {
        //获取当前请求的页码
        String currentPage = request.getParameter(CURRENT);
        ///获取当前请求的分页条数
        String pageSize = Optional.ofNullable(request.getParameter(SIZE)).orElse(DEFAULT_SIZE);
        //如果获取到了页码,则开启分页线程
        if (StringUtils.hasText(currentPage)) {
            //开启线程
            PageUtils.setCurrentPage(new Page<>(Long.parseLong(currentPage), Long.parseLong(pageSize)));
        }
        return true;
    }


    /**
     * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面)
     *
     * @param request  请求
     * @param response 响应
     * @param handler  处理程序
     * @param ex       前女友
     */
    @Override
    public void afterCompletion(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler, Exception ex) {
        //调用完成销毁此线程
        PageUtils.remove();
    }

}

 

 

Paging tools

/**
 * 分页工具类
 * 类用来提供线程内部的局部变量,不同的线程之间不会相互干扰
 * @author DarkClouds
 * @date 2023/05/10
 */
public class PageUtils {

    private static final ThreadLocal<Page<?>> PAGE_HOLDER = new ThreadLocal<>();

    public static void setCurrentPage(Page<?> page) {
        PAGE_HOLDER.set(page);
    }

    public static Page<?> getPage() {
        Page<?> page = PAGE_HOLDER.get();
        if (Objects.isNull(page)) {
            setCurrentPage(new Page<>());
        }
        return PAGE_HOLDER.get();
    }

    public static Long getCurrent() {
        return getPage().getCurrent();
    }

    public static Long getSize() {
        return getPage().getSize();
    }

    public static Long getLimit() {
        return (getCurrent() - 1) * getSize();
    }

    //销毁
    public static void remove() {
        PAGE_HOLDER.remove();
    }

}

 

 

The front end of the project has written a unified paging data interface to receive, so the paging return class is written separately to return the paging results, and the paging return results of MybatisPlus are not used

Here why write a paging return class separately instead of using new Page() to return

And write the return class separately, and manually write LIMIT to get the page number when writing SQL

Does anyone have an answer?

/**
 * 分页返回类
 *
 * @author DarkClouds
 * @date 2023/05/11
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "分页返回类")
public class PageResult<T> {

    /**
     * 分页结果
     */
    @ApiModelProperty(value = "分页结果")
    private List<T> recordList;

    /**
     * 总数
     */
    @ApiModelProperty(value = "总数", dataType = "long")
    private Long count;

}

Guess you like

Origin blog.csdn.net/a1404359447/article/details/130647605