1. 编写从前端接收的当前页码及每页显示条数的实体类PromotionAdVo
package com.lagou.domain;
public class PromotionAdVO {
//当前页
private Integer currentPage;
//每页显示的条数
private Integer pageSize;
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
2. Service层代码编写PromotionAdService
package com.lagou.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.PromotionAdMapper;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVO;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class PromotionAdServiceImpl implements PromotionAdService {
@Autowired
private PromotionAdMapper promotionAdMapper;
@Override
public PageInfo<PromotionAd> findAllPromotionAdByPage(PromotionAdVO promotionAdVO) {
PageHelper.startPage(promotionAdVO.getCurrentPage(),promotionAdVO.getPageSize());
List<PromotionAd> allPromotionAdByPage = promotionAdMapper.findAllPromotionAdByPage();
PageInfo<PromotionAd> pageInfo = new PageInfo<>(allPromotionAdByPage);
return pageInfo;
}
}
3. WEB层 PromotionAdController编写
package com.lagou.controller;
import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {
@Autowired
private PromotionAdService promotionAdService;
/*
广告分页查询
*/
@RequestMapping("/findAllPromotionAdByPage")
public ResponseResult findAllAdByPage( PromotionAdVO promotionAdVO){
PageInfo<PromotionAd> pageInfo = promotionAdService.findAllPromotionAdByPage(promotionAdVO);
ResponseResult responseResult = new ResponseResult(true, 200, "广告分页查询成功", pageInfo);
return responseResult;
}
}
节选自拉钩教育JAVA培训系列课程