mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
数据分页排序案例
This commit is contained in:
@@ -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