From d717f786953e70cbdebc1b82440c5dce798193be Mon Sep 17 00:00:00 2001 From: Lewis <1019175915@qq.com> Date: Tue, 10 Oct 2017 11:34:02 +0800 Subject: [PATCH] push code push the complete code --- pom.xml | 10 ++++- src/main/java/com/lewis/dao/SeckillDao.java | 7 +++ src/main/java/com/lewis/mapper/SeckillDao.xml | 10 +++++ .../com/lewis/service/SeckillService.java | 10 +++++ .../service/impl/SeckillServiceImpl.java | 38 +++++++++++++--- .../java/com/lewis/web/SeckillController.java | 4 +- src/main/resources/spring/spring-dao.xml | 13 +++--- .../com/lewis/dao/cache/RedisDaoTest.java | 45 +++++++++++++++++++ .../com/lewis/service/SeckillServiceTest.java | 13 ++++++ 9 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/lewis/dao/cache/RedisDaoTest.java diff --git a/pom.xml b/pom.xml index 5dbae81..c93c52f 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ javax.servlet javax.servlet-api 3.1.0 - provided + provided @@ -129,7 +129,7 @@ 4.1.7.RELEASE - + redis.clients jedis @@ -147,6 +147,12 @@ protostuff-runtime 1.0.8 + + + commons-collections + commons-collections + 3.2.2 + seckill diff --git a/src/main/java/com/lewis/dao/SeckillDao.java b/src/main/java/com/lewis/dao/SeckillDao.java index bf78eac..a207897 100644 --- a/src/main/java/com/lewis/dao/SeckillDao.java +++ b/src/main/java/com/lewis/dao/SeckillDao.java @@ -2,6 +2,7 @@ import java.util.Date; import java.util.List; +import java.util.Map; import org.apache.ibatis.annotations.Param; @@ -34,4 +35,10 @@ public interface SeckillDao { * @return */ List queryAll(@Param("offset") int offset, @Param("limit") int limit); + + /** + * 使用储存过程执行秒杀 + * @param paramMap + */ + void killByProcedure(Map paramMap); } diff --git a/src/main/java/com/lewis/mapper/SeckillDao.xml b/src/main/java/com/lewis/mapper/SeckillDao.xml index e5e2bab..44d8dff 100644 --- a/src/main/java/com/lewis/mapper/SeckillDao.xml +++ b/src/main/java/com/lewis/mapper/SeckillDao.xml @@ -35,4 +35,14 @@ limit #{offset},#{limit} + + + \ No newline at end of file diff --git a/src/main/java/com/lewis/service/SeckillService.java b/src/main/java/com/lewis/service/SeckillService.java index fcda8ba..eee649e 100644 --- a/src/main/java/com/lewis/service/SeckillService.java +++ b/src/main/java/com/lewis/service/SeckillService.java @@ -50,4 +50,14 @@ public interface SeckillService { */ SeckillExecution executeSeckill(long seckillId, long userPhone, String md5) throws SeckillException, RepeatKillException, SeckillCloseException; + + /** + * 调用存储过程来执行秒杀操作,不需要抛出异常 + * + * @param seckillId 秒杀的商品ID + * @param userPhone 手机号码 + * @param md5 md5加密值 + * @return 根据不同的结果返回不同的实体信息 + */ + SeckillExecution executeSeckillProcedure(long seckillId,long userPhone,String md5); } diff --git a/src/main/java/com/lewis/service/impl/SeckillServiceImpl.java b/src/main/java/com/lewis/service/impl/SeckillServiceImpl.java index aac1a5e..50161f4 100644 --- a/src/main/java/com/lewis/service/impl/SeckillServiceImpl.java +++ b/src/main/java/com/lewis/service/impl/SeckillServiceImpl.java @@ -1,5 +1,6 @@ package com.lewis.service.impl; +import org.apache.commons.collections.MapUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -22,7 +23,9 @@ import javax.annotation.Resource; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; //@Component @Service @Repository @Controller @Service @@ -42,8 +45,8 @@ public class SeckillServiceImpl implements SeckillService { // @Resource private SuccessKilledDao successKilledDao; -// @Autowired -// private RedisDao redisDao; + @Autowired + private RedisDao redisDao; public List getSeckillList() { return seckillDao.queryAll(0, 4); @@ -57,18 +60,16 @@ public Exposer exportSeckillUrl(long seckillId) { // 优化点:缓存优化:超时的基础上维护一致性 // 1.访问redi -// Seckill seckill = redisDao.getSeckill(seckillId); - Seckill seckill=seckillDao.queryById(seckillId); + Seckill seckill = redisDao.getSeckill(seckillId); if (seckill == null) { // 2.访问数据库 seckill = seckillDao.queryById(seckillId); if (seckill == null) {// 说明查不到这个秒杀产品的记录 return new Exposer(false, seckillId); - } - /*else { + } else { // 3.放入redis redisDao.putSeckill(seckill); - }*/ + } } // 若是秒杀未开启 @@ -139,4 +140,27 @@ public SeckillExecution executeSeckill(long seckillId, long userPhone, String md } } + + @Override + public SeckillExecution executeSeckillProcedure(long seckillId, long userPhone, String md5) { + if (md5 == null || !md5.equals(getMD5(seckillId))) { + return new SeckillExecution(seckillId, SeckillStatEnum.DATE_REWRITE); + } + Date killTime = new Date(); + Map map = new HashMap<>(); + map.put("seckillId", seckillId); + map.put("phone", userPhone); + map.put("killTime", killTime); + map.put("result", null); + // 执行储存过程,result被复制 + seckillDao.killByProcedure(map); + // 获取result + int result = MapUtils.getInteger(map, "result", -2); + if (result == 1) { + SuccessKilled successKilled = successKilledDao.queryByIdWithSeckill(seckillId, userPhone); + return new SeckillExecution(seckillId, SeckillStatEnum.SUCCESS, successKilled); + } else { + return new SeckillExecution(seckillId, SeckillStatEnum.stateOf(result)); + } + } } diff --git a/src/main/java/com/lewis/web/SeckillController.java b/src/main/java/com/lewis/web/SeckillController.java index b02ff71..4ae0106 100644 --- a/src/main/java/com/lewis/web/SeckillController.java +++ b/src/main/java/com/lewis/web/SeckillController.java @@ -87,7 +87,9 @@ public SeckillResult execute(@PathVariable("seckillId") Long s } try { - SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5); + //这里改为调用存储过程 +// SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5); + SeckillExecution execution = seckillService.executeSeckillProcedure(seckillId, userPhone, md5); return new SeckillResult(true, execution); }catch (RepeatKillException e1) { diff --git a/src/main/resources/spring/spring-dao.xml b/src/main/resources/spring/spring-dao.xml index 424c1e4..f0bd4df 100644 --- a/src/main/resources/spring/spring-dao.xml +++ b/src/main/resources/spring/spring-dao.xml @@ -4,7 +4,8 @@ xmlns:context="http://www.springframework.org/schema/context" 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/context + http://www.springframework.org/schema/context/spring-context.xsd"> @@ -54,9 +55,9 @@ - - + + + + + \ No newline at end of file diff --git a/src/test/java/com/lewis/dao/cache/RedisDaoTest.java b/src/test/java/com/lewis/dao/cache/RedisDaoTest.java new file mode 100644 index 0000000..539f297 --- /dev/null +++ b/src/test/java/com/lewis/dao/cache/RedisDaoTest.java @@ -0,0 +1,45 @@ +package com.lewis.dao.cache; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.lewis.dao.SeckillDao; +import com.lewis.entity.Seckill; + +@RunWith(SpringJUnit4ClassRunner.class) +// 告诉junit spring的配置文件 +@ContextConfiguration({ "classpath:spring/spring-dao.xml" }) +public class RedisDaoTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private long id = 1001; + + @Autowired + private RedisDao redisDao; + + @Autowired + private SeckillDao seckillDao; + + @Test + public void testSeckill() { + + Seckill seckill = redisDao.getSeckill(id); + if (seckill == null) { + seckill = seckillDao.queryById(id); + if (seckill != null) { + String result = redisDao.putSeckill(seckill); + logger.info("result={}", result); + seckill = redisDao.getSeckill(id); + logger.info("seckill={}", seckill); + } + } + } + +} diff --git a/src/test/java/com/lewis/service/SeckillServiceTest.java b/src/test/java/com/lewis/service/SeckillServiceTest.java index c593015..0f9f70d 100644 --- a/src/test/java/com/lewis/service/SeckillServiceTest.java +++ b/src/test/java/com/lewis/service/SeckillServiceTest.java @@ -90,4 +90,17 @@ public void testSeckillLogic() throws Exception { logger.warn("exposer={}", exposer); } } + + @Test + public void executeSeckillProcedure(){ + long seckillId = 1001; + long phone = 13680115101L; + Exposer exposer = seckillService.exportSeckillUrl(seckillId); + if (exposer.isExposed()) { + String md5 = exposer.getMd5(); + SeckillExecution execution = seckillService.executeSeckillProcedure(seckillId, phone, md5); + logger.info("execution={}", execution); + } + } + }