電商專案實戰
阿新 • • 發佈:2020-12-27
SpringBoot+MyBatisPlus實現分頁功能
前言
本片文章僅為了本人整理思路,總結經驗所寫。
(作為剛入行的新人,大佬看到勿噴。)
專案框架
基於SpringBoot + MyBatisPlus實現分頁功能。話不多說,下面直接上程式碼。(其餘的別問我,問就是不會。)
第一步
封裝一個實體類,包含分頁所需要的引數
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AgentApplyResponse<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty (name = "currentPage", value = "當前頁")
private Long currentPage;
@ApiModelProperty(name = "size", value = "獲取每頁顯示條數")
private Long size;
@ApiModelProperty(name = "total", value = "當前滿足條件總行數")
private Long total;
@ApiModelProperty (name = "pages", value = "當前頁面總數")
private Long pages;
@ApiModelProperty(name = "list", value = "分頁記錄列表")
private List<T> list;
}
第二步
一個轉換類,把分頁需要的引數,轉換成想要的資料形式返回。
import com.flashsal.dto.FlashSaleDto;
import com.flashsal.entity.FlashsaleActivity;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface FlashConvert {
FlashConvert CONVERT = Mappers.getMapper(FlashConvert.class);
List<FlashSaleDto> proxy(List<FlashsaleActivity> flashsaleActivity);
FlashSaleDto flashSaleDto(FlashsaleActivity flashsaleActivity);
}
還有分頁需要的service。
public interface FlashConfigService {
AgentApplyResponse select(Integer currentPage, Integer size);
}
第三步
實現上步service
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.agent.response.AgentApplyResponse;
import com.flashsal.convert.FlashConvert;
import com.flashsal.dto.FlashSaleDto;
import com.flashsal.entity.FlashsaleActivity;
import com.flashsal.mapper.FlashConfigMapper;
import com.flashsal.mapper.ProductSpuMapper;
import com.flashsal.service.FlashConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
@Slf4j
public class FlashConfigServiceImpl implements FlashConfigService {
@Resource
private FlashConfigMapper flashConfigMapper;
@Resource
private ProductSpuMapper productSpuMapper;
@Override
public AgentApplyResponse select(Integer currentPage, Integer size) {
log.info("current{}=" + currentPage + "size{}" + size);
IPage<FlashsaleActivity> flashSaleDtoIPage = new Page<>(currentPage, size);
LambdaQueryWrapper<FlashsaleActivity> flashWrapper = new LambdaQueryWrapper<>();
IPage<FlashsaleActivity> selectPage = flashConfigMapper.selectPage(flashSaleDtoIPage, flashWrapper);
//這是你查到資料 遍歷這個 list 能取到資料
//1 查詢到資料 轉換成想要的返回型別 2 在new一個AgentApplyResponse 設定分頁 把資料裝進去
List<FlashsaleActivity> records = selectPage.getRecords();
AgentApplyResponse response = new AgentApplyResponse<>();
response.setPages(selectPage.getPages());
response.setSize(selectPage.getSize());
response.setTotal(selectPage.getTotal());
response.setCurrentPage(selectPage.getCurrent());
List<FlashSaleDto> flashSaleDtos = FlashConvert.CONVERT.proxy(records);
for (int i = 0; i < records.size(); i++) {
FlashsaleActivity flashsaleActivity = records.get(i);
String spuId = flashsaleActivity.getSpuId();
LambdaQueryWrapper<FlashsaleActivity> activity = new LambdaQueryWrapper<FlashsaleActivity>()
.eq(FlashsaleActivity::getSpuId,spuId)
.eq(FlashsaleActivity::getIsDelete,0);
FlashsaleActivity fs = flashConfigMapper.selectOne(activity);
FlashSaleDto saleDto = flashSaleDtos.get(i);
saleDto.setSpuId(fs.getSpuId());
saleDto.setSpuName(productSpuMapper.selectSpuName(spuId));
saleDto.setSpuLogo(productSpuMapper.selectSpuLogo(spuId));
saleDto.setFakeTotal(fs.getRoundNumber());
saleDto.setStartTime(fs.getStartTime());
saleDto.setRoundNumber(fs.getRoundNumber());
saleDto.setDuration(fs.getDuration());
saleDto.setStartingPrice(fs.getStartingPrice());
saleDto.setStride(fs.getStride());
saleDto.setStatus(fs.getStatus());
}
response.setList(flashSaleDtos);
log.info("{}" + response);
return response;
}
}
最後一步
寫Api介面
import com.admin.common.exception.BaseApiException;
import com.admin.common.result.ApiResult;
import com.agent.response.AgentApplyResponse;
import com.domain.sys.AdminEnumStatus;
import com.flashsal.service.FlashConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Api(tags = "商品管理")
@Slf4j
@RestController
@RequestMapping("/flashsale")
public class FlashConfigApi {
@Resource
private FlashConfigService flashConfigService;
@GetMapping("/select")
@ApiOperation(value = "分頁獲取商品資訊", notes = "")
public ApiResult select(@RequestParam("currentPage") Integer currentPage, @RequestParam("size") Integer size) {
try {
AgentApplyResponse response = flashConfigService.select(currentPage, size);
return ApiResult.ok(response);
} catch (Exception e) {
log.error("異常:分頁展示異常", e);
if (e instanceof BaseApiException) {
return ApiResult.error(((BaseApiException) e).getCode(), ((BaseApiException) e).getMsg());
}
return ApiResult.error(AdminEnumStatus.SYSTEM_ERROR.getResultCode(), AdminEnumStatus.SYSTEM_ERROR.getResultMsg());
}
}
}
以上就是分頁大致思路整理。作為新手確實還有很多不懂的地方,並且程式碼也可能寫的非常low,還請看到的大佬們勿噴,歡迎評論不吝賜教!