diff --git a/chapter-5-spring-boot-paging-sorting/pom.xml b/chapter-5-spring-boot-paging-sorting/pom.xml new file mode 100644 index 0000000..77efa6a --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + spring.boot.core + chapter-5-spring-boot-paging-sorting + 0.0.1-SNAPSHOT + jar + + chapter-5-spring-boot-paging-sorting + 第五章数据分页排序案例 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.M4 + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + com.h2database + h2 + runtime + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.1.RELEASE + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone + + false + + + + + diff --git a/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/PagingSortingApplication.java b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/PagingSortingApplication.java new file mode 100644 index 0000000..702f57a --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/PagingSortingApplication.java @@ -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); + } +} diff --git a/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/User.java b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/User.java new file mode 100644 index 0000000..1badbf8 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/User.java @@ -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 + + '}'; + } +} diff --git a/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/UserRepository.java b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/UserRepository.java new file mode 100644 index 0000000..aad8278 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/domain/UserRepository.java @@ -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 { + +} diff --git a/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/UserService.java b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/UserService.java new file mode 100644 index 0000000..ccc4b18 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/UserService.java @@ -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 findByPage(Pageable pageable); + + /** + * 新增用户 + * + * @param user + * @return + */ + User insertByUser(User user); +} diff --git a/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/impl/UserServiceImpl.java b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..6510b52 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/service/impl/UserServiceImpl.java @@ -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 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); + } +} diff --git a/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/web/UserController.java b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/web/UserController.java new file mode 100644 index 0000000..bba3d10 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/java/spring/boot/core/web/UserController.java @@ -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 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); + } + +} \ No newline at end of file diff --git a/chapter-5-spring-boot-paging-sorting/src/main/resources/application.properties b/chapter-5-spring-boot-paging-sorting/src/main/resources/application.properties new file mode 100644 index 0000000..133a1b7 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/chapter-5-spring-boot-paging-sorting/src/test/java/spring/boot/core/PagingSortingApplicationTests.java b/chapter-5-spring-boot-paging-sorting/src/test/java/spring/boot/core/PagingSortingApplicationTests.java new file mode 100644 index 0000000..c89f055 --- /dev/null +++ b/chapter-5-spring-boot-paging-sorting/src/test/java/spring/boot/core/PagingSortingApplicationTests.java @@ -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() { + } + +}