springboot项目之数据库分页SQL实现

在面对大批量数据查询的时候,如果一次性全部查出来的话,即便是查询方法再怎么做优化,都会遇到数据量大这个瓶颈,有时候一次查询会非常耗时,在web项目中甚至会造成查询超时,进而会影响到用户使用体验。这个时候就要用到分页查询,避免大量数据一次性查询造成阻塞。下面基于springboot项目介绍使用SQL分页,和springmvc中GET方法传递参数,完成前后端数据分页的查询。建议入门级小白阅读,大神请略过。

项目环境:springboot + mybatis + postgresql

使用SQL分页,主要就是两个参数,一个是pageSize,另一个是pageIndex,分别代表分页的每一页有多少条数据和页码的索引,也就是第几页。

SQL分页

mapper.xml文件中SQL代码为:

<!-- 分页查询(无查询条件) 根据id倒序排列 -->
<select id="pagingSelectByUnconditional" resultType="meicius.ori.pojo.Selary ">
         (SELECT * FROM "base".selary limit ${pageSize} OFFSET ${index}) order by id desc ;
</select>

mapper (dao)对应接口中的代码为:

public List<Selary> pagingSelectByUnconditional(@Param("pageSize") int pageSize, @Param("index") int index);

service接口和实现类中对应代码为:

//service接口
public List<Selary> pagingSelectByUnconditional(@Param("pageSize") int pageSize, @Param("index") int index);
 
//service实现类
@Override
public List<Selary> pagingSelectByUnconditional(int pageSize, int index) {
        return selaryMapper.pagingSelectByUnconditional(pageSize, index);
}

这样就可以在controller中查询出该页码上的数据了。但是这样并不是完整的,因为这样只返回了当前页的数据,还需要返回一共有多少条数据,所以还需要再进行一次有多少条数据的SQL查询。这里只给出查询的SQL语句:

SELECT COUNT(id) FROM "base".Selary;

将查询总数返回的数据和查询该页的数据一起返回给前端页面。

springmvc GET方法传递参数查询

在controller中使用GET方法查询,pageSize和pageIndex(代码中我使用index)参数写在url中。

controller中接收参数有不同的方式,但是基本都是通过不同的注解完成。下面逐一介绍:

1)@RequestMapping注解

@RequestMapping(value= "/pagingselectbyunconditional", method = RequestMethod.GET)
public Result pagingSelectByUnconditional(int pageSize, int index){
			Map<String, Object> stringObjectMap = new HashMap<String, Object>();
       try{
             stringObjectMap.put("total",
             selaryImpl.getselaryTableCount());
             stringObjectMap.put("result", selaryImpl.pagingSelectByUnconditional(pageSize, index));
             stringObjectMap.put("pageSize", pageSize);
             stringObjectMap.put("pageIndex", index);
             result.returnSuccess(stringObjectMap);
       }catch (Exception e){
           result.returnError();
       }finally {
           return result;
       }

这里直接在方法的形参中写入参数,此时url为:

http://localhost:ip/pagingselectbyunconditional?pageSize=10&index=0

可以直接解析出url中的pageSize和index

2)@RequestParams注解

@RequestMapping(value = "/pagingselectbyunconditional", method = RequestMethod.GET)
public Result pagingSelectByUnconditional(@RequestParam("pageSize") int pg, @RequestParam("index") int in){ 
  Map<String, Object> stringObjectMap = new HashMap<String, Object>();
  try{
  		  stringObjectMap.put("total", selaryImpl.getselaryTableCount());
          stringObjectMap.put("result", selaryImpl.pagingSelectByUnconditional(pageSize, index));
          stringObjectMap.put("pageSize", pageSize);
          stringObjectMap.put("pageIndex", index);
          result.returnSuccess(stringObjectMap);
       }catch (Exception e){
            result.returnError();
       }finally {
            return result;
       }
}

@RequestParams注解会解析url中的pageSize和index,

此时url为:http://localhost:ip/pagingselectbyunconditional?pageSize=10&index=0

注意@RequestParams括号中的参数一定要和url中的对应。

3)@PathVariable注解

@RequestMapping(value = "/pagingSelectByUnconditional/{pageSize}/{index}", method = RequestMethod.GET) 
public Result pagingSelectByUnconditional(@PathVariable ("pageSize") int pg, @PathVariable("index") int in){
  Map<String, Object> stringObjectMap = new HashMap<String, Object>();
  try{
            stringObjectMap.put("total", selaryImpl.getselaryTableCount());
            stringObjectMap.put("result", selaryImpl.pagingSelectByUnconditional(pageSize, index));
            stringObjectMap.put("pageSize", pageSize);
            stringObjectMap.put("pageIndex", index);
            result.returnSuccess(stringObjectMap);
        }catch (Exception e){
            result.returnError();
        }finally {
            return result;
        }
}

@PathVariable会按顺序解析url中的参数,此时url为:

http://lcoalhost:ip/ori/orbitczmlpath/pagingselectbyunconditional/10/0

注意此时@RequestMapping中的value

最后附上一张请求成功的图片

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值