使用PageHelper完成列表的显示分页(后台代码编写)

引言

使用pageHelper进行数据查询,显示分页,大大减少了后台的代码编写复杂度,下面是本人的一个简单的springboot项目里面的Controller层代码的小例子演示.

首先需要在pom文件里加入PageHelper的相关依赖

<dependency>
	
	<groupId>tk.mybatisgroupId>
	<artifactId>mapper-spring-boot-starterartifactId>
	<version>1.2.1version>
dependency>

Controller层代码


@Autowired
private UserService userService;

@RequestMapping(value = "/getList" , method = {RequestMethod.POST , RequestMethod.GET})
@ResponseBody
public PageInfo<User> getList(User user , 
	@RequestParam(value = "pageNum" , defaultValue = "1")Integer pageNum,
	@RequestParam(value = "pageSize" , defaultValue = "10")Integer pageSize){
		PageInfo<User> list = userService.selectList(pageNum , pageSize , user);	
		return list;
}

前台接收到的数据是一组json数据,如下所示

{	
	"pageNum":1,
	"pageSize":10,
	"size":10,
	"startRow":1,
	"endRow":10,
	"total":157,
	"pages":16,
	"list":[{"id":"1","name":"张三","age":"25"},{"id":"2","name":"李四","age":"23"},{"id":"3","name":"王五","age":"28"},......{"id":"157","name":"张某","age":"26"}],
	"prePage":0,
	"nextPage":2,
	"isFirstPage":true,
	"isLastPage":false,
	"hasPreviousPage":false,
	"hasNextPage":true,
	"navigatePages":8,
	"navigateNums":[1,2,3,4,5,6,7,8],
	"navitageFirstPage":1,
	"navigateLastPage":8,
	"firstPage":1,
	"lastPage":8
}

当然了,这些数据如果在浏览器看的话会密密麻麻的显示在页面,为了方便大家看数据的格式和信息,我才把数据梳理了一下,具体的大家可以自己尝试着去写一写!

你可能感兴趣的:(随笔)