PagingAndSortingRepository接口访问数据

       PagingAndSortingRepository继承自CrudRepository接口,所以除了拥有CrudRepository的功能之外,还增加了排序和分页查询的功能。

创建持久化类

程序清单:/jpa/src/main/java/com/dwx/bean/Book.java

package com.dwx.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_book")
public class Book {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer bookId;			//图书编号
	private String bookName;		//图书名称
	private String category;		//类别
	private String author;			//作者
	private String press;			//出版社
	private Double price;			//价格
	public Integer getBookId() {
		return bookId;
	}
	public void setBookId(Integer bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
}

定义数据访问接口

程序清单:/jpa/src/main/java/com/dwx/repository/BookRepository.java

package com.dwx.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.dwx.bean.Book;
public interface BookRepository extends PagingAndSortingRepository {

}

定义业务层类

程序清单:/jpa/src/main/java/com/dwx/service/BookService.java

package com.dwx.service;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.dwx.bean.Book;
import com.dwx.repository.BookRepository;
@Service
public class BookService {
	@Resource
	private BookRepository bookRepository;
	
	public Iterable findAllSort(Sort sort){
		return bookRepository.findAll(sort);
	}
	
	public Page findAll(Pageable page){
		return bookRepository.findAll(page);
	}
}

定义控制器类

程序清单:/jpa/src/main/java/com/dwx/controller/BookController.java

package com.dwx.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dwx.bean.Book;
import com.dwx.service.BookService;
@RestController
public class BookController {
	@Resource
	private BookService bookService;
	
	@RequestMapping("/sort")
	public Iterable sortBook(){
		Sort sort=new Sort(Sort.Direction.DESC,"bookId");
		Iterable bookSort=bookService.findAllSort(sort);
		return bookSort;
	}
	
	@RequestMapping("/pager")
	public List sortPagerBook(int pageIndex){
		Sort sort=new Sort(Sort.Direction.DESC,"bookId");
		Pageable page=PageRequest.of(pageIndex-1, 2,sort);
		Page bookDatas=bookService.findAll(page);
		List books=bookDatas.getContent();
		return books;
	}
}

测试应用

启动Spring Boot项目,访问以下地址:http://localhost:8080/sort

PagingAndSortingRepository接口访问数据_第1张图片

测试分页查询第一页,访问以下地址:http://localhost:8080/pager?pageIndex=1

PagingAndSortingRepository接口访问数据_第2张图片

你可能感兴趣的:(spring,boot)