1.1 分页查询
1.1.1 创建pageBean封装分页的数据
package com.geji.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;//分页返回结果对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean <T>{private Long total;//总条数private List<T> items;//当前页数据集合
}
1.1.2 引入pagehelper坐标
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version></dependency>
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 {@Autowiredprivate ArticleService articleService;@PostMappingpublic Result add(@RequestBody @Validated Article article) {articleService.add(article);return Result.success();}@GetMappingpublic Result<PageBean<Article>> list(Integer pageNum,Integer pageSize,@RequestParam(required = false) Integer categoryId, //不是必须得参数@RequestParam(required = false) String state) {PageBean<Article> 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<Article> 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 {@Autowiredprivate ArticleMapper articleMapper;@Overridepublic void add(Article article) {//补充属性值article.setCreateTime(LocalDateTime.now());article.setUpdateTime(LocalDateTime.now());Map<String,Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");article.setCreateUser(userId);articleMapper.add(article);}@Overridepublic PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {//1.创建PageBean对象PageBean<Article> pb = new PageBean<>();//2.开启分页查询 PageHelperPageHelper.startPage(pageNum,pageSize);//3.调用mapperMap<String,Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");List<Article> as = articleMapper.list(userId,categoryId,state);//Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据Page<Article> p = (Page<Article>) as;//把数据填充到PageBean对象中pb.setTotal(p.getTotal());pb.setItems(p.getResult());return pb;}
}
1.5 Mapper中写动态sql比较麻烦,使用映射配置文件,注意xml路径要保持一致
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.geji.mapper.ArticleMapper"><!--动态sql--><select id="list" resultType="com.geji.pojo.Article">select * from article<where><if test="categoryId!=null">category_id=#{categoryId}</if><if test="state!=null">and state=#{state}</if>and create_user=#{userId}</where></select>
</mapper>
1.6 Postman测试