栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot分页条件查询操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot分页条件查询操作

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);
    }

}

浏览器测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/531033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号