先添加配置
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybaticPlusPageConfig { /** * 旧版的操作 */ // @Bean // public PaginationInterceptor paginationInterceptor(){ // return new PaginationInterceptor(); // } /** * 新版的分页插件配置 * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
这里给一个例子,操作也比较流畅
Controller
import net.xdclass.utils.JsonData; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import net.xdclass.service.CouponService; import org.springframework.beans.factory.annotation.Autowired; 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 java.util.Map; @RestController @RequestMapping("/api/v1/coupon") @Api("优惠券模块") public class CouponController { @Autowired private CouponService couponService; @ApiOperation("分页查询优惠券") @GetMapping("page_coupon") public JsonData pageCouponList( @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page, @ApiParam(value = "每页显示多少条") @RequestParam(value = "size", defaultValue = "10") int size ) { Map<String, Object> pageMap = couponService.pageCouponActivity(page, size); return JsonData.buildSuccess(pageMap); } }
Service
public interface CouponService extends IService<CouponDO> { /** * 分页查询优惠券 * @param page * @param size * @return */ Map<String,Object> pageCouponActivity(int page, int size); }
Impl
import net.xdclass.enums.CouponCategoryEnum; import net.xdclass.enums.CouponPublishEnum; import net.xdclass.model.CouponDO; import net.xdclass.mapper.CouponMapper; import net.xdclass.service.CouponService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import net.xdclass.vo.CouponVO; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import net.xdclass.mapper.CouponMapper; import net.xdclass.model.CouponDO; import net.xdclass.service.CouponService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; @Service @Slf4j public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> implements CouponService { @Autowired private CouponMapper couponMapper; @Override public Map<String, Object> pageCouponActivity(int page, int size) { Page<CouponDO> pageInfo = new Page<>(page,size); IPage<CouponDO> couponDOIPage = couponMapper.selectPage(pageInfo, new QueryWrapper<CouponDO>() //优惠券状态 比如说什么下架呀,正在上架等 .eq("publish", CouponPublishEnum.PUBLISH) //优惠券类型 如:新人卷,任务卷,营销卷等 .eq("category", CouponCategoryEnum.PROMOTION) //简单的排序 降序 .orderByDesc("create_time")); Map<String,Object> pageMap = new HashMap<>(3); //总条数 pageMap.put("total_record", couponDOIPage.getTotal()); //总页数 pageMap.put("total_page",couponDOIPage.getPages()); //因为我们不想要将所有东西发出去给用户,所以这里进行转换一下 pageMap.put("current_data",couponDOIPage.getRecords().stream().map(obj->beanProcess(obj)).collect(Collectors.toList())); return pageMap; } private CouponVO beanProcess(CouponDO couponDO) { CouponVO couponVO = new CouponVO(); BeanUtils.copyProperties(couponDO,couponVO); return couponVO; } }
Enum
public enum CouponCategoryEnum { /** * 新人注册 */ NEW_USER, /** * 活动任务 */ TASK, /** * 促销劵 */ PROMOTION }
/** * 优惠券状态 */ public enum CouponPublishEnum { /** * 线上 */ PUBLISH, /** * 草稿 */ DRAFT, /** * 下线 */ OFFLINE }
VO
@Data public class CouponVO { /** * id */ private Long id; /** * 优惠卷类型[NEW_USER注册赠券,TASK任务卷,PROMOTION促销劵] */ private String category; /** * 优惠券图片 */ @JsonProperty("coupon_img") private String couponImg; /** * 优惠券标题 */ @JsonProperty("coupon_title") private String couponTitle; /** * 抵扣价格 */ private BigDecimal price; /** * 每人限制张数 */ @JsonProperty("user_limit") private Integer userLimit; /** * 优惠券开始有效时间 */ @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",locale = "zh",timezone = "GMT+8") @JsonProperty("start_time") private Date startTime; /** * 优惠券失效时间 */ @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",locale = "zh",timezone = "GMT+8") @JsonProperty("end_time") private Date endTime; /** * 优惠券总量 */ @JsonProperty("publish_count") private Integer publishCount; /** * 库存 */ private Integer stock; /** * 满多少才可以使用 */ @JsonProperty("condition_price") private BigDecimal conditionPrice; }
本文作者为DBC,转载请注明。