接口文档
pojo类
package com.heo.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;//当前页数据集合
}
controller
@GetMapping
public 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);
}
service
@Override
public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
PageBean<Article> pb = new PageBean<>();
// 开启分页查询
PageHelper.startPage(pageNum, pageSize);
Map<String, Object> map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
List<Article> as = articleMapper.list(userId, categoryId, state);
Page<Article> p = (Page<Article>) as;
pb.setTotal(p.getTotal());
pb.setItems(p.getResult());
return pb;
}
pagehelper 依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
mapper
List<Article> list(Integer userId, Integer categoryId, String state);
映射配置文件写动态SQL
映射配置文件与源目录结构一致,创建 com/heo/mapper
(这里是 /
分隔)。文件名字也需要一致。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.heo.mapper.ArticleMapper">
<!--动态 sql-->
<select id="list" resultType="com.heo.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>