springboot jpa 实现分页查询

1、创建springboot web工程

2、引入依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.0.RELEASE
         
    
    com.llg
    jpa
    0.0.1-SNAPSHOT
    jpa
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            1.18.10
            provided
        

        
            mysql
            mysql-connector-java
            runtime
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3、数据源配置

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=12345678

4、实体类

package com.llg.jpa.entity;


import lombok.Data;

import javax.persistence.*;

@Data

@Table(name = "users")
@Entity
public class Users {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;
    private String username;
    private String password;
}

5、dao层,要实现PagingAndSortingRepository接口

package com.llg.jpa.dao;

import com.llg.jpa.entity.Users;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface UserDao extends PagingAndSortingRepository {

}

6、service层

package com.llg.jpa.service;

import com.llg.jpa.dao.UserDao;
import com.llg.jpa.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public Page listUser(int pageNum, int pageLimit){
        Pageable pageable =PageRequest.of(pageNum - 1, pageLimit);
        return userDao.findAll(pageable);
    }

}

7、controller层

package com.llg.jpa.controller;


import com.llg.jpa.entity.Users;
import com.llg.jpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("listUserPage")
    public Page listUserPage(@RequestParam int pageNum, @RequestParam int pageSize){
        return userService.listUser(pageNum, pageSize);
    }

}

 

 

 

 

 

 

 

你可能感兴趣的:(java,spring,boot)