PagingAndSortingRepository接口实战

一 pom


     4.0.0
     org.fkit
     pagingandsortingrepositorytest
     0.0.1-SNAPSHOT
     jar
     pagingandsortingrepositorytest
     http://maven.apache.org
     
     
           org.springframework.boot
           spring-boot-starter-parent
           2.0.0.RELEASE
           
     
     
           UTF-8
           UTF-8
           1.8
     
     
           
           
                org.springframework.boot
                spring-boot-starter-web
           
           
           
                org.springframework.boot
                spring-boot-starter-thymeleaf
           
           
           
                mysql
                mysql-connector-java
           
           
           
                org.springframework.boot
                spring-boot-starter-data-jpa
           
           
                junit
                junit
                test
           
     

二 运行类

package org.fkit.pagingandsortingrepositorytest;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
        // SpringApplication 用于从main方法启动Spring应用的类。
        SpringApplication.run(App.class, args);
    }
}

三 持久化类

package org.fkit.pagingandsortingrepositorytest.bean;


import java.io.Serializable;
import java.util.Date;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


/**
*  商品对象
* */
@Entity
@Table(name="tb_article")
public class Article implements Serializable{


    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String title;
    private String supplier;
    private Double price;
    private String locality;
    private Date putawayDate;
    private int storage;
    private String image;
    private String description;
    private Date createDate;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getSupplier() {
        return supplier;
    }
    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    public String getLocality() {
        return locality;
    }
    public void setLocality(String locality) {
        this.locality = locality;
    }
    public Date getPutawayDate() {
        return putawayDate;
    }
    public void setPutawayDate(Date putawayDate) {
        this.putawayDate = putawayDate;
    }
    public int getStorage() {
        return storage;
    }
    public void setStorage(int storage) {
        this.storage = storage;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

四 控制器

package org.fkit.pagingandsortingrepositorytest.controller;


import java.util.List;
import javax.annotation.Resource;
import org.fkit.pagingandsortingrepositorytest.bean.Article;
import org.fkit.pagingandsortingrepositorytest.service.ArticleService;
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;


@RestController
@RequestMapping("/article")
public class ArticleController {
    
    // 注入ArticleService
    @Resource
    private ArticleService articleService;


    @RequestMapping("/sort")
    public Iterable
sortArticle() { // 指定排序参数对象:根据id,进行降序查询 Sort sort = new Sort(Sort.Direction.DESC, "id"); Iterable
articleDatas = articleService.findAllSort(sort); return articleDatas; } @RequestMapping("/pager") public List
sortPagerArticle(int pageIndex) { // 指定排序参数对象:根据id,进行降序查询 Sort sort = new Sort(Sort.Direction.DESC, "id"); /** * 封装分页实体 参数一:pageIndex表示当前查询的第几页(默认从0开始,0表示第一页) 参数二:表示每页展示多少数据,现在设置每页展示2条数据 * 参数三:封装排序对象,根据该对象的参数指定根据id降序查询 * */ Pageable page = PageRequest.of(pageIndex - 1, 2, sort); Page
articleDatas = articleService.findAll(page); System.out.println("查询总页数:" + articleDatas.getTotalPages()); System.out.println("查询总记录数:" + articleDatas.getTotalElements()); System.out.println("查询当前第几页:" + articleDatas.getNumber() + 1); System.out.println("查询当前页面的记录数:" + articleDatas.getNumberOfElements()); // 查询出的结果数据集合 List
articles = articleDatas.getContent(); System.out.println("查询当前页面的集合:" + articles); return articles; } }

五 Service层

package org.fkit.pagingandsortingrepositorytest.service;

import javax.annotation.Resource;

import org.fkit.pagingandsortingrepositorytest.bean.Article;
import org.fkit.pagingandsortingrepositorytest.repository.ArticleRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


@Service
public class ArticleService {
    
    // 注入数据访问层接口对象
    @Resource
    private ArticleRepository articleRepository;
    
    public Iterable
findAllSort(Sort sort) { return articleRepository.findAll(sort); } public Page
findAll(Pageable page) { return articleRepository.findAll(page); } }

六 Repository层

package org.fkit.pagingandsortingrepositorytest.repository;

import org.fkit.pagingandsortingrepositorytest.bean.Article;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface ArticleRepository extends PagingAndSortingRepository {
}

七 数据库

INSERT INTO tb_article (
  TITLE,
  SUPPLIER,
  PRICE,
  LOCALITY,
  PUTAWAY_DATE,
  STORAGE,
  IMAGE,
  DESCRIPTION,
  CREATE_DATE
)
VALUES
  (
    '疯狂Java讲义(附光盘)。',
    '李刚 著',
    108.8,
    '电子工业出版社',
    NULL,
    100,
    'java.jpg',
    '疯狂源自梦想,技术成就辉煌 本书来自作者3年的Java培训经历,凝结了作者近3000个小时的授课经验,总结了几百个Java学员学习过程中的典型错误。',
    '2008-10-01'
  ),
  (
    '轻量级Java EE企业应用实战。',
    '李刚 著',
    79.8,
    '电子工业出版社',
    NULL,
    100,
    'ee.jpg',
    '本书主要介绍以Spring+Hibernate为基础的Java EE应用。',
    '2008-11-01'
  ),
  (
    '疯狂Ajax讲义(含CD光盘1张)。',
    '李刚 著',
    66.6,
    '电子工业出版社',
    NULL,
    100,
    'ajax.jpg',
    '异步访问技术,现在所有网站都在用。',
    '2011-07-01'
  ),
  (
    '疯狂iOS讲义(基础篇)(含光盘1张)',
    '李刚 著',
    85.6,
    '电子工业出版社',
    NULL,
    100,
    'ios.jpg',
    '基于iOS全新版本彻底升级,Swift和Objective-C双语讲解。',
    '2016-05-01'
  ),
  (
    '魔戒:插图珍藏版(200套限量编号版随机发送!)。奴役全世界的力量。',
    '(英国) J.R.R.托尔金 著',
    132.3,
    '译林出版社',
    NULL,
    100,
    '22566493-1_b.jpg',
    '天真无邪的哈比男孩佛罗多继承了一枚戒,这是一次异常艰险的远征……',
    '2012-06-18'
  ),
  (
    '肖申克的救赎。',
    '(美)金 著,施寄青,赵永芬,齐若兰',
    19.9,
    '人民文学出版社',
    NULL,
    100,
    '9198692-1_l.jpg',
    '本书是斯蒂芬•金最为人精精乐道的杰出代表作。',
    '2012-06-11'
  ),
  (
    '权威定本四大名著: 红楼梦 三国演义  水浒传  西游记 全国独家',
    '[明] 吴承恩,[明] 施耐庵,[明] 罗贯中,[清] 曹雪芹 著;黄肃秋 注',
    145.8,
    '清华大学出版社',
    NULL,
    100,
    '20605371-1_a.jpg',
    '权威定本四大名著: 红楼梦 三国演义  水浒传  西游记。',
    '2012-06-11'
  ) ,
  (
    '万物生光辉',
    '[英] 吉米·哈利 著;余国芳,谢瑶玲 译',
    23.6,
    '湖北教育出版社',
    NULL,
    100,
    '22639083-1_a.jpg',
    '邂逅最可爱的动物,感受最纯真的幽默。',
    '2012-06-11'
  ),
  (
    '爱你是最好的时光',
    '匪我思存 著 ',
    17.6,
    '译林出版社',
    NULL,
    100,
    '22630101-1_a.jpg',
    '终极大结局完结篇。',
    '2012-06-11'
  ),
  (
    '白鹿原',
    '陈忠实 著',
    360,
    '春风文艺出版社',
    NULL,
    100,
    '22541642-1_a.jpg',
    '中国首部当代名家名篇宣纸线装书,陈忠实先生亲笔签名签章限量珍藏版。',
    '2012-06-11'
  ),
(
    'Spring+MyBatis企业应用实战。',
    '肖文吉 著',
    58,
    '电子工业出版社',
    NULL,
    100,
    'Spring+MyBatis.jpg',
    '媲美于SSH组合的轻量级Java EE开发方式。',
    '2017-01-01'
  ),
  (
    '疯狂XML讲义(含光盘1张),包括DTD、Schema等技术的深入讲解。',
    '李刚 著',
    48.8,
    '电子工业出版社',
    NULL,
    100,
    'xml.jpg',
    '本书主要以XML为核心,深入地介绍了XML的各种相关知识。',
    '2012-06-11'
  ),
  (
    '疯狂Java:突破程序员基本功的16课(修订版)',
    '李刚 著',
    44.2,
    '电子工业出版社',
    NULL,
    100,
    'basic.jpg',
    '《疯狂Java:突破程序员基本功的16课(修订版)》',
    '2012-06-11'
  ),
  (
    '疯狂Android讲义(含CD光盘1张)',
    '李刚 著',
    60.6,
    '电子工业出版社',
    NULL,
    100,
    'android.jpg',
    '本书全面地介绍了Android应用开发的相关知识。',
    '2012-06-11'
  );

八 测试

PagingAndSortingRepository接口实战_第1张图片

PagingAndSortingRepository接口实战_第2张图片

你可能感兴趣的:(Spring,Boot)