用limit 实现java的简单分页
生活随笔
收集整理的這篇文章主要介紹了
用limit 实现java的简单分页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
https://blog.csdn.net/xinyuezitang/article/details/84324359
?
用limit 實現java的簡單分頁
xinyuezitang?2018-11-21 16:01:13??4447??收藏?9
分類專欄:?Java 小Demo?文章標簽:?分頁?limit?mysql?實現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:
@Autowiredprivate 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);?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的用limit 实现java的简单分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正数和负数的定义 什么叫正数和负数
- 下一篇: 煤油的密度是多少 煤油的密度介绍