使用pagehelper进行分页处理

导入依赖:

<dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.1.1</version>
</dependency>
package com.provider.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.provider.mapper.ShopMapper;
import com.provider.utils.JsonUtils;
import com.provider.vo.ShopStation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/*
         *  author  :  ajing
         *  date    :  2018/4/8 10:07
         * */
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private ShopMapper shopMapper;

    @RequestMapping(value = "/getShopList", method = RequestMethod.POST)
    public String query( @RequestParam(value = "pageNum",defaultValue = "1") int pageNum ) {
        Map<String, Object> bacMap = new HashMap<>();
        PageHelper.startPage(pageNum, 20); //设置起始页,每页条数
        try {
            Page<ShopStation> shopList = this.shopMapper.findShopsByPage();  //返回结果为Page对象
            bacMap.put("data", new PageInfo<>(shopList)); //返回给前端要封装成PageInfo类型
            bacMap.put("code", 0);
            bacMap.put("msg", "success");
            return JsonUtils.writeValueAsString(bacMap);
           
        } catch (Exception e) {
            bacMap.put("data", null);
            bacMap.put("code", 5);
            bacMap.put("msg", "失败");
        }
        return JsonUtils.writeValueAsString(bacMap);
    }
}

mapper:

    @Select({"Select * ,ss.station_id From shop_station ss ,station s,shop sh " +
            "where ss.station_id = s.sid  and ss.shopid=sh.id " })
    @Results({
            @Result(property = "name", column = "shop_name"),
            @Result(property = "stations", column = "station_id", many = @Many(select = "com.provider.mapper.ShopMapper.findStations"))
    })
    Page<ShopStation> findShopsByPage();
  @Select("Select * From station s Where s.sid = #{stationId} ")
  List<Station> findStations(@Param("stationId") Long stationId);

解释:

 @Result(property = "stations", column = "station_id", many = @Many(select = "com.provider.mapper.ShopMapper.findStations"))
这是根据查询结果得到的station_id 来进行二次查询获取关联对象的集合。many=@Many表示结果集为list。one=@One则结果为一个。

shopstion:

package com.ycb.app.provider.vo;

import java.util.List;

/**
 * Created by zhuhui on 17-7-28.
 */
public class ShopStation extends BaseEntity {

    // 商铺站点名
    private String name;
    // 地址
    private String address;
    // 设备
    private List<Station> stations;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


    public List<Station> getStations() {
        return stations;
    }

    public void setStations(List<Station> stations) {
        this.stations = stations;
    }

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值