javaweb商城秒杀系统--秒杀功能的实现(四)

本文详细介绍了秒杀系统的实现过程,包括秒杀页面的代码、控制器的逻辑处理,如库存检查、重复秒杀判断、减库存下单以及秒杀订单信息的页面展示,并深入解析了服务层的具体操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、秒杀页面的代码
秒杀的部分界面代码
二、秒杀的controller的实现
1.先判断库存是否还存在
2.判断是不是已经秒杀过了
3.库存还有,也没有下过订单然后就是 减库存 下订单 写入秒杀订单
4.将秒杀后的订单信息直接写入页面

 @RequestMapping("/do_miaosha")
    public String doMiaosha(Model model, MiaoshaUser user, @RequestParam("goodsId") Long goodsId){

        model.addAttribute("user",user);
        if (user==null){
            return "login";
        }
     //先判断库存是否还存在
        GoodsVo goodsVo=goodsService.getGoosVoByGoodsId(goodsId);
        int stockCount=goodsVo.getStockCount();
        if(stockCount<=0){
            model.addAttribute("errmsg", CodeMsg.MIAO_SHA_OVER);
            return "miaosha_fail";
        }
        //判断是不是已经秒杀过了
     MiaoshaOrder miaoshaOrder=orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(),goodsId);
        if(miaoshaOrder!=null){
            model.addAttribute("errmsg", CodeMsg.REPEATE_MIAOSHA);
            return "miaosha_fail";
        }
        //库存还有,也没有下过订单然后就是 减库存 下订单 写入秒杀订单
        //因为要满足事务的操作  所以 建一个单独的service

     OrderInfo orderInfo= miaoshaService.miaosha(user,goodsVo);

        //将秒杀后的订单信息直接写入页面
        model.addAttribute("orderInfo",orderInfo);
        //显示商品信息
        model.addAttribute("goods",goodsVo);

        return "order_detail";

三、秒杀的service的实现

/**
 * @Author:zhangyx
 * @Date:Created in 12:492018/11/17
 * @Modified By:
 */
@Service
public class MiaoshaService {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;

   //做秒杀的操作
    @Transactional
    public OrderInfo miaosha(MiaoshaUser user, GoodsVo goodsVo) {
        //减少库存
        goodsService.reduceStock(goodsVo);
        //写订单
        return orderService.createOrder(user,goodsVo);
    }
}
java实现秒杀系统@Controller @RequestMapping("seckill")//url:/模块/资源/{id}/细分 /seckill/list public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method = RequestMethod.GET) public String list(Model model){ //获取列表页 List list=seckillService.getSeckillList(); model.addAttribute("list",list); //list.jsp+model = ModelAndView return "list";//WEB-INF/jsp/"list".jsp } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model){ if (seckillId == null){ return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); if (seckill == null){ return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax json @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult exposer(@PathVariable("seckillId") Long seckillId){ SeckillResult result; try { Exposer exposer =seckillService.exportSeckillUrl(seckillId); result = new SeckillResult(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); result = new SeckillResult(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"} ) @ResponseBody public SeckillResult execute(@PathVariable("seckillId")Long seckillId,
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值