使用Spring Data实现数据的查询、分页及排序功能

使用Spring框架自带的Spring Data模块可以简单方便地实现数据的查询、分页及排序功能,请看以下示例:

pom.xml

<dependency>
    <groupId>org.springframework.datagroupId>
    <artifactId>spring-data-commonsartifactId>
    <version>2.1.8.RELEASEversion>
dependency>

mapper

注意这里需要固定的返回值类型:Page

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public class UserMapper {

    Page<UserVo> getVoList(UserVo userVo, Pageable pageable);
}

mapper.xml

注意配对:_Count

<select id="getVoList" resultMap="voMap">
    select id, code, name, email, tel, address from t_user
select>

<select id="getVoList_Count" resultType="java.lang.Long">
    select count(*) from t_user
select>

controller

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/list")
    public Page<UserVo> getVoList(UserVo userVo, Pageable pageable) {
        return userService.getVoList(userVo, pageable);
    }
} 

api

/user/list?code=10001&page=0&size=10&sort=update_time,desc

你可能感兴趣的:(Java,Coding)