Springboot工程中
mybatis-plus分页配置
@EnableTransactionManagement
@Configuration
@MapperScan("com.cy.springboot.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
一、定义一个接口数据返回类
自定义异常值 Constants
public interface Constants {
String CODE_200 = "200";
String CODE_401 = "401";
String CODE_400 = "400";
String CODE_500 = "500";
String CODE_600 = "600";
}
接口数据返回类 Result.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private String code;
private String msg;
private Object data;
public static Result success() {
return new Result(Constants.CODE_200, "", null);
}
public static Result success(Object data) {
return new Result(Constants.CODE_200, "", data);
}
public static Result error(String code, String msg) {
return new Result(code, msg, null);
}
public static Result error() {
return new Result(Constants.CODE_500, "系统错误", null);
}
}
二、编写控制层接口
UserController.java
方式一
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/page")
public Result findPage() {
Page<User> userPage = new Page<>();
userPage.setCurrent(1);
userPage.setSize(1);
Page<User> page = userService.page(userPage);
return Result.success(page);
}
接口测试 打印sql
方式二
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/page")
public Result findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
return Result.success(userService.page(new Page<>(pageNum, pageSize), queryWrapper));
}
}
接口测试, http://localhost:8081/user/page?pageNum=1&pageSize=1 query中需要指定pageNum和pageSize(当前页,每页数据条数) 打印sql
|