用limit 实现java的简单分页

一 mysql 中limit 用法

select * from table limit m,n       
意思是: 在table数据库中, 从m开始,拉取n条数据.

在mysql中, m代表index, 默认从0 开始;   n最小从m+1开始,取n条
limit start,size           从start条开始,获取size条数据

二 分页实现

前端思路:

将page 和 rows 两个参数传递给后端	
page : 代表第几页
rows:  代表当前页显示的数据条数

java思路:

获取当前页的第一条:          (page-1)*rows	
sql语句查询分页: 	        limit (page-1)*rows,rows
sql语句获取列表总数量:       select count(1) from table

三 后端代码:

Controller:

	@Autowired
    private ActivityService activityService;
    	
	@RequestMapping("/url")
    public ResponseEntity<?> getRecords(@RequestParam("uid") String uid,
                                            @RequestParam(value="page",required = false, defaultValue ="1") int page,
                                            @RequestParam(value="rows",required = false, defaultValue ="10") int rows){ 
        List<MyRecord> records = activityService.getMyRecord(uid,page,rows);
        Long  total = activityService.getMyRecordCount(uid);
        JSONObject result = new JSONObject();
        result.put("result", "ok");
        result.put("records", records);
        result.put("total ", total );
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

Service:

List<MyRecord> getMyRecord((String uid, int page, int rows);
Long  getMyRecordCount(String uid);

ServiceImpl:

@Autowired
private ActivityMapper activityMapper;
   
   /**
   * 获取分页列表
   */
public List<MyRecord> getMyRecord(String uid, int page, int rows) {
    int i = (page - 1) * rows;
    List<MyRecord> records = activityMapper.getMyRecordToPage(uid, i, rows);
    return records;
}

/**
   * 获取列表总数量
   */
public Long getMyRecorCount(String uid) {
     Long total= activityMapper.getMyRecordToPageCount(uid);
     return total;
}

Dao:

@Select("select * from myRecord where uid = #{0} order by create_time desc limit #{1},#{2}")
List<NineMyRecord> getMyWinRecordToPage(String uid, int i, int rows);

@Select("select count(*) from myRecord where uid = #{0})
Long  getMyWinRecordToPageCount(String uid);
  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值