您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關springboot中怎么利用Jpa 實現分頁功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
由于用的技術并不復雜,所以我們開門見山,直接上代碼
先來看下代碼結構
pom.xml
引入相關jar包<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springboot-blog</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-blog</name> <description>Blog project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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<Article, Integer> { }
ArticleService
接口public interface ArticleService { Page<Article> getArticleWithPage(Integer page, Integer size); }
ArticleServiceImpl
實現類@Service @Slf4j public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao articleDao; @Override public Page<Article> getArticleWithPage(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) { Page<Article> articles = articleService.getArticleWithPage(page, size); model.addAttribute("articles", articles); return "/client/index"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <!-- 載入文章頭部頁面,位置在/client文件夾下的header模板頁面,模板名稱th:fragment為header --> <div th:replace="/client/header::header(null,null)"/> <body> <div class="am-g am-g-fixed blog-fixed index-page"> <div class="am-u-md-8 am-u-sm-12"> <!-- 文章遍歷并分頁展示 : 需要同學們手動完成,基本樣式已經給出,請使用th標簽及表達式完成頁面展示 --> <div th:each="article:${articles.getContent()}"> <article class="am-g blog-entry-article"> <div class="am-u-lg-6 am-u-md-12 am-u-sm-12 blog-entry-text"> <!-- 文章分類 --> <span th:text="${article.categories}" class="blog-color" ><a>默認分類</a></span> <span> </span> <!-- 發布時間 --> <span th:text="'發布于 '+ ${article.created}"/> <h3> <!-- 文章標題 --> <div><a th:text="${article.title}"/> </div> </h3> <!-- 文章內容--> <div th:text="${article.content}"/> </div> </article> </div> <div class="page"> <a th:href="@{/article/index}">首頁</a> <a th:href="@{/article/index(page = ${articles.hasPrevious()} ? ${articles.getNumber() } : 1)}">上一頁</a> <a th:href="@{/article/index(page = ${articles.hasNext()} ? ${articles.getNumber()} + 2 : ${articles.totalPages})}">下一頁</a> <a th:href="@{/article/index(page = ${articles.totalPages})}">尾頁</a></p> </div> </div> <!-- 博主信息描述 --> <div class="am-u-md-4 am-u-sm-12 blog-sidebar"> <div class="blog-sidebar-widget blog-bor"> <h3 class="blog-text-center blog-title"><span>子慕</span></h3> <img th:src="@{/assets/img/me.jpg}" alt="about me" class="blog-entry-img"/> <p> Java后臺開發 </p> <p>個人博客小站,主要發表關于Java、Spring、Docker等相關文章</p> </div> <div class="blog-sidebar-widget blog-bor"> <h3 class="blog-text-center blog-title"><span>聯系我</span></h3> <p> <a><span class="am-icon-github am-icon-fw blog-icon"></span></a> <a><span class="am-icon-weibo am-icon-fw blog-icon"></span></a> </p> </div> </div> </div> </body> <!-- 載入文章尾部頁面,位置在/client文件夾下的footer模板頁面,模板名稱th:fragment為footer --> <div th:replace="/client/footer::footer"/> </html>
以上就是springboot中怎么利用Jpa 實現分頁功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。