Learning Spring-Data-Jpa (xvii) --- support for Web module

  Spring-Data Web also provides support to the module, which requires Web components Spring-MVC jar package located in the classpath. Usually to enable inheritance support through the use of @EnableSpringDataWebSupport comment.

On SpringDataWebAutoConfiguration has been added @EnableSpringDataWebSupport comments, we do not need additional configuration.

  

A, EnableSpringDataWebSupport will register several components to us:

  1, DomainClassConverter: we can directly use the entity types in the method parameter list Spring-MVC controller, without the need to manually look for instance by Repository (Repository achieve CrudRepository need to be found to be eligible to be converted).

1.1、Controller:

/ ** 
 * 
 * @author caofanqi
  * / 
@RestController 
@ RequestMapping ( "/ the User" )
 public  class the UserController { 

    
    / ** 
     * here will directly call the corresponding User Repository to findById query. 
     * Repository needed to achieve CrudRepository found to be eligible for conversion 
     * / 
    @GetMapping ( "/ {ID}" )
     public the User findUserById (@PathVariable ( "ID" ) the User User) {
         return User; 
    } 
}

1.2, unit testing:

@SpringBootTest
@AutoConfigureMockMvc
class UserControllerTest {

    @Resource
    private MockMvc mockMvc;

    @Test
    void findUserById() throws Exception {

        String result = this.mockMvc.perform(get("/user/47"))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();

        System.out.println(result);

    }
}

  

  2, HandlerMethodArgumentResolvers: We also registered PageableHandlerMethodArgumentResolver and SortHandlerMethodArgumentResolver let Pageable and Sort method can be used as the control layer parameter.

2.1, Pageable layer, or as a control method Sort Parameters

  Parameters page: this page, the default is 0; Parameter size: page size, default 20; parameters sort: the sort direction, if a plurality of different directions using the sort parameters.

    / ** 
     * Parameters page: The default is 0. 
     * Parameters size: The default is 20. 
     * Parameters sort: sort by property, property (, ASC | DESC ) way to fill out, the default is ascending order, if you want to have a different sort direction property, use multiple sort parameters 
     * / 
    @GetMapping 
    public Page <the User> LISTUSER (the Pageable Pageable) { 
        System.out.println ( "Page:" + pageable.getPageNumber ()); 
        System.out.println ( "size:" + pageable.getPageSize ()); 
        System.out.println ( "Sort: "+ pageable.getSort ());
         return userRepository.findAll (Pageable); 
    } 


    @GetMapping ( " / Sort " )
     public  String Sort (the Sort Sort) {
        System.out.println ( " sort);
        return "sort";
    }
    @Test
    void listUser1() throws Exception {
        String result = this.mockMvc.perform(get("/user"))
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();

        System.out.println(result);
    }

    @Test
    void listUser2() throws Exception {
        String result = this.mockMvc.perform(get("/user")
                .param("page","2").param("size","2").param("sort","username,asc"
                .Param ()"sort","age,desc"))
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();

        System.out.println(result);
    }

2.2, or if there are multiple tabs sorting (multiple tables), a @Qualifier to solve, to start with a request parameter $ {qualifier} _

    @GetMapping("/multi/pageable")
    public String multiPageable(@Qualifier("user") Pageable userPageable, @Qualifier("book") Pageable bookPageable){
        System.out.println("userPageable page:" + userPageable.getPageNumber());
        System.out.println("userPageable size:" + userPageable.getPageSize());
        System.out.println("userPageable sort:" + userPageable.getSort());

        System.out.println("bookPageable page:" + bookPageable.getPageNumber());
        System.out.println("bookPageable size:" + bookPageable.getPageSize());
        System.out.println("bookPageable sort:" + bookPageable.getSort());
        return "test";
    }
    @Test
    void multiPageable() throws Exception {
         this.mockMvc.perform(get("/user/multi/pageable")
                .param("user_page","2").param("user_size","2").param("user_sort","username,asc").param("user_sort","age,desc")
                .param("book_page","20").param("book_size","20").param("book_sort","bookName").param("book_sort","price,desc"))
                .andExpect(status().isOk());
    }

  

 

 Second, the use @PageableDefault custom pageable

  We can also customize the default by @PageableDefault

    /**
     * 使用@PageableDefault自定义pageable
     */
    @GetMapping("pageable/default")
    public String pageableDefault(@PageableDefault(page = 2,size = 20,sort = {"username","age"},direction = Sort.Direction.DESC) Pageable pageable){
        System.out.println("page:" + pageable.getPageNumber());
        System.out.println("size:" + pageable.getPageSize());
        System.out.println("sort:" + pageable.getSort());
        return "PageableDefault";
    }
    @Test
    void pageableDefault() throws Exception {
        this.mockMvc.perform(get("/user/pageable/default"))
                .andExpect(status().isOk());
    }

  

 

三、properties中的属性配置

  Spring-Data还为我们提供了一些定制化配置,可以修改常见的是否将1作为第一页,默认是0,定制参数的名字,和每页最大数量等。

#是否将1为第一页
spring.data.web.pageable.one-indexed-parameters=true
#pageable默认每页大小
spring.data.web.pageable.default-page-size=10
#每页大小最大值
spring.data.web.pageable.max-page-size=100
#当前页参数名
spring.data.web.pageable.page-parameter=pageIndex
#每页大小参数名
spring.data.web.pageable.size-parameter=pageSize
#排序参数名
spring.data.web.sort.sort-parameter=pageSort
#page和size参数的前缀,不会影响sort的参数名称
spring.data.web.pageable.prefix=pre
#多参数时分隔符
spring.data.web.pageable.qualifier-delimiter=-

 

 

 

源码地址:https://github.com/caofanqi/study-spring-data-jpa

Guess you like

Origin www.cnblogs.com/caofanqi/p/12051854.html