SpringBoot_Jpa_自定义排序结果分页显示

 本博文只为打卡,自定义排序结果分页显示(相关解释部分在注释)

 

直接打开迭代部分(测试类):

    
    @Autowired
	private pageDisplay pagedisplay;
 
    @Test
	public void contextLoads_page() {
		// 排序(排序规则)
		Order idOrder = new Order(Direction.DESC, "id");
		Order nameOrder = new Order(Direction.ASC, "name");
		Sort sort = new Sort(idOrder, nameOrder);
		Pageable pageable = new PageRequest(1, 2, sort);
		Page findAll = null;
		List content = null;
		///输出(判断下一页是否存在)
		while(pageable != null) {
			findAll = pagedisplay.findAll( pageable);
			content = findAll.getContent();
			System.out.println("------------------------------");
			System.out.println( content );
			System.out.println("------------------------------");
			pageable = findAll.nextPageable();
		}	
	}

 

接口部分的代码:

package com.briup.jpa.dao;

import org.springframework.data.repository.PagingAndSortingRepository;

import com.briup.jpa.bean.Customer;
/**
  *   父类存在相关接口的定义,findAll()  springBoot会进行处理
  */
public interface pageDisplay extends PagingAndSortingRepository{

	
}

 

首先自定义一个Customer对象:

package com.briup.jpa.bean;

import javax.persistence.Entity;
import javax.persistence.Id;
//import javax.persistence.Table;

/*
 * 建立和表中的联系
 */
@Entity
public class Customer {
	///识别(主键)
	@Id
	private long id;
	
	private String name;
	private long age;
	private String gender;

	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + "]";
	}

	public long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public long getAge() {
		return age;
	}

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

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Customer(long id, String name, long age, String gender) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	public Customer() {

	}
}

 

你可能感兴趣的:(SpringBoot)