mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
数据分页排序案例
This commit is contained in:
79
chapter-5-spring-boot-paging-sorting/pom.xml
Normal file
79
chapter-5-spring-boot-paging-sorting/pom.xml
Normal file
@@ -0,0 +1,79 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>spring.boot.core</groupId>
|
||||
<artifactId>chapter-5-spring-boot-paging-sorting</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>chapter-5-spring-boot-paging-sorting</name>
|
||||
<description>第五章数据分页排序案例</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.M4</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Web 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 单元测试依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- h2 数据源连接驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- Spring Boot Maven 插件 -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>1.5.1.RELEASE</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/libs-milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package spring.boot.core;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* 应用启动程序
|
||||
*
|
||||
* Created by bysocket on 18/09/2017.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class PagingSortingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PagingSortingApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package spring.boot.core.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户实体类
|
||||
*
|
||||
* Created by bysocket on 18/09/2017.
|
||||
*/
|
||||
@Entity
|
||||
public class User implements Serializable {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 年龄
|
||||
*/
|
||||
private Integer age;
|
||||
|
||||
/**
|
||||
* 出生时间
|
||||
*/
|
||||
private String birthday;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(String birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
", birthday=" + birthday +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package spring.boot.core.domain;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
/**
|
||||
* 用户持久层操作接口
|
||||
*
|
||||
* Created by bysocket on 18/09/2017.
|
||||
*/
|
||||
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package spring.boot.core.service;
|
||||
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import spring.boot.core.domain.User;
|
||||
|
||||
/**
|
||||
* User 业务层接口
|
||||
*
|
||||
* Created by bysocket on 18/09/2017.
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
/**
|
||||
* 获取用户分页列表
|
||||
*
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
Page<User> findByPage(Pageable pageable);
|
||||
|
||||
/**
|
||||
* 新增用户
|
||||
*
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
User insertByUser(User user);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package spring.boot.core.service.impl;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import spring.boot.core.domain.User;
|
||||
import spring.boot.core.domain.UserRepository;
|
||||
import spring.boot.core.service.UserService;
|
||||
|
||||
/**
|
||||
* User 业务层实现
|
||||
*
|
||||
* Created by bysocket on 18/09/2017.
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public Page<User> findByPage(Pageable pageable) {
|
||||
LOGGER.info(" \n 分页查询用户:"
|
||||
+ " PageNumber = " + pageable.getPageNumber()
|
||||
+ " PageSize = " + pageable.getPageSize());
|
||||
return userRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User insertByUser(User user) {
|
||||
LOGGER.info("新增用户:" + user.toString());
|
||||
return userRepository.save(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package spring.boot.core.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import spring.boot.core.domain.User;
|
||||
import spring.boot.core.service.UserService;
|
||||
|
||||
/**
|
||||
* 用户控制层
|
||||
*
|
||||
* Created by bysocket on 18/09/2017.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/users") // 通过这里配置使下面的映射都在 /users
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
UserService userService; // 用户服务层
|
||||
|
||||
/**
|
||||
* 获取用户分页列表
|
||||
* 处理 "/users" 的 GET 请求,用来获取用户分页列表
|
||||
* 通过 @RequestParam 传递参数,进一步实现条件查询或者分页查询
|
||||
*
|
||||
* Pageable 支持的分页参数如下
|
||||
* page - 当前页 从 0 开始
|
||||
* size - 每页大小 默认值在 application.properties 配置
|
||||
*/
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public Page<User> getUserPage(Pageable pageable) {
|
||||
return userService.findByPage(pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
* 处理 "/users" 的 POST 请求,用来获取用户列表
|
||||
* 通过 @RequestBody 绑定实体类参数
|
||||
*/
|
||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
||||
public User postUser(@RequestBody User user) {
|
||||
return userService.insertByUser(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
## 是否显示 SQL 语句
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
## DATA WEB 相关配置 {@link SpringDataWebProperties}
|
||||
## 分页大小 默认为 20
|
||||
spring.data.web.pageable.default-page-size=3
|
||||
## 当前页参数名 默认为 page
|
||||
spring.data.web.pageable.page-parameter=pageNumber
|
||||
## 当前页参数名 默认为 size
|
||||
spring.data.web.pageable.size-parameter=pageSize
|
||||
## 字段排序参数名 默认为 sort
|
||||
spring.data.web.sort.sort-parameter=orderBy
|
||||
@@ -0,0 +1,16 @@
|
||||
package spring.boot.core;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class PagingSortingApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user