SpringBoot整合Mybatis-Plus分页失效,Mybatis-Plus 3.4.1分页插件失效踩坑

  1. Mybatis-Plus官网测试用例
//Spring boot方式
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
    
    

    @Bean
    public PaginationInterceptor paginationInterceptor() {
    
    
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

在这里插入图片描述
2. 调整修改后的代码 MyBatisPlusConfig.java

/**
 * @FileName: MyBatisPlusConfig
 * @Author Steven
 * @Date: 2020/12/29
 */
@EnableTransactionManagement
@MapperScan("com.macro.cloud.mapper")
@Configuration
public class MyBatisPlusConfig {
    
    

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor innerInterceptor=new PaginationInnerInterceptor();
        innerInterceptor.setDbType(DbType.MYSQL);
        innerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(innerInterceptor);
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
    
    
        return mybatisConfiguration -> mybatisConfiguration.setUseGeneratedShortKey(false);
    }
}
  1. 实体类 User.java
/**
 * @FileName: User
 * @Author Steven
 * @Date: 2020/12/28
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    

    /**
     * 对应数据库中的主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;

    /**
     * 字段添加填充内容
     */
    @TableField(fill= FieldFill.INSERT)
    private Date createTime;


    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 乐观锁注解@Version
     */
    @Version
    private Integer version;

    @TableLogic(value = "0",delval = "1")
    @TableField(fill=FieldFill.INSERT)
    private Integer deleteFlag;
}

3.映射类:UserMapper.java

public interface UserMapper extends BaseMapper<User>{
    
    
}
  1. UserController.java
RequestMapping("user")
@Controller
@Api(tags = "用户管理")
public class UserController {
    
    

    @Autowired
    UserMapper userMapper;
    /**
     *
     */
    @ApiOperation("分页查询")
    @ResponseBody
    @GetMapping("/getUsers")
    public Page<User> getUsers(@RequestParam int current,@RequestParam int size){
    
    
        Page<User> page=new Page<>(current,size);
        Page<User> userPage = userMapper.selectPage(page, null);
        return userPage;
    }
 }

测试结果
{
“records”: [
{
“id”: 1,
“name”: “Jone”,
“age”: 18,
“email”: “[email protected]”,
“createTime”: “2020-12-29T23:50:05.000+00:00”,
“updateTime”: “2020-12-29T23:50:05.000+00:00”,
“version”: 0,
“deleteFlag”: 0
},
{
“id”: 2,
“name”: “Jack”,
“age”: 20,
“email”: “[email protected]”,
“createTime”: “2020-12-29T23:50:05.000+00:00”,
“updateTime”: “2020-12-29T23:50:05.000+00:00”,
“version”: 0,
“deleteFlag”: 0
},
{
“id”: 3,
“name”: “Tom”,
“age”: 28,
“email”: “[email protected]”,
“createTime”: “2020-12-29T23:50:05.000+00:00”,
“updateTime”: “2020-12-29T23:50:05.000+00:00”,
“version”: 0,
“deleteFlag”: 0
}
],
“total”: 9,
“size”: 3,
“current”: 1,
“orders”: [],
“optimizeCountSql”: true,
“hitCount”: false,
“countId”: null,
“maxLimit”: null,
“searchCount”: true,
“pages”: 3
}

猜你喜欢

转载自blog.csdn.net/SuperstarSteven/article/details/112059336