JAVA全系列 教程
3762个小节阅读:7092.9k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在秒杀实现类重写查询商品列表方法
xxxxxxxxxx
@Override
public Page<SeckillGoods> findPageByRedis(int page, int size) {
// 1.查询所有秒杀商品列表
List<SeckillGoods> seckillGoodsList = redisTemplate.boundHashOps("seckillGoods").values();
// 2.获取当前页商品列表
// 开始截取索引
int start = (page - 1) * size;
// 结束截取索引
int end = start + page > seckillGoodsList.size() ? seckillGoodsList.size() : start + page;
// 获取当前页结果集
List<SeckillGoods> seckillGoods = seckillGoodsList.subList(start, end);
// 3.构造页面对象
Page<SeckillGoods> seckillGoodsPage = new Page();
seckillGoodsPage.setCurrent(page) // 当前页
.setSize(size) // 每页条数
.setTotal(seckillGoodsList.size()) // 总条数
.setRecords(seckillGoods); // 结果集
return seckillGoodsPage;
}
修改秒杀服务的启动类,添加分页插件
xxxxxxxxxx
@SpringBootApplication
@MapperScan("com.itbaizhan.shopping_seckill_service.mapper")
@EnableScheduling // 开启定时任务
public class ShoppingSeckillServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingSeckillServiceApplication.class, args);
}
// 分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
在秒杀Api模块编写秒杀控制器
xxxxxxxxxx
/**
* 秒杀商品
*/
@RestController
@RequestMapping("/user/seckillGoods")
public class SeckillGoodsController {
@DubboReference
private SeckillService seckillService;
/**
* 用户分页查询秒杀商品
* @param page 页数
* @param size 每页条数
* @return 查询结果
*/
@GetMapping("/findPage")
public BaseResult<Page<SeckillGoods>> findPage(int page, int size){
Page<SeckillGoods> seckillGoodsPage = seckillService.findPageByRedis(page, size);
return BaseResult.ok(seckillGoodsPage);
}
}
测试接口