一、定义一个分页通用entity
/**
* @ClassName PageVo
* @Description T0D0
* @Author cc
* @Date 2022/3/28 13:00
* @Version 1.0
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageVo<T> {
private Integer total;//总条数
private List<T> dataList;//数据
private Integer currentPage;//当前页数
private Integer pageSize;//当前页面条数
}
二、编写controller、service、mapper
1.controller
@GetMapping("/myApply/{currentPage}/{pageSize}")
public R getAllApply(@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize,Apply apply){
PageVo<Apply> page = applyService.getPage(currentPage,pageSize,apply);
// System.out.println(page);
return new R(true,page);
}
2.service
@Override
public PageVo<Apply> getPage(Integer currentPage, Integer pageSize, Apply apply) {
String name = apply.getName();
String username = apply.getUsername();
currentPage=currentPage-1;
Integer total = applyMapper.selectApplyCount();
List<Apply> dataList = applyMapper.getPage(currentPage,pageSize,name,username);
PageVo<Apply> pageVo = new PageVo<>();
pageVo.setTotal(total);//设置总条数
pageVo.setDataList(dataList);//设置查询数据
pageVo.setCurrentPage(currentPage+1);//设置页面显示当前页
pageVo.setPageSize(pageSize);//设置页面当前显示条数
return pageVo;
}
3.mapper.xml
<select id="getPage" resultType="com.cc.travel.admin.entity.Apply">
select a.id,
a.name,
a.city,
a.user_id userId,
a.number,
a.area,
a.address,
a.description,
a.is_cross isCross,
a.createTime,
a.editTime,
u.username username
from t_travel_apply a
inner join t_general_user u on a.user_id = u.id
<where>
<if test="name !=null and name !=''">
and a.name like '%' #{name} '%'
</if>
<if test="username !=null and username !=''">
and u.username like '%' #{username} '%'
</if>
</where>
limit #{currentPage},#{pageSize}
</select>
#查询总条数
<select id="selectApplyCount" resultType="java.lang.Integer">
select count(*) from t_travel_apply a
inner join t_general_user u on a.user_id = u.id
</select>

被折叠的 条评论
为什么被折叠?



