Java教程

MyBatis-Plus之分页插件使用

本文主要是介绍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;
/**
 * <p>
 * Mapper 接口 * </p>
 * * @author youcong
 * @since 2020-04-18
 */@Repository
public interface UsersDao extends BaseMapper<Users> {
    IPage<Users> selectPageVo(Page<Users> page);
}
<?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.blog.tutorial.dao.UsersDao">
    <!-- 通用查询映射结果 -->
 <resultMap id="BaseResultMap" type="com.blog.tutorial.entity.Users">
        <id column="ID" property="id"/>
        <result column="user_login" property="userLogin"/>
        <result column="user_pass" property="userPass"/>
        <result column="user_nicename" property="userNicename"/>
        <result column="user_email" property="userEmail"/>
        <result column="user_url" property="userUrl"/>
        <result column="user_registered" property="userRegistered"/>
        <result column="user_activation_key" property="userActivationKey"/>
        <result column="user_status" property="userStatus"/>
        <result column="display_name" property="displayName"/>
    </resultMap>
    <!-- 通用查询结果列 -->
 <sql id="Base_Column_List">
        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
    </sql>
    <!-- 查询用户列表 -->
 <select id="selectPageVo" resultMap="BaseResultMap">
        select
        ID,user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name
        from
        wp_users
    </select>
</mapper>

三、编写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<Users> {
    IPage<Users> selectUserPage(Page<Users> 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<UsersDao, Users> implements UsersService {
    @Autowired
 private UsersDao usersDao;
    @Override
 public IPage<Users> selectUserPage(Page<Users> 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<String, Object> returnMap = new HashMap<>();
        Page<Users> page = new Page<>(pageNum, pageSize);
        IPage<Users> data = usersService.selectUserPage(page);
        returnMap.put("count", data.getTotal());
        returnMap.put("data", data.getRecords());
        return returnMap;
    }
}

五、测试

效果图如下:
image.png

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

六、总结

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

这篇关于MyBatis-Plus之分页插件使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!