前言

该篇主要实现秒杀业务层,秒杀业务逻辑里主要包括暴露秒杀接口地址、实现秒杀业务逻辑。同时声明了三个业务类: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; }
}

  

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

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

    前言 该篇主要实现客户端和服务的交互.在第一篇概况里我已经贴出了业务场景的交互图片. 客户端交互主要放在seckill.js里来实现.页面展现基于jsp+jstl来实现. 准备工作 1.配置web.x ...

  2. 基于SpringMVC+Spring+MyBatis实现秒杀系统【概况】

    前言 本教程使用SpringMVC+Spring+MyBatis+MySQL实现一个秒杀系统.教程素材来自慕课网视频教程[https://www.imooc.com/learn/631].有感兴趣的可 ...

  3. 基于SpringMVC+Spring+MyBatis实现秒杀系统【数据库接口】

    前言 该篇教程主要关注MyBatis实现底层的接口,把MyBatis交给Spring来托管.数据库连接池用的c3p0.数据库用的MySQL.主要有2个大类:秒杀商品的查询.秒杀明细的插入. 准备工作 ...

  4. 手把手教你使用VUE+SpringMVC+Spring+Mybatis+Maven构建属于你自己的电商系统之vue后台前端框架搭建——猿实战01

            猿实战是一个原创系列文章,通过实战的方式,采用前后端分离的技术结合SpringMVC Spring Mybatis,手把手教你撸一个完整的电商系统,跟着教程走下来,变身猿人找到工作不是 ...

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

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十二天】(系统架构讲解、nginx)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

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

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  8. SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  9. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

随机推荐

  1. Layui下拉框改变时触发事件

    layui.use(['form', 'layer'], function () { var form = layui.form(); var layer = layui.layer; form.on ...

  2. mac上安装iterm2的一些步骤记录

    1.首先到item官网上下载item   下载地址 http://iterm2.com/ 2.把iitem2设置为默认终端: 3.设置快速打开关闭的hotkey 我们这里设置为command + T键 ...

  3. mysql学习3

    1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引, 会保存在额外的文件中. 作用: 约束 加速查找 1.1.建立索引 a.额外的文件保存特殊的数据结构 ...

  4. 自然语言处理(四)统计机器翻译SMT

    1.统计机器翻译三要素 1.翻译模型 2.语言模型 3.排序模型 2.翻译流程 1.双语数据预处理 2.词对齐 3.构造短语翻译表 4.对短语翻译表进行概率估计 5.解码,beam search 6. ...

  5. 初识Jmeter

    初识Jmeter 测试计划是根节点,其下可以有多个Thread Group,起始可配setUp Thread Group和tearDown Group.在每个Group下可创建其它节点,模拟各类实际行 ...

  6. icpc2018焦作-I. Distance

    第一发又超时了... 题目大意:给你n个点,然后给你n-1的数,表示两两距离,然后让你输出n个答案,第i个答案表示从这n个点里面挑i个点,然后这i个点两两之间会有一个距离,答案要求这些距离和的最大值. ...

  7. 20175324 2018-2019-2 《Java程序设计》第5周学习总结

    20175324 2018-2019-2 <Java程序设计>第5周学习总结 教材学习内容总结 抽象类和具体类的区别在于抽象类中有抽象方法而具体类中没有.且抽象类不能实例化. 接口:如果一 ...

  8. 文件访问时间简记(Modify time 和 Change time)

    [root@77-29-68-bx-core]# stat hql.out File: 'hql.out' Size: 13750 Blocks: 32 IO Block: 4096 regular ...

  9. Java (JDK 多版本切换)—— Windows平台

    0. 背景 常常在不同的应用中需要用到不同版本的Java ,需要切换不同JAVA_HOME. 1. 方法 Step 1. 安装不同版本的JDK(JRE),最好都安装在一个Java目录分支下.例如: S ...

  10. SQL 查询当前时间

    Mysql: select date_format(now(),'%Y-%m-%d'); Oracle: Oracle中如何获取系统当前时间进行语句的筛选是SQL语句的常见功能 获取系统当前时间dat ...