Explorar el Código

第一种最慢的解决方案

yegang hace 2 años
padre
commit
c5370abe2c

+ 42 - 2
mask.iml

@@ -28,7 +28,6 @@
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.6.1" level="project" />
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.3" level="project" />
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.3" level="project" />
-    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.3" level="project" />
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.3" level="project" />
     <orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.10" level="project" />
     <orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.10" level="project" />
@@ -54,7 +53,6 @@
     <orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.15" level="project" />
     <orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.15" level="project" />
     <orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.15" level="project" />
-    <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
     <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.6.3" level="project" />
     <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.6.3" level="project" />
     <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.6.3" level="project" />
@@ -86,5 +84,47 @@
     <orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.15" level="project" />
     <orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.3.15" level="project" />
     <orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.8.4" level="project" />
+    <orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.47" level="project" />
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-boot-starter:3.0.7.1" level="project" />
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus:3.0.7.1" level="project" />
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-extension:3.0.7.1" level="project" />
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-core:3.0.7.1" level="project" />
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-annotation:3.0.7.1" level="project" />
+    <orderEntry type="library" name="Maven: com.github.jsqlparser:jsqlparser:1.3" level="project" />
+    <orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.2" level="project" />
+    <orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.6" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.3" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.6.3" level="project" />
+    <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.3.15" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-activemq:2.0.0.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-jms:5.3.15" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-messaging:5.3.15" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.15" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.activemq:activemq-broker:5.16.3" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.activemq:activemq-client:5.16.3" level="project" />
+    <orderEntry type="library" name="Maven: org.fusesource.hawtbuf:hawtbuf:1.11" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:1.0.1" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.activemq:activemq-openwire-legacy:5.16.3" level="project" />
+    <orderEntry type="library" name="Maven: javax.jms:javax.jms-api:2.0.1" level="project" />
+    <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.6.3" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.6.1" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.6.1" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.6.1" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.3.15" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.3.15" level="project" />
+    <orderEntry type="library" name="Maven: io.lettuce:lettuce-core:6.1.6.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-common:4.1.73.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.73.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.73.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.73.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.73.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-tcnative-classes:2.0.46.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.73.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.4.14" level="project" />
+    <orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
+    <orderEntry type="library" name="Maven: cn.hutool:hutool-core:4.4.0" level="project" />
+    <orderEntry type="library" name="Maven: cn.hutool:hutool-all:4.5.7" level="project" />
   </component>
 </module>

+ 2 - 1
src/main/java/com/sw/dao/TMaskDao.java

@@ -2,6 +2,7 @@ package com.sw.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.sw.domain.TMask;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * (TMask)表数据库访问层
@@ -10,6 +11,6 @@ import com.sw.domain.TMask;
  * @since 2022-02-18 10:29:23
  */
 public interface TMaskDao extends BaseMapper<TMask> {
-
+    boolean decreaseStock(@Param("stock") Integer stock,@Param("version") Integer version,@Param("id") Integer id);
 
 }

+ 1 - 1
src/main/java/com/sw/domain/TMask.java

@@ -28,6 +28,6 @@ public class TMask implements Serializable {
      * 创建时间
      */
     private Date createTime;
-
+    private Integer version;
 
 }

+ 4 - 2
src/main/java/com/sw/service/impl/TMaskServiceImpl.java

@@ -48,8 +48,10 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
             return "库存不够,抢购失败";
         }
         //2.减库存
-        tMask.setMaskStock(tMask.getMaskStock()-5);
-        tMaskDao.updateById(tMask);
+        boolean b = tMaskDao.decreaseStock(5, tMask.getVersion(), 2);
+        if(!b){
+            return "抢购失败,请重试";
+        }
         //3.下订单
         TOrder tOrder =new TOrder();
         tOrder.setCreateTime(new Date());

+ 3 - 1
src/main/resources/mapper/TMaskMapper.xml

@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.sw.dao.TMaskDao">
-
+    <update id="decreaseStock">
+       UPDATE  t_mask SET mask_stock =mask_stock-#{stock} , version =version +1 where id=#{id} and version =#{version}
+    </update>
 
 </mapper>

+ 2 - 2
src/test/java/com/sw/RedisTest.java

@@ -30,8 +30,8 @@ public class RedisTest {
         for (int i = 0;i < 1000; i++){
             uids.add( i + 1);
         }
-        redisTemplate.opsForSet().add("userUids", uids.toArray());
-        Assert.assertEquals(1000l, redisTemplate.opsForSet().size("userUids").longValue());
+        redisTemplate.opsForSet().add("userUid", uids.toArray());
+        Assert.assertEquals(1000l, redisTemplate.opsForSet().size("userUid").longValue());
     }
 
     @Test