欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot 结合 mybatis-plus使用layui分页

程序员文章站 2022-07-15 10:14:17
...
<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>${mybatis-plus.version}</version>
		</dependency>

配置

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

 

使用

public Map<String, Object> pagination(Long currentPage, Long limit, String title, Long categoryId) {
        QueryWrapper<Article> query = new QueryWrapper<>();
        if (null != title && "".equals(title)) {
            query.like("title", title);
        }
        if (null != categoryId && categoryId > 0) {
            query.eq("category_id", categoryId);
        }
        query.orderByDesc("id");
        Page<Article> page = new Page<Article>(currentPage, limit);
        Page<Article> pageObj = this.baseMapper.selectPage(page, query);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("count", pageObj.getTotal());
        map.put("data", pageObj.getRecords());
        return map;
    }

 

返回

public JsonResponse list(@RequestParam Long page, @RequestParam Long limit, @RequestParam(required = false) String title, @RequestParam(required = false) Long categoryId)
    {
        Map<String, Object> list = articleService.pagination(page, limit, title, categoryId);
        return new JsonResponse().success().pagedata(list.get("count"), list.get("data"));
    }