Spring-Data-Jpa数据分页展示,java服务器开发技术

1、创建持久化类

在entity包中创建一个持久化类Article.java,代码如下

//用于标记持久化类,SpringBoot项目加载后会自动根据持久化类建表

@Entity

//设置表名为tb_article

@Table(name=“tb_article”)

public class Article {

/**

  • 使用@id指定主键。使用代码@GeneratedValue(strategy = GenerationType.IDENTITY)

  • 指定主键的生存策略,mysql默认为自动增长

*/

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer id; //主键

private String title; //名称

private String supplier; //作者

private Double price; //单价

private String locality; //出版社

private int storage; //库存

private String image; //商品图片

private String description; //商品描述

private Date createDate; //出版时间

public Integer getId() {

return id;

}

public void setId(Integer 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 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;

}

}

2、定义数据访问层接口

在repository包下新建一个接口,命名为ArticleRepository,让该接口继承PagingAndSortingRepository继承,以持久化对象Article作为PagingAndSortingRepository的第一类型参数,表示当前所操作的持久化对象类型,Integer作为PagingAndSortingRepository的第二个类型参数,用于指定ID类型,完整代码如下

import com.mcy.springdatajpa.entity.Article;

import org.springframework.data.repository.PagingAndSortingRepository;

public interface ArticleRepository extends PagingAndSortingRepository<Article, Integer> {

}

PagingAndSortingRepository提供了查询所有和分页查询的方法,PagingAndSortingRepository接口的源码如下

import org.springframework.data.domain.Page;

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

import org.springframework.data.domain.Pageable;

import org.springframework.data.domain.Sort;

@NoRepositoryBean

public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {

Iterable findAll(Sort var1); //查询所有,排序

Page findAll(Pageable var1); //分页查询

}

在SpringBoot项目中,数据访问层无须提供实现,直接继承数据访问接口即可。

3、定义业务层类

ArticleService .java代码如下(有findAllSort排序查询所有,findAll分页查询两个方法)

import com.mcy.springdatajpa.entity.Article;

import com.mcy.springdatajpa.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;

import javax.annotation.Resource;

@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);

}

}

在业务层中需要注入数据访问层对象,在上述代码中通过@Resource注解将ArticleRepository接口对应的实现类注入进来的。

4、定义控制器

ArticleController.java代码如下(有sortArticle排序查询所有,sortPagerArticle分页排序查询两个方法)

import com.mcy.springdatajpa.entity.Article;

import com.mcy.springdatajpa.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.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;

@RestController

@RequestMapping("/article")

public class ArticleController {

//注入ArticleServer

@Resource

private ArticleService articleService;

//排序查询

@RequestMapping("/sort")

public Iterable

sortArticle(){

//指定培训参数对象:根据id,进行降序查询

Sort sort = Sort.by(Sort.Direction.DESC, “id”);

Iterable

articleDatas = articleService.findAllSort(sort);

return articleDatas;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值