mysql 中limit 用法
select * from table limit m,n
在mysql中, m代表index, 默认从0 开始; n最小从m+1开始,取n条
意思是: 在table数据库中, 从m开始,拉取n条数据.
limit start,size 从start条开始,获取size条数据
分页实现
前端:
将page 和 rows 两个参数传递给后端
page : 代表第几页
rows: 代表当前页显示的数据条数
java思路:
获取当前页的第一条: (page-1)*rows
sql语句查询分页: limit (page-1)*rows,rows
后端:
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 ="25") int rows){
List<MyRecord> records = activityService.getMyRecord(uid,page,rows);
JSONObject result = new JSONObject();
result.put("result", "ok");
result.put("records", records);
return new ResponseEntity<>(result, HttpStatus.OK);
}
Service:
@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;
}
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);