SpringBoot2 实现JPA分页和排序分页

分页

application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1/jpa?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
  # 更新或者创建数据表结构
      ddl-auto: update
  # 控制台显示SQL
    show-sql: true
    properties:
      hibernate.format_sql: true

实体类

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer empId;

    private String lastName;

    private String email;

    @Temporal(TemporalType.DATE)
    private Date birth;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;

    @ManyToOne
    @JoinColumn(name = "dept_id")
    private Department department;

    // 省去 set get方法
}


@Entity
@Table(name = "department")
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer deptId;

    private String deptName;
    // 省去 set get方法
}

Repository接口类

import com.springboot.jpa.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}

service 接口类

import com.springboot.jpa.entity.Employee;
import org.springframework.data.domain.Page;

public interface EmployeeService {

    // 普通分页
    Page getPage(Integer pageNum, Integer pageLimit);

    // 排序分页
    Page getPageSort(Integer pageNum, Integer pageLimit);
}

Service 实现类

import com.springboot.jpa.dao.EmployeeRepository;
import com.springboot.jpa.entity.Employee;
import com.springboot.jpa.service.EmployeeService;
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.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeRepository employeeRepository;

    // 普通分页
    @Override
    @Transactional(readOnly = true)  // 只读事务
    public Page getPage(Integer pageNum, Integer pageLimit) {
        Pageable pageable =new PageRequest(pageNum - 1,pageLimit);
        return employeeRepository.findAll(pageable);
    }

    // 分页排序
    @Override
    @Transactional(readOnly = true)  
    public Page getPageSort(Integer pageNum, Integer pageLimit) {
        Sort sort = new Sort(Sort.Direction.DESC,"createTime");
        Pageable pageable =new PageRequest(pageNum - 1, pageLimit, sort);
        return employeeRepository.findAll(pageable);
    }
}

Controller控制器类

import com.springboot.jpa.entity.Employee;
import com.springboot.jpa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    // 分页显示数据
    @GetMapping("/emp")
    public Page showPage(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size){
        System.out.println("分页: page:"+page+"; size:"+size);
        return employeeService.getPage(page, size);
    }

    // 排序分页显示数据
    @GetMapping("/emp_sort")
    public Page showSortPage(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size){
        System.out.println("排序分页: page:"+page+"; size:"+size);
        return employeeService.getPageSort(page, size);
    }
}

分页显示的json格式串

http://localhost:8080/emp_sort?page=1&size=10 url格式

{
    "content": [{
        "lastName": "7QW",
        "email": "[email protected]",
        "birth": "2018-08-06",
        "createTime": "2018-08-30T07:40:34.000+0000",
        "id": 5,
        "dempartment": {
            "deptName": "BBB",
            "id": 2
        }
    }, {
        "lastName": "qax",
        "email": "[email protected]",
        "birth": "2018-08-06",
        "createTime": "2018-08-24T07:40:56.000+0000",
        "id": 6,
        "dempartment": {
            "deptName": "AAA",
            "id": 1
        }
    }
    }],
    "pageable": {
        "sort": {
            "sorted": true,
            "unsorted": false
        },
        "offset": 0,
        "pageNumber": 0,
        "pageSize": 10,
        "unpaged": false,
        "paged": true
    },
    "last": true,
    "totalElements": 6,
    "totalPages": 1,
    "number": 0,
    "size": 10,
    "sort": {
        "sorted": true,
        "unsorted": false
    },
    "numberOfElements": 6,
    "first": true
}

你可能感兴趣的:(SpringBoot,java,SpringBoot2,技术点总结)