oracle+mybatis-plus+springboot實現分頁查詢的實例

今天蠢瞭一上午才弄出這玩意,話不多說上代碼!

1、建一個配置類

package com.sie.demo.config;
 
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @author liaozhifeng
 * @date 2021/7/31 11:14
 * @Version 1.0
 */
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
 
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        數據庫的配置DbType.ORACLE 根據自己的數據庫選擇
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
        return interceptor;
    }
}

2、service層的代碼

package com.sie.demo.service.impl; 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sie.demo.entity.Users;
import com.sie.demo.entity.vo.PageUserVo;
import com.sie.demo.mapper.UserMapper;
import com.sie.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
 
import javax.annotation.Resource;
 
import javax.servlet.http.HttpSession;
import java.util.List;
 
/**
 * @author liaozhifeng
 * @date 2021/7/27 14:03
 * @Version 1.0
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, Users> implements UserService {
    @Resource
    UserMapper userMapper;
 
    
    /**
     * 分頁查詢用戶
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageUserVo selectAll(long currentPage, long pageSize) {
        IPage<Users> userPage = new Page<>(currentPage, pageSize);//參數一是當前頁,參數二是每頁個數
        IPage<Users> iPage = userMapper.selectPage(userPage, null);
        List<Users> list = iPage.getRecords();
        Long total = iPage.getTotal();
        PageUserVo pageUserVo = new PageUserVo(list, total);
        return pageUserVo;
    }
}

3、controller層 RetResult是我自己定義的一個統一返回的泛型對象

package com.sie.demo.controller; 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sie.demo.entity.RetCode;
import com.sie.demo.entity.RetResponse;
import com.sie.demo.entity.RetResult;
import com.sie.demo.entity.Users;
 
import com.sie.demo.entity.vo.PageUserVo;
import com.sie.demo.service.impl.UserServiceImpl;
import com.sie.demo.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
 
 
@Controller
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    UserServiceImpl userService;
 
  
    /**
     *
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("selectAll")
    public RetResult<PageUserVo> selectAll(long currentPage,  long pageSize) {
        log.info(currentPage+pageSize+"=========================");
        PageUserVo pageUserVo  = userService.selectAll(currentPage,pageSize);
        return RetResponse.makeOKRsp(pageUserVo);
    }
}

到此這篇關於oracle+mybatis-plus+springboot實現分頁查詢的實例的文章就介紹到這瞭,更多相關 mybatis-plus springboot分頁查詢內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: