Spring boot 用pageHelper进行分页

Main 方法
@RestController
@SpringBootApplication
public class SpringBoot {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBoot.class, args);
    }
      @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("dialect","mysql");
        properties.setProperty("supportMethodsArguments","true");
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

逻辑层
@Autowired
private StudentMapper mapper;

public Result Query(String name, String curPage, String pageNum)throws Exception{
	if(StringUtils.isEmpty(curPage)) {
		curPage="1";
	}
	if(StringUtils.isEmpty(pageNum)) {
		pageNum="10";
	}
	if(StringUtils.isEmpty(name)) {
		name="";
	}
	int curPageI=Integer.parseInt(curPage);
	int pageNumI=Integer.parseInt(pageNum);
	List li=mapper.querylimit("%"+name+"%",curPageI,pageNumI);
	PageInfo pi=new PageInfo(li);
	Result r =new Result();
	r.setCode(0);
	r.setCount((int)pi.getTotal());
	r.setData(li);
	return r;
}

控制层

@Autowired
	private StudentService service;
	@GetMapping(value="/students")
	public Result ListStudent(String name, String page, String limit) throws Exception {
		try {
			Result li = service.Query(name, page,limit);
			return li;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
@PostMapping(value="/student")
public Result addStudent(Student student) throws Exception {
	Result li=new Result();
	try{
		service.add(student);
	}catch (Exception e){
	    //回调函数
		li.setCode(1);
		li.setMsg("新增出错:"+e.getMessage());
	}
	return li;
}

@DeleteMapping(value="/student/{id}")
public Result deleteStudent(@PathVariable String id) throws Exception {
	Result li=new Result();
	try{
		service.delete(id);
	}catch (Exception e){
		li.setCode(1);
		li.setMsg("删除出错:"+e.getMessage());
	}
	return li;
}

@PutMapping(value="/student/{id}")
public Result updateStudent(@PathVariable String id, Student student) throws Exception {
	Result li=new Result();
	student.setId(id);
	try{
		service.update(student);
	}catch (Exception e){
		li.setCode(1);
		li.setMsg("修改出错:"+e.getMessage());
	}
	return li;
}

xml文件配置

	
		org.springframework.boot
		spring-boot-starter-web
	
	
		mysql
		mysql-connector-java
	
	
		org.springframework.boot
		spring-boot-starter-data-jpa
	
	
		com.alibaba
		druid-spring-boot-starter
		1.1.10
	
	
		org.mybatis.spring.boot
		mybatis-spring-boot-starter
		1.3.1
	
	
		commons-lang
		commons-lang
		2.1
	
	
	
		com.github.pagehelper
		pagehelper
		4.1.6
	

mapper

@Mapper
public interface StudentMapper{
    @Select("select * from student where name like #{name}")
    public List querylimit(@Param("name")String name, @Param("pageNum")int pageNum, @Param("pageSize") int pageSize);
    @Delete("delete from student where id=#{id}")
    public void delete(@Param("id")String id);
    @Insert("insert into student(name,age,sex,email,phone) values(#{name},#{age},#{sex},#{email},#{phone})")
    public void add(Student student);
    @Update("update student set name=#{name},age=#{age},sex=#{sex},email=#{email},phone=#{phone} where id=#{id}")
    public void update(Student student);
}

回调函数属性类

public class Result {
    private int code;
    private String msg;
    private int count;
    private List data;
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }


}

属性类

public class Student {
	private String id;
	private String name;
	private String age;
	private String sex;
	private String email;
	private String phone;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

}

application.properties 配置数据库连接

spring.datasource.url=jdbc:mysql://localhost/nuit02
spring.datasource.username=root
spring.datasource.password=ps123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

html 视图




    
    Title
    
    
    
    


新增

你可能感兴趣的:(Spring boot 用pageHelper进行分页)