Mybatis-Plus 分页插件
Mybatis-Plus 分页插件
前言
参考文档:https://mp.baomidou.com/guide/page.html
笔者使用的是SpringBoot结合Mybatis-Plus。
如果读者对SpringBoot创建项目已经熟悉,或者只想了解分页插件的内容,请直接看章节”使用Mybatis-Plus分页插件“。
工程结构
准备
首先,创建SpringBoot项目,实现从前端到后台跑通。
创建Maven工程
因为我已经创建过了,这里爆红。
修改POM文件
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
com.example
mybatis-plus-pagination
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
org.projectlombok
lombok
1.18.2
org.springframework.boot
spring-boot-starter-jdbc
com.baomidou
mybatis-plus-boot-starter
3.1.2
mysql
mysql-connector-java
5.1.47
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-maven-plugin
编写yml文件
在 resource文件夹下创建application.yml
# 端口
server:
port: 8080
# 数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springbootdemo?characterEncoding=utf-8&useSSL=false
username: root
password: root
# mybatis-plus设置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #mapper.xml文件位置,如果没有映射文件,请注释掉。
注:这里的数据库名称、用户名和密码记得改成自己的。
useSSL=false,是为了去掉一个检测,不至于控制台报红,不影响运行。
启动类
创建Application.java
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@MapperScan 用来扫描Mapper文件
业务类
这部分内容包扩:实体类、Dao、Service、Controller
实体类
User.java
import java.io.Serializable;
@Data
@TableName("user")
public class User implements Serializable {
@TableId(type = IdType.AUTO, value = "userId") //自增
private Long userId;
@TableField(value = "userName")
private String userName;
@TableField(value = "password")
private String password;
@TableField(value = "status")
private int status;
}
@Data为lombok注解
@TableName 用来映射数据表名称
@TableId 用来映射主键,value属性用来映射字段名称
如果不指定,mybatis-plus会根据属性名自动映射,如“userId”映射为“user_id"。
Result.java 统一的前端返回类型
package com.example.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result implements Serializable {
private boolean success;
private String message;
private Object data;
public Result(boolean success, String message) {
this.success = success;
this.message = message;
}
}
@AllArgsConstructor 全参构造
@NoArgsConstructor 无参构造,它们都是lombok的注解
Dao层
UserMapper.java
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entities.User;
public interface UserMapper extends BaseMapper {
}
在之前的yml编写中有这么一句:
# mybatis-plus设置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml #mapper.xml文件位置,如果没有映射文件,请注释掉。
接下来,我们创建映射的xml文件
在resource文件夹下,创建mapper文件夹,再创建UserMapper.xml文件,内容如下
<?xml version="1.0" encoding="UTF-8" ?>
Service层
首先,创建UserService接口。
package com.example.service;
import com.example.entities.User;
import java.util.List;
public interface UserService {
List findList();
}
再创建它的实现类UserServiceImpl
package com.example.service.impl;
import com.example.entities.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List findList() {
return userMapper.selectList(null);
}
}
Controller层
创建UserController.java
package com.example.controller;
import com.example.entities.Result;
import com.example.entities.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("user/findList")
public Result findList() {
List list = userService.findList();
if (list != null && list.size() > 0) {
return new Result(true, "查询成功", list);
} else {
return new Result(false, "查询失败");
}
}
}
测试
运行启动类>Application.java
访问:http://localhost:8080/user/findList
访问成功!
使用Mybatis-Plus分页插件
在pom文件中,导入所需坐标:(这一步,准备中已经做过)
com.baomidou
mybatis-plus-boot-starter
3.1.2
第一步:编写分页插件配置类
创建MyBatisPlusConfig配置类,内容如下:
package com.example.config;
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;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan("com.example.mapper")
public class MyBatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制
//paginationInterceptor.setLimit(2);
return paginationInterceptor;
}
}
第二步:编写Dao层代码
修改UserMapper.java ,内容如下:
public interface UserMapper extends BaseMapper {
IPage selectPage(Page page);
}
编写对应的xml映射文件UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
编写一个普通 list 查询,mybatis-plus 自动替你分页
加不加where取决于你是否携带查询条件
第三步:调用分页方法
修改UserService接口:
public interface UserService {
List findList();
IPage selectPage(Page page);
}
修改UserServiceImpl实现类:
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List findList() {
return userMapper.selectList(null);
}
@Override
public IPage selectPage(Page page) {
return userMapper.selectPage(page);
}
}
第四步:编写Controller
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("user/findList")
public Result findList() {
List list = userService.findList();
if (list != null && list.size() > 0) {
return new Result(true, "查询成功", list);
} else {
return new Result(false, "查询失败");
}
}
@GetMapping("user/selectPage")
public IPage selectPage() {
/**
* Page(current,size)
* current:当前页,long类型
* size:每页显示的数量,long类型
* 可参考其构造方法
*/
IPage userIPage = userService.selectPage(new Page<>(1, 5));
return userIPage;
}
}
测试
运行启动类>Application.java
访问:http://localhost:8080/user/findList
使用浏览器访问:
使用postman工具访问:
可以看到,分页插件自动帮助我们实现了分页功能。