MyBatis-Plus之分页插件使用

分页在企业级系统必不可少,特别是早年开发人员,在sql里写limit。后来随着技术不断更新升级,向limit我们无需手写,可以通过插件来实现,插件本质上就是limit(在查询语句后面添加limit来做分页)。

MyBatis有PageHelper做分页,MyBatis-Plus只需添加一个配置(spring)或配置类修改就能实现分页。

一、配置类(关键是分页插件)

package com.blog.tutorial.config;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.extension.incrementer.H2KeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Author: fun
 * @Description: 分页拦截器
 * @Date: 2020/9/10 10:26
 * @Version: 1.0.0
 */@Configuration
public class MybatisPlusConfig {
    /**
 * 注入主键生成器 */ @Bean
 public IKeyGenerator keyGenerator() {
        return new H2KeyGenerator();
    }
    /**
 * 分页插件 */ @Bean
 public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

二、编写DAO和XML

package com.blog.tutorial.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.tutorial.entity.Users;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * 

* Mapper 接口 *

* * @author youcong * @since 2020-04-18 */@Repository public interface UsersDao extends BaseMapper { IPage selectPageVo(Page page); }



    
 
        
        
        
        
        
        
        
        
        
        
    
    
 
        ID AS id, user_login AS userLogin, user_pass AS userPass, user_nicename AS userNicename, user_email AS userEmail, user_url AS userUrl, user_registered AS userRegistered, user_activation_key AS userActivationKey, user_status AS userStatus, display_name AS displayName
    
    
 

三、编写service及其实现类

package com.blog.tutorial.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.blog.tutorial.entity.Users;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:26
 */public interface UsersService extends IService {
    IPage selectUserPage(Page page);
}
package com.blog.tutorial.service.impl;
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.blog.tutorial.dao.UsersDao;
import com.blog.tutorial.entity.Users;
import com.blog.tutorial.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@Service
public class UsersServiceImpl extends ServiceImpl implements UsersService {
    @Autowired
 private UsersDao usersDao;
    @Override
 public IPage selectUserPage(Page page) {
        return usersDao.selectPageVo(page);
    }
}

四、编写Controller

package com.blog.tutorial.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.tutorial.entity.Users;
import com.blog.tutorial.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
 private UsersService usersService;
    @GetMapping("/list")
    public Map list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Map returnMap = new HashMap<>();
        Page page = new Page<>(pageNum, pageSize);
        IPage data = usersService.selectUserPage(page);
        returnMap.put("count", data.getTotal());
        returnMap.put("data", data.getRecords());
        return returnMap;
    }
}

五、测试

效果图如下:
MyBatis-Plus之分页插件使用_第1张图片

数据刚好十条,并且有总条数。

六、总结

本次例子用的是自己写的sql并结合MyBatis-Plus分页插件来实现分页的,适用场景比较广,通常是多表关联的查询语句等。如果是单表的话,直接用MyBatis-Plus单表自带的分页API即可。

你可能感兴趣的:(java,后端,springboot,spring)