数据库使用的是mysql
要想在Mybatis中使用分页查询,首先要清楚mysql中limit的用法。
limit a,b
a是从第a+1条数据开始,b是指读取几条数据
例如:select * from table limit 0,10
这句sql语句是说从表中获取第1条开始的10条记录
前端将page:页码
pageSize:每页多少条
这两个参数,传到后台。
通过这两个参数,可以算出a计算方法 a=(page-1)*pageSize
- 首先在mapper.xml中写sql语句
<select id="doGetList" resultMap="studyRecordMap"> select * from study_record order by create_date desc limit ${start},${pageSize} </select>
- 接着在对应的mapper文件中写一个接口调用这句sql语句
注意:接口名一定要与上面的id名相同,否则会找不到对应的sql语句 -
List<studyRecord> doGetList(@Param("start") String start, @Param("pageSize") String pageSize);
最后,调用这个方法,查询数据
-
/** *根据公司来分页查询数据 * @author hsj * @param * @return com.github.wxiaoqi.security.common.msg.TableResultResponse<com.github.wxiaoqi.security.study.entity.HdAppUser> */ @ApiOperation("查询记录(前端)") @RequestMapping(value = "/findRecord",method = RequestMethod.POST) @ResponseBody public List<studyRecord> findRecord(String page,String pageSize){ List<studyRecord> list=new ArrayList<>(); try { if(StringUtil.isEmpty(page)){ page="1"; } if(StringUtil.isEmpty(pageSize)){ index="10"; } int start=Integer.parseInt(page); int indexInt=Integer.parseInt(pageSize); list=baseBiz.doGetList(String.valueOf((start-1)*indexInt),String.valueOf(indexInt)); } catch (Exception e) { e.printStackTrace(); } return list; }
-