Spring Boot 笔记 018 创建接口_文章列表(分页)

1.1 分页查询

1.1.1 创建pageBean封装分页的数据

Spring Boot 笔记 018 创建接口_文章列表(分页)_第1张图片

package com.geji.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

//分页返回结果对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
    private Long total;//总条数
    private List items;//当前页数据集合
}

1.1.2 引入pagehelper坐标

Spring Boot 笔记 018 创建接口_文章列表(分页)_第2张图片

    
      com.github.pagehelper
      pagehelper-spring-boot-starter
      1.4.6
    

1.2 Controller

package com.geji.controller;

import com.geji.pojo.Article;
import com.geji.pojo.PageBean;
import com.geji.pojo.Result;
import com.geji.service.ArticleService;
import com.geji.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId, //不是必须得参数
            @RequestParam(required = false) String state
    ) {
        PageBean
pb = articleService.list(pageNum,pageSize,categoryId,state); return Result.success(pb); } }

1.3 Service

package com.geji.service;

import com.geji.pojo.Article;
import com.geji.pojo.PageBean;

public interface ArticleService {
    void add(Article article);

    PageBean
list(Integer pageNum, Integer pageSize, Integer categoryId, String state); }

1.4 ServiceImpl

package com.geji.service.impl;

import com.geji.mapper.ArticleMapper;
import com.geji.pojo.Article;
import com.geji.pojo.PageBean;
import com.geji.service.ArticleService;
import com.geji.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        //补充属性值
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());

        Map map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);

        articleMapper.add(article);
    }

    @Override
    public PageBean
list(Integer pageNum, Integer pageSize, Integer categoryId, String state) { //1.创建PageBean对象 PageBean
pb = new PageBean<>(); //2.开启分页查询 PageHelper PageHelper.startPage(pageNum,pageSize); //3.调用mapper Map map = ThreadLocalUtil.get(); Integer userId = (Integer) map.get("id"); List
as = articleMapper.list(userId,categoryId,state); //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据 Page
p = (Page
) as; //把数据填充到PageBean对象中 pb.setTotal(p.getTotal()); pb.setItems(p.getResult()); return pb; } }

1.5 Mapper中写动态sql比较麻烦,使用映射配置文件,注意xml路径要保持一致

Spring Boot 笔记 018 创建接口_文章列表(分页)_第3张图片




    
    

1.6 Postman测试

Spring Boot 笔记 018 创建接口_文章列表(分页)_第4张图片

你可能感兴趣的:(Spring,Boot,spring,boot,笔记,后端)