基于SpringMVC+Spring+MyBatis实现秒杀系统【业务逻辑】

前言

该篇主要实现秒杀业务层,秒杀业务逻辑里主要包括暴露秒杀接口地址、实现秒杀业务逻辑。同时声明了三个业务类:Exposer、SeckillExecution、SeckillResult。 Exposer主要用来实现暴露接口时一个md5的加密,防止用户在客户端篡改数据。根据seckillid生成md5,提交秒杀请求时会根据这个md5和seckillid比对是否是合法的请求。SeckillExecution主要封装秒杀时的返回值。

SeckillExecution有2个属性,state、stateinfo,这里我没有封装枚举值,还是用整型和字符串给客户端传值,在Service里看着也直观些。

准备工作

1、spring-service.xml

业务逻辑里的关键是开启事务,这里推荐用注解的方式实现。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--1、注解包扫描-->
    <context:component-scan base-package="com.seckill.service"/>

    <!--2、配置声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据库-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--3、配置基于注解的声明式事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

实现秒杀业务

秒杀相关的关键方法就是最后两个方法,一个是对外暴漏秒杀地址,一个是秒杀方法。

public interface SeckillService {

    List<Seckill> getSeckillList();

    Seckill getById(long seckillId);

    /**对外暴漏秒杀接口**/
    Exposer exposeSeckillUrl(long seckillId);

    /**
     * 执行秒杀操作,有可能成功,有可能失败,所以这里我们抛出自自定义异常
     * ***/
    SeckillExecution executeSeckill(long seckillId,long phone,String md5) throws SeckillException,
            RepeatKillException,
            SeckillCloseException;

}

  

@Service
public class SeckillServiceImpl implements SeckillService {

    /***
     * 秒杀行为的枚举放在这里说明
     * 1、 秒杀成功
     * 0、 秒杀结束
     * -1、重复秒杀
     * -2、系统异常
     * -3、数据篡改
     * ***/

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SeckillDao seckillDao;

    @Autowired
    SuccessKillDao successKillDao;

    private String salt = "zhangfei";

    @Override
    public List<Seckill> getSeckillList() {
        return seckillDao.queryAll(0, 100);
    }

    @Override
    public Seckill getById(long seckillId) {
        return seckillDao.queryById(seckillId);
    }

    @Override
    public Exposer exposeSeckillUrl(long seckillId) {
        Seckill seckill = getById(seckillId);

        Date startTime = seckill.getStartTime();
        Date endTime = seckill.getEndTime();

        Date now = new Date();

        if (now.getTime() < startTime.getTime() || now.getTime() > endTime.getTime()) {
            return new Exposer(false, seckillId, startTime.getTime(), endTime.getTime(), now.getTime());
        }

        String md5 = getMd5(seckillId);

        return new Exposer(true, md5, seckillId);
    }

    @Override
    @Transactional
    public SeckillExecution executeSeckill(long seckillId, long phone, String md5)
            throws SeckillException,RepeatKillException,SeckillCloseException {

        if (md5 == null || !md5.equals(getMd5(seckillId))) {
            throw new SeckillException("非法请求");
        }

        Date now = new Date();

        try {
            int insertCount = successKillDao.insertSuccessKilled(seckillId, phone);
            if (insertCount <= 0) {
                throw new RepeatKillException("重复秒杀");

            } else {
                int updateCount = seckillDao.reduceNumber(seckillId, now);
                if (updateCount <= 0) {
                    throw new SeckillCloseException("秒杀已关闭");
                } else {
                    //秒杀成功,可以把秒杀详情和商品详情实体返回
                    SuccessKilled successKilled = successKillDao.queryByIdWithSeckill(seckillId, phone);
                    return new SeckillExecution(seckillId, 1, "秒杀成功", successKilled);
                }
            }

        } catch (SeckillCloseException e) {
            throw e;
        } catch (RepeatKillException e1) {
            throw e1;
        } catch (SeckillException e2) {
            logger.error(e2.getMessage(), e2);
            throw new SeckillException("Unkonwn error:" + e2.getMessage());
        }

    }

    private String getMd5(long seckillId) {
        String base = seckillId + "/" + salt;
        String md5 = DigestUtils.md5DigestAsHex(base.getBytes());

        return md5;

    }
}

  

原文地址:https://www.cnblogs.com/sword-successful/p/9230313.html

时间: 2024-08-14 20:25:08

基于SpringMVC+Spring+MyBatis实现秒杀系统【业务逻辑】的相关文章

基于SpringMVC+Spring+MyBatis实现秒杀系统【客户端交互】

前言 该篇主要实现客户端和服务的交互.在第一篇概况里我已经贴出了业务场景的交互图片. 客户端交互主要放在seckill.js里来实现.页面展现基于jsp+jstl来实现. 准备工作 1.配置web.xml.web.xml里配置springmvc前端控制器时需要把spring托管的3个xml全部加载.分别是spring-dao.xml.spring-service.xml.spring-web.xml. <web-app xmlns="http://xmlns.jcp.org/xml/ns/

基于Springmvc+Spring+Mybatis+Jqueryeasyui个人信息管理平台(日程管理、天气类型、资产管理、理财规划)

基于Springmvc+Spring+Mybatis+Jqueryeasyui个人信息管理平台(日程管理.天气类型.资产管理.理财规划) 课程讲师老牛 课程分类Java 适合人群中级 课时数量78课时 更新程度完毕 服务类型C类普通服务类课程 用到技术Springmvcspringmybatisjquery easyui 涉及项目个人信息管理好友管理报表实现 咨询QQ2050339477 课程链接http://www.dwz.cn/LO1X3 课程背景 本系统主要用于个人信息的管理通过软件工具对

课程分享】基于Springmvc+Spring+Mybatis+Bootstrap+jQuery Mobile +MySql教务管理系统

课程讲师:老牛 课程分类:Java框架 适合人群:初级 课时数量:85课时 更新程度:完成 用到技术:Springmvc+Spring+Mybatis+Bootstrap+jQueryMobile 涉及项目:PC端和手机端教务管理系统 需要更多相关资料可以联系 Q2748165793 课程大纲 技能储备 第1课springMVC概述和基础配置 第2课springMVC注解和参数传递 第3课springMVC和JSON数据 第4课springMVC上传下载 第5课springMVC 与 sprin

基于Springmvc+Spring+Mybatis+Bootstrap+jQuery Mobile +MySql教务管理系统(分为PC端和手机端)

刚开始我也不信,可自己根据http://url.cn/TgrIZT注册一下,然后通过这个网站获取了学习卡的用户名和密码之后,真的有200元抵用券到了自己的账户中,所以,我就买了一些课程,自从在北风网学习了一些课程之后,我感觉自己对于提成技能特别高,可能和自己刚刚毕业有关系,在学校每天都是理论知识,没有过多的时间,但是自从在北风网上学习了一些项目的知识之后,尤其和老师们一起做项目,我起初以为只是简单的视频教程,谁知道和培训机构一样,老师还可以给你解答问题,费用只相当于培训机构的10%,后来自己又学

第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十天】(单点登录系统实现)

https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结[第六天] 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)[第七天](redis缓存) 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)[第八天](solr服务器搭建.搜

Idea SpringMVC+Spring+MyBatis+Maven调整【转】

Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetype,然后选中下方列表中的webapp,然后点击Next 在GroupId和ArtifactId中填入指定内容,点击Next 直接点Next 输入项目名称,Finish Idea会自动开始下载所依赖的包,等待其完成. 项目结构 项目刚建好的时候是没有这些文件的,所以自己手动创建缺少的文件夹(包) 创建完后

SpringMVC+Spring+Mybatis(SSM~Demo) 【转】

SpringMVC+Spring+Mybatis 框架搭建 整个Demo的视图结构: JAR: 下载地址:http://download.csdn.net/detail/li1669852599/8546059 首先,我是使用MyEclipse工具做的这个例子,整合了Sping 3 .Spring MVC 3 .MyBatis框架,演示数据库采用MySQL数据库.例子中主要操作包括对数据的添加(C).查找(R).更新(U).删除(D).我在这里采用的数据库连接池是来自阿里巴巴的Druid,至于D

第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)

https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结[第五天] 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结[第六天] 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)[第七天](redis缓存) 第04

springmvc+spring+mybatis+maven项目构建

1.首先在myeclipse10中安装maven的插件,将插件放入D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropins\maven中, 2. 新建文件:maven.link填入如下内容:path=D:/Program Files (x86)/myEclipse10/MyEclipse Blue Edition 10/dropins/maven 3.重启myeclipse插件安装成功. 4.在myeclipse10