使用SpringBoot/Restful的Datagrid查操作.

本文详细介绍如何在项目中实现Datagrid的分页查询功能,包括引入通用mapper坐标、修改Datagrid请求设置、创建返回参数格式的POJO类以及实现查询功能的Controller和服务层代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Datagrid分页查询

1.首先在项目引入通用mapper坐标:

<!--pagehelper:通用Mapper分页-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

2.修改页面中Datagrid请求的路径和请求方式:

// 收派标准信息表格
$('#grid').datagrid( {
 	method : "get",           //Restful中的查请求方式为get.
    url : "/standard",
    iconCls : 'icon-forward',
    fit : true,
    border : false,
    rownumbers : true,
    striped : true,
    pageList: [3,5,100],
    pagination : true,
    toolbar : toolbar,
    idField : 'id',
    columns : columns
});

3.创建一个pojo类,目的是迎合Datagrid所需的返回参数的格式.

public class DataGridResult<T> {   //T  --  泛型,代表的是传过来的是什么类型就返回什么类型
    private Long total;
    private List<T> rows;
   /* get/set方法.....*/
}

4.查询功能:Controller

/**
  * 分页查找
  * @Param page:第几页数据
  * @Param rows:每页条数
  **/
@GetMapping
public ResponseEntity<DataGridResult<Standard>> queryStandard(Integer page,Integer rows){
    try {
        // 查询取派标准
        DataGridResult<Standard> result = this.standardService.queryStandardByPage(page, rows);
        if(result != null){
            // 资源存在,返回200
            return new ResponseEntity<>(result, HttpStatus.OK);
        }
        // 资源不存在,返回404
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (Exception e) {
        e.printStackTrace();
        // 出现异常,返回500
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
  1. 查询功能:Service

    public DataGridResult queryStandardByPage(Integer page, Integer rows) {
    // 使用分页助手开始分页,指定两个参数:当前页码,每页条数
    PageHelper.startPage(page, rows);
    // 然后分页拦截器会自动对接下来的查询进行分页
    List standards = this.standardMapper.select(null);// 不传查询条件
    // 封装分页信息对象
    PageInfo pageInfo = new PageInfo<>(standards);
    // 封装页面数据对象
    DataGridResult result = new DataGridResult(pageInfo.getTotal(), standards);
    return result;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值