使用pagehelper进行分页处理

导入依赖:


     com.github.pagehelper
     pagehelper-spring-boot-starter
     1.1.1
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 bacMap = new HashMap<>();
        PageHelper.startPage(pageNum, 20); //设置起始页,每页条数
        try {
            Page 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 findShopsByPage();
  @Select("Select * From station s Where s.sid = #{stationId} ")
  List 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 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 getStations() {
        return stations;
    }

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

    

你可能感兴趣的:(mybatis)