共计 4930 个字符,预计需要花费 13 分钟才能阅读完成。
分页在企业级零碎必不可少,特地是早年开发人员,在 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;
}
}
五、测试
效果图如下:
数据刚好十条,并且有总条数。
六、总结
本次例子用的是本人写的 sql 并联合 MyBatis-Plus 分页插件来实现分页的,实用场景比拟广,通常是多表关联的查问语句等。如果是单表的话,间接用 MyBatis-Plus 单表自带的分页 API 即可。
正文完