springboot项目分页查询
pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.9</version>
<exclusions>
<exclusion>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
utils/PageResult.java(自定义分页返回格式)
package com.example.server_assess.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
/**
* 封装分页查询结果
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult implements Serializable {
private long total; //总记录数
private List records;//当前页数据集合
}
Service层使用
import com.example.server_assess.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@Autowired
AssessMapper assessMapper;
@Override
public ResultJson getAssessList(Map<String, Object> params) {
// 设置分页
Integer currentPage = 1;
Integer pageNum = 30;
if(params.containsKey("currentPage")){
currentPage = (Integer) params.get("currentPage");
}
if(params.containsKey("pageNum")){
pageNum = (Integer) params.get("pageNum");
}
PageHelper.startPage(currentPage, pageNum);
Page<AssessListDto> page = assessMapper.getAssessList(params);
long total = page.getTotal();
return new ResultJson<>(new PageResult(total, page.getResult()));
}
Mapper层
@Mapper
public interface AssessMapper extends BaseMapper<Assess> {
Page<AssessListDto> getAssessList(Map<String, Object> params);
}
xml
<select id="getAssessList" parameterType="map" resultType="com.example.server_assess.pros.assess.model.dto.AssessListDto">
SELECT
assess_id as "assessId",
name,
idno,
age
FROM
tb_assess
WHERE
state=1
<if test="name != null and name != ''">
and name like concat('%', #{name}, '%')
</if>
<if test="idno != null and idno != ''">
and idno = #{idno}
</if>
ORDER BY assess_id desc
</select>
原文地址:https://blog.csdn.net/admin_web/article/details/144292417
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!