- Mysql分页
首先需要接收一个参数page用来获取当前页数和一个参数rows用来获取每页多少条数据。接着从数据库中取出全部数据,算出它的长度,然后根据长度和page和rows来判断总页数、本页起始数据是第几条start等。接着将start起始条数和rows每页多少天传递至DAO层查询对应数据放入集合中传递至前台。具体实现如下
//Controller层
@RequestMapping("queryAll")
@ResponseBody
public List<Product> queryAll(int rows,int page){
Map<String,Object> map = new HashMap<>();
List<Product> list = productService.queryAll();
System.out.println(list);
//判断起始条数start和总共多少页total
int num = list.size();
int total = num / rows;
int lost = num % rows;
if(lost != 0){
total++;
}
if(page > total){
page = total;
}
int start = (page-1)*rows;//起始条数
//根据起始条数和每页多少条查询出当前page页的数据
List<Product> proList = productService.queryPro(rows,start);;
return proList;
}
//Mapper查询语句
select * from shop_product limit #{start},#{rows}