SpringBoot分页条件查询操作
UserQuery
package com.xxxx.springboot.query;
public class UserQuery {
private Integer pageNum = 1;//当前页
private Integer pageSize = 10;//每页显示的记录数量
private String userName;//查询条件:用户名
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
接口方法定义
package com.xxxx.springboot.dao;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.query.UserQuery;
import java.util.List;
public interface UserDao {
//分页条件查询
public List queryUserList(UserQuery userQuery);
}
映射文件配置
select
*
from
student
and user_name like concat('%',#{userName},'%')
UserService
package com.xxxx.springboot.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.springboot.dao.UserDao;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.query.UserQuery;
import com.xxxx.springboot.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
private UserDao userDao;
public PageInfo queryUserByParams(UserQuery userQuery) {
//开启分页
PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
//返回分页对象
return new PageInfo(userDao.queryUserList(userQuery));
}
}
UserController
package com.xxxx.springboot.controller;
import com.github.pagehelper.PageInfo;
import com.xxxx.springboot.exception.ParamsException;
import com.xxxx.springboot.po.ResultInfo;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.query.UserQuery;
import com.xxxx.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("user/list")
public PageInfo list(UserQuery userQuery) {
return userService.queryUserByParams(userQuery);
}
}
浏览器测试