成都创新互联网站制作重庆分公司

springboot中怎么利用Jpa实现分页功能

本篇文章给大家分享的是有关springboot中怎么利用Jpa 实现分页功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为发电机回收等企业提供专业服务。

Springboot 集成 Jpa 实现分页

由于用的技术并不复杂,所以我们开门见山,直接上代码

先来看下代码结构
springboot中怎么利用Jpa 实现分页功能

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 {

}

Article Service 层

ArticleService接口

public interface ArticleService {

    Page
 getArticleWithPage(Integer page, Integer size); }

ArticleServiceImpl实现类

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    public Page
 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
 articles = articleService.getArticleWithPage(page, size);         model.addAttribute("articles", articles);         return "/client/index";     } }

页面核心代码







    

        
        
            

                
                    
                    默认分类
                       
                    
                    
                    

                                                 
                        
                    

                                                          
                     
                     首页             上一页             下一页             尾页

        
                                    子慕                          

                Java后台开发             

            

个人博客小站,主要发表关于Java、Spring、Docker等相关文章

                              联系我             

                                              

             

效果展示

springboot中怎么利用Jpa 实现分页功能

以上就是springboot中怎么利用Jpa 实现分页功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


本文题目:springboot中怎么利用Jpa实现分页功能
网站路径:http://cxhlcq.com/article/gededj.html

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部