项目结构
配置文件
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author miemie
* @since 2018-08-10
*/
@Configuration
@MapperScan("com.example.demo.fr.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.fr.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.fr.model.MyPage;
import com.example.demo.fr.model.ParamSome;
import org.apache.ibatis.annotations.Param;
/**
*
* Mapper 接口
*
*
* @author fr
* @since 2019-01-24
*/
public interface UserMapper extends BaseMapper {
/**
*
* 查询 : 根据state状态查询用户列表,分页显示
* 注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值
*
*
* @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象)
* @return 分页对象
*/
public IPage selectPageVo(Page page);
}
UserService.java 方法内容
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.fr.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*
* 服务类
*
*
* @author fr
* @since 2019-01-24
*/
public interface UserService extends IService {
public IPage selectUserPage(Page page);
}
UserServiceImpl.java 方法内容
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.fr.entity.User;
import com.example.demo.fr.mapper.UserMapper;
import com.example.demo.fr.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* 服务实现类
*
*
* @author fr
* @since 2019-01-24
*/
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public IPage selectUserPage(Page page) {
// 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题,这时候你需要自己查询 count 部分
// page.setOptimizeCountSql(false);
// 当 total 为非 0 时(默认为 0),分页插件不会进行 count 查询
// 要点!! 分页返回的对象与传入的对象是同一个
return userMapper.selectPageVo(page);
}
}
分页使用model类
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* @author miemie
* @since 2018-08-10
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class MyPage extends Page {
private static final long serialVersionUID = 5194933845448697148L;
private Integer selectInt;
private String selectStr;
public MyPage(long current, long size) {
super(current, size);
}
}
package com.example.demo.fr.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author miemie
* @since 2018-09-20
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ParamSome {
private Integer yihao;
private String erhao;
}
Controller层测试类
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.fr.entity.User;
import com.example.demo.fr.mapper.UserMapper;
import com.example.demo.fr.model.MyPage;
import com.example.demo.fr.model.ParamSome;
import com.example.demo.fr.service.UserService;
import ikidou.reflect.TypeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
*
* 前端控制器
*
*
* @author fr
* @since 2019-01-24
*/
@RestController
@RequestMapping("/fr/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
@PostMapping("/page")
public void tes1t1s11() {
System.out.println("----- baseMapper 自带分页 ------");
Page page = new Page<>(1, 5);
IPage userIPage = userMapper.selectPage(page, new QueryWrapper());
// Assert.assertSame(page, userIPage);
System.out.println("总条数 ------> " + userIPage.getTotal());
System.out.println("当前页数 ------> " + userIPage.getCurrent());
System.out.println("当前每页显示数 ------> " + userIPage.getSize());
print(userIPage.getRecords());
System.out.println("----- baseMapper 自带分页 ------");
System.out.println("json 正反序列化 begin");
String json = JSON.toJSONString(page);
Page page1 = JSON.parseObject(json, TypeBuilder.newInstance(Page.class).addTypeParam(User.class).build());
print(page1.getRecords());
System.out.println("json 正反序列化 end");
System.out.println("----- 自定义 XML 分页 ------");
MyPage myPage = new MyPage(1, 5).setSelectInt(20).setSelectStr("Jack");
ParamSome paramSome = new ParamSome(20, "Jack");
IPage userMyPage = userService.selectUserPage(myPage);
System.out.println("总条数 ------> " + userMyPage.getTotal());
System.out.println("当前页数 ------> " + userMyPage.getCurrent());
System.out.println("当前每页显示数 ------> " + userMyPage.getSize());
print(userMyPage.getRecords());
System.out.println("----- 自定义 XML 分页 ------");
}
private void print(List list) {
if (!CollectionUtils.isEmpty(list)) {
list.forEach(System.out::println);
}
}
}
xml
数据库表结构及数据
/*
Navicat MySQL Data Transfer
Source Server : 47.98.144.196
Source Server Version : 50724
Source Host : 47.98.144.196:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50724
File Encoding : 65001
Date: 2019-01-31 17:13:30
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` bigint(20) NOT NULL,
`roleName` varchar(255) DEFAULT NULL,
`roleDescribe` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '1', '1');
INSERT INTO `role` VALUES ('2', '2', '2');
INSERT INTO `role` VALUES ('3', '3', '3');
INSERT INTO `role` VALUES ('4', '4', '4');
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(15) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`tel` varchar(255) DEFAULT NULL,
`sex` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', '2', '3', '4');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(50) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) DEFAULT '' COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`role_id` bigint(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'mp', '3', '[email protected]', '1');
INSERT INTO `user` VALUES ('2', 'Jack', '20', '[email protected]', '2');
INSERT INTO `user` VALUES ('3', 'Tom', '28', '[email protected]', '3');
INSERT INTO `user` VALUES ('4', 'Sandy', '21', null, '4');
INSERT INTO `user` VALUES ('5', 'Billie', '24', '[email protected]', '1');
INSERT INTO `user` VALUES ('6', 'Jone', '18', '[email protected]', '2');
INSERT INTO `user` VALUES ('7', 'Jack', '20', '[email protected]', '3');
INSERT INTO `user` VALUES ('8', 'Tom', '28', '[email protected]', '4');
INSERT INTO `user` VALUES ('9', 'Sandy', '21', '[email protected]', '1');
INSERT INTO `user` VALUES ('10', 'Billie', '24', '[email protected]', '2');
INSERT INTO `user` VALUES ('11', 'Jone', '18', '[email protected]', '3');
INSERT INTO `user` VALUES ('12', 'Jack', '20', '[email protected]', '4');
INSERT INTO `user` VALUES ('13', 'Tom', '28', '[email protected]', '1');
INSERT INTO `user` VALUES ('14', 'Sandy', '21', '[email protected]', '2');
INSERT INTO `user` VALUES ('15', 'Billie', '24', '[email protected]', '3');
INSERT INTO `user` VALUES ('19', '小羊', '3', '[email protected]', '4');
参考:https://mp.baomidou.com/guide/page.html
项目源码:https://pan.baidu.com/s/1tNkDVpvD493TNLn1WqXlUg