SpringBoot使用PageHelper实现分页,简单实例

一.添加依赖及配置

	
        com.github.pagehelper
        pagehelper-spring-boot-starter
        1.2.5
    
    //听说版本高了搞不好会莫名报错?

	application.yml文件:
	
	pagehelper:
	helper-dialect: mysql
	params: count=countSql
	reasonable: true
	support-methods-arguments: true

二.实体类

	@Entity
	public class User {
		@Id
		@GeneratedValue
			private Integer id;
			private String name;
			private Integer age;

			public User() {
			}

		public User(Integer id,Integer age) {
    		this.id = id;
    		this.name = name;
    		this.age = age;
			 }

		public Integer getId() {
    		return id;
		}

		public void setId(Integer id) {
    		this.id = id;
		}

		public String getName() {
    		return name;
		}

		public void setName(String name) {
    		this.name = name;
		}

		public Integer getAge()
		{
    		return age;
		}

		public void setAge(Integer age)
		{
   			this.age=age;
		}
	}

三.Mapper

	@Mapper
		public interface  UserMapper {
			@Select("SELECT * FROM USER")
			 Page getUserList();
		}

四.Controller

	@PostMapping (value = "/searchh")
	@ResponseBody
	public Page getall(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                    @RequestParam(value = "pageSize", required = false, defaultValue = "2") int pageSize)

	 {
   		PageHelper.startPage(page,pageSize);
   		Page userlist = userMapper.getUserList();
   		return userlist;
	}

五.验证

数据库里是20条数据,我用postman 发的参数是: page=1,pageSize=5,即显示第1页,总共分为5页,显示如下:
SpringBoot使用PageHelper实现分页,简单实例_第1张图片

你可能感兴趣的:(SpringBoot使用PageHelper实现分页,简单实例)