MyBatis-Plus实现2种分页方法(QueryWrapper查询分页和SQL查询分页)

目录

  • 1 MyBatisPlusConfig
  • 2 UserPagination
  • 3 Mapper
    • 3.1 UserMapper.java
    • 3.2 UserMapper.xml
  • 4 Service
    • 4.1 UserService
    • 4.2 UserServiceImpl
  • 5 UserController
    • 6 调试结果
      • 6.1 QueryWrapper查询分页
      • 6.2 SQL查询分页

    1 MyBatisPlusConfig MyBatisPlus配置类。
    package com.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.*; /** * MyBatisPlus配置类 */@Configurationpublic class MyBatisPlusConfig { /*** MyBatisPlus拦截器(用于分页)*/@Beanpublic MybatisPlusInterceptor paginationInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //添加MySQL的分页拦截器interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; }}


    2 UserPagination 用户查询条件类。
    package com.entity; import lombok.Data; /** * 查询条件 */@Datapublic class UserPagination {/*** 当前页号*/private int currentPage; /*** 每页显示条数*/private int pageSize; }


    3 Mapper
    3.1 UserMapper.java
    package com.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.entity.UserEntity; import com.entity.UserPagination; import org.apache.ibatis.annotations.Mapper; /** * 用户信息dao层 */@Mapperpublic interface UserMapper extends BaseMapper {/*** 获取用户信息(SQL查询分页)** @param page 分页条件* @return*/Page getUserListBySQLPage(Page page); }


    3.2 UserMapper.xml
    SELECT *from users


    4 Service
    4.1 UserService
    package com.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.entity.*; public interface UserService extends IService {/*** 获取用户信息(QueryWrapper查询分页)** @param pagination 查询条件* @return*/Page getUserListByQueryWrapperPage(UserPagination pagination); /*** 获取用户信息(SQL查询分页)** @param pagination 查询条件* @return*/Page getUserListBySQLPage(UserPagination pagination); }


    4.2 UserServiceImpl
    package com.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.entity.*; import com.mapper.UserMapper; import com.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Servicepublic class UserServiceImpl extends ServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper; /*** 获取用户信息(QueryWrapper查询分页)** @param pagination 查询条件* @return*/public Page getUserListByQueryWrapperPage(UserPagination pagination) {QueryWrapper queryWrapper = new QueryWrapper<>(); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); return this.page(page, queryWrapper); } /*** 获取用户信息(SQL查询分页)** @param pagination 查询条件* @return*/@Overridepublic Page getUserListBySQLPage(UserPagination pagination) {Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); return userMapper.getUserListBySQLPage(page); }}


    5 UserController 调试代码。
    package com.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.entity.*; import com.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestControllerpublic class UserController {@Autowiredprivate UserService userService; /*** 获取用户信息(QueryWrapper查询分页)** @return*/@GetMapping("/getUserListByQueryWrapperPage")public Page getUserListByQueryWrapperPage(UserPagination pagination) {return userService.getUserListByQueryWrapperPage(pagination); } /*** 获取用户信息(SQL查询分页)** @return*/@GetMapping("/getUserListBySQLPage")public Page getUserListBySQLPage(UserPagination pagination) {return userService.getUserListBySQLPage(pagination); } }


    6 调试结果
    6.1 QueryWrapper查询分页
    MyBatis-Plus实现2种分页方法(QueryWrapper查询分页和SQL查询分页)
    文章图片


    6.2 SQL查询分页
    MyBatis-Plus实现2种分页方法(QueryWrapper查询分页和SQL查询分页)
    文章图片

    注:
    更多MyBatis-Plus的配置请查看以下博客。
    Spring Boot 配置MyBatis-Plus(实现查询、新增、更新、删除)
    【MyBatis-Plus实现2种分页方法(QueryWrapper查询分页和SQL查询分页)】到此这篇关于MyBatis-Plus实现2种分页方法(QueryWrapper查询分页和SQL查询分页)的文章就介绍到这了,更多相关MyBatis-Plus 分页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

      推荐阅读