大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章给大家分享的是有关springboot中怎么利用Jpa 实现分页功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为发电机回收等企业提供专业服务。
由于用的技术并不复杂,所以我们开门见山,直接上代码
先来看下代码结构
pom.xml
引入相关jar包4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE com.example springboot-blog 0.0.1-SNAPSHOT springboot-blog Blog project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web junit junit MySQL mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
application.properties
配置# 配置数据源 spring.datasource.url=jdbc:mysql://localhost:3306/blog_system spring.datasource.username=root spring.datasource.password=root #基本配置 spring.application.name=springboot-blog server.port=8080 # 显示sql spring.jpa.show-sql=true
Article
@Data @ToString @Entity @Table(name = "t_article") public class Article { @Id private Integer id; @Column(name = "title") private String title; @Column(name = "content") private String content; @Column(name = "created") private Date created; @Column(name = "modified") private Date modified; @Column(name = "categories") private String categories; @Column(name = "tags") private String tags; @Column(name = "allow_comment") private Integer allowComment; @Column(name = "thumbnail") private String thumbnail; }
ArticleDao
层实现public interface ArticleDao extends JpaRepository{ }
ArticleService
接口public interface ArticleService { PagegetArticleWithPage(Integer page, Integer size); }
ArticleServiceImpl
实现类@Service @Slf4j public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao articleDao; @Override public PagegetArticleWithPage(Integer page, Integer size) { log.info("page is {}, size is {}", page, size); if (page <= 0) { page = 1; } Pageable pageRequest = PageRequest.of(page - 1, size); return articleDao.findAll(pageRequest); } }
ArticleController
控制层实现@Controller @Slf4j @RequestMapping("/article") public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping("/index") public String toIndex(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "3") Integer size) { Pagearticles = articleService.getArticleWithPage(page, size); model.addAttribute("articles", articles); return "/client/index"; } }
以上就是springboot中怎么利用Jpa 实现分页功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。