SpringBoot+MyBatisPlus自定义分页查询(两个表)

一、定义一个分页通用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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CC_Waiting

我,大学未工作,感谢您的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值