Skip to content

Commit 6969fcc

Browse files
JeffLi1993liqiangqiang
authored andcommitted
数据分页排序案例
1 parent 2949e09 commit 6969fcc

File tree

9 files changed

+330
-0
lines changed

9 files changed

+330
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>spring.boot.core</groupId>
7+
<artifactId>chapter-5-spring-boot-paging-sorting</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>chapter-5-spring-boot-paging-sorting</name>
12+
<description>第五章数据分页排序案例</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.M4</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
29+
<!-- Web 依赖 -->
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<!-- 单元测试依赖 -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-data-jpa</artifactId>
46+
</dependency>
47+
48+
<!-- h2 数据源连接驱动 -->
49+
<dependency>
50+
<groupId>com.h2database</groupId>
51+
<artifactId>h2</artifactId>
52+
<scope>runtime</scope>
53+
</dependency>
54+
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<!-- Spring Boot Maven 插件 -->
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
<version>1.5.1.RELEASE</version>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
<repositories>
69+
<repository>
70+
<id>spring-milestones</id>
71+
<name>Spring Milestones</name>
72+
<url>https://repo.spring.io/libs-milestone</url>
73+
<snapshots>
74+
<enabled>false</enabled>
75+
</snapshots>
76+
</repository>
77+
</repositories>
78+
79+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package spring.boot.core;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* 应用启动程序
8+
*
9+
* Created by bysocket on 18/09/2017.
10+
*/
11+
@SpringBootApplication
12+
public class PagingSortingApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(PagingSortingApplication.class, args);
16+
}
17+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package spring.boot.core.domain;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import java.io.Serializable;
7+
8+
/**
9+
* 用户实体类
10+
*
11+
* Created by bysocket on 18/09/2017.
12+
*/
13+
@Entity
14+
public class User implements Serializable {
15+
16+
/**
17+
* 编号
18+
*/
19+
@Id
20+
@GeneratedValue
21+
private Long id;
22+
23+
/**
24+
* 名称
25+
*/
26+
private String name;
27+
28+
/**
29+
* 年龄
30+
*/
31+
private Integer age;
32+
33+
/**
34+
* 出生时间
35+
*/
36+
private String birthday;
37+
38+
public Long getId() {
39+
return id;
40+
}
41+
42+
public void setId(Long id) {
43+
this.id = id;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public Integer getAge() {
55+
return age;
56+
}
57+
58+
public void setAge(Integer age) {
59+
this.age = age;
60+
}
61+
62+
public String getBirthday() {
63+
return birthday;
64+
}
65+
66+
public void setBirthday(String birthday) {
67+
this.birthday = birthday;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "User{" +
73+
"id=" + id +
74+
", name='" + name + '\'' +
75+
", age=" + age +
76+
", birthday=" + birthday +
77+
'}';
78+
}
79+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package spring.boot.core.domain;
2+
3+
import org.springframework.data.repository.PagingAndSortingRepository;
4+
5+
/**
6+
* 用户持久层操作接口
7+
*
8+
* Created by bysocket on 18/09/2017.
9+
*/
10+
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
11+
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package spring.boot.core.service;
2+
3+
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import spring.boot.core.domain.User;
7+
8+
/**
9+
* User 业务层接口
10+
*
11+
* Created by bysocket on 18/09/2017.
12+
*/
13+
public interface UserService {
14+
15+
/**
16+
* 获取用户分页列表
17+
*
18+
* @param pageable
19+
* @return
20+
*/
21+
Page<User> findByPage(Pageable pageable);
22+
23+
/**
24+
* 新增用户
25+
*
26+
* @param user
27+
* @return
28+
*/
29+
User insertByUser(User user);
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package spring.boot.core.service.impl;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.stereotype.Service;
9+
import spring.boot.core.domain.User;
10+
import spring.boot.core.domain.UserRepository;
11+
import spring.boot.core.service.UserService;
12+
13+
/**
14+
* User 业务层实现
15+
*
16+
* Created by bysocket on 18/09/2017.
17+
*/
18+
@Service
19+
public class UserServiceImpl implements UserService {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
22+
23+
@Autowired
24+
UserRepository userRepository;
25+
26+
@Override
27+
public Page<User> findByPage(Pageable pageable) {
28+
LOGGER.info(" \n 分页查询用户:"
29+
+ " PageNumber = " + pageable.getPageNumber()
30+
+ " PageSize = " + pageable.getPageSize());
31+
return userRepository.findAll(pageable);
32+
}
33+
34+
@Override
35+
public User insertByUser(User user) {
36+
LOGGER.info("新增用户:" + user.toString());
37+
return userRepository.save(user);
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package spring.boot.core.web;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.web.bind.annotation.*;
7+
import spring.boot.core.domain.User;
8+
import spring.boot.core.service.UserService;
9+
10+
/**
11+
* 用户控制层
12+
*
13+
* Created by bysocket on 18/09/2017.
14+
*/
15+
@RestController
16+
@RequestMapping(value = "/users") // 通过这里配置使下面的映射都在 /users
17+
public class UserController {
18+
19+
@Autowired
20+
UserService userService; // 用户服务层
21+
22+
/**
23+
* 获取用户分页列表
24+
* 处理 "/users" 的 GET 请求,用来获取用户分页列表
25+
* 通过 @RequestParam 传递参数,进一步实现条件查询或者分页查询
26+
*
27+
* Pageable 支持的分页参数如下
28+
* page - 当前页 从 0 开始
29+
* size - 每页大小 默认值在 application.properties 配置
30+
*/
31+
@RequestMapping(method = RequestMethod.GET)
32+
public Page<User> getUserPage(Pageable pageable) {
33+
return userService.findByPage(pageable);
34+
}
35+
36+
/**
37+
* 创建用户
38+
* 处理 "/users" 的 POST 请求,用来获取用户列表
39+
* 通过 @RequestBody 绑定实体类参数
40+
*/
41+
@RequestMapping(value = "/create", method = RequestMethod.POST)
42+
public User postUser(@RequestBody User user) {
43+
return userService.insertByUser(user);
44+
}
45+
46+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## 是否显示 SQL 语句
2+
spring.jpa.show-sql=true
3+
4+
## DATA WEB 相关配置 {@link SpringDataWebProperties}
5+
## 分页大小 默认为 20
6+
spring.data.web.pageable.default-page-size=3
7+
## 当前页参数名 默认为 page
8+
spring.data.web.pageable.page-parameter=pageNumber
9+
## 当前页参数名 默认为 size
10+
spring.data.web.pageable.size-parameter=pageSize
11+
## 字段排序参数名 默认为 sort
12+
spring.data.web.sort.sort-parameter=orderBy
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package spring.boot.core;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class PagingSortingApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)