本篇文章給大家分享的是有關(guān)springboot中怎么利用Jpa 實(shí)現(xiàn)分頁(yè)功能,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。
網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專(zhuān)注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營(yíng)網(wǎng)站定制開(kāi)發(fā).小程序定制開(kāi)發(fā),H5頁(yè)面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為發(fā)電機(jī)回收等企業(yè)提供專(zhuān)業(yè)服務(wù)。
由于用的技術(shù)并不復(fù)雜,所以我們開(kāi)門(mén)見(jiàn)山,直接上代碼
先來(lái)看下代碼結(jié)構(gòu)
pom.xml
引入相關(guān)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
配置# 配置數(shù)據(jù)源 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
層實(shí)現(xiàn)public interface ArticleDao extends JpaRepository{ }
ArticleService
接口public interface ArticleService { PagegetArticleWithPage(Integer page, Integer size); }
ArticleServiceImpl
實(shí)現(xiàn)類(lèi)@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
控制層實(shí)現(xiàn)@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 實(shí)現(xiàn)分頁(yè)功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。