秒杀总结1


package com.yf.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.sql.DataSource;

@Configuration
public class TsetConfig {
    @Bean
    public JdbcTemplate getJdbcTemplate() {
        JdbcTemplate jdbc = new JdbcTemplate();
        jdbc.setDataSource(dataSource());
        return jdbc;
    }

    @Bean(name = "dataSource")
    @Qualifier(value = "dataSource")
    @Primary
    @ConfigurationProperties(prefix = "c3p0")
    public DataSource dataSource() {
        return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
    }

    /**
     * 跨域
     * @return
     */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
            }
        };
    }
}
package com.yf.dao;

import com.yf.pojo.Goods;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsDao {

    List<Goods> getAllGoods();

    int minusNum(String name);
}
package com.yf.dao;

import com.yf.pojo.GoodUser;
import com.yf.pojo.Goods;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodUserDao {
    List<GoodUser> getAllGoodUser();

    void saveGoods(GoodUser goodUser);
}
package com.yf.pojo;

public class Goods {
    private Integer id;
    private String name;
    private Integer num;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}
package com.yf.pojo;

public class GoodUser {
    private Integer id;
    private String name;
    private String goodsName;
    private String killTime;

    public String getKillTime() {
        return killTime;
    }

    public void setKillTime(String killTime) {
        this.killTime = killTime;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }
}
package com.yf.service;

import com.yf.dao.GoodsDao;
import com.yf.dao.UserDao;
import com.yf.pojo.Goods;
import com.yf.pojo.UserPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    public List<Goods> getGoods() {
        List<Goods> goods = goodsDao.getAllGoods();
        return goods;
    }

    public int minusNum(String name){
        return goodsDao.minusNum(name);
    }


}
package com.yf.service;

import com.yf.dao.GoodUserDao;
import com.yf.dao.GoodsDao;
import com.yf.pojo.GoodUser;
import com.yf.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodUserService {
    @Autowired
    private GoodUserDao goodUserDao;

    @Autowired
    private GoodsDao goodsDao;

    public List<GoodUser> getGoods() {
        List<GoodUser> goods = goodUserDao.getAllGoodUser();
        return goods;
    }

    //保存
    public void saveGoods(GoodUser goodUser){
        goodUserDao.saveGoods(goodUser);
    }

    //秒杀1.减少数量 2.插入用户记录
    public void killGood(String userName){
        GoodUser goodUser=new GoodUser();
        goodUser.setName(userName);
        goodUser.setGoodsName("phone");
        int i=goodsDao.minusNum("phone");
        if(i>0){
            goodUserDao.saveGoods(goodUser);
            System.out.println(userName+" 秒杀成功");
        }

    }

}
<?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.yf.dao.GoodsDao" >

   <resultMap id="BaseResultMaps" type="com.yf.pojo.Goods" >
       <id column="id" property="id" jdbcType="INTEGER" />
       <result column="name" property="name" jdbcType="VARCHAR" />
       <result column="num" property="num" jdbcType="INTEGER" />
     </resultMap>

    <sql id="Base_Column_Lists" >
    id, name, num
  </sql>

    <!-- 得到所有的用户信息 -->
    <select id="getAllGoods" resultMap="BaseResultMaps" >
        select
        <include refid="Base_Column_Lists" />
        from goods
    </select>

    <update id="minusNum" parameterType="string">
        update goods set num=num-1 where name=#{name} and num>0
    </update>



</mapper>
<?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.yf.dao.GoodUserDao" >

    <resultMap id="BaseResultMapG" type="com.yf.pojo.GoodUser" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="goods_name" property="goodsName" jdbcType="VARCHAR" />
    </resultMap>

    <sql id="Base_Column_ListG" >
    id, name, goods_name
  </sql>

    <!-- 得到所有的用户信息 -->
    <select id="getAllGoodUser" resultMap="BaseResultMapG" >
        select
        <include refid="Base_Column_ListG" />
        from goodUser
    </select>

    <insert id="saveGoods" parameterType="com.yf.pojo.GoodUser">
        insert into goodUser(name,goods_name,kill_time) value(#{name},#{goodsName},now())
    </insert>





</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
</configuration>
server.port=7878
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.minPoolSize=2
c3p0.maxPoolSize=10
c3p0.maxIdleTime=1800
c3p0.acquireIncrement=3
c3p0.maxStatements=1000
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=60
c3p0.acquireRetryAttempts=30
c3p0.acquireRetryDelay=1000
c3p0.breakAfterAcquireFailure=false
c3p0.testConnectionOnCheckout=false

c3p0.jdbcUrl=jdbc:mysql://localhost/yftest?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
c3p0.user=root
c3p0.password=123456


mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.yf.pojo
package com.yf.service;

import com.yf.pojo.GoodUser;
import com.yf.testMain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.concurrent.CountDownLatch;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = testMain.class)
public class GoodsServiceTest {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodUserService goodUserService;

    private static final int threadNum=10000;

    private CountDownLatch cdl=new CountDownLatch(threadNum);

    @Test
    public void testGet() {
        List<GoodUser> goods=goodUserService.getGoods();
        for (GoodUser gu:goods){
            System.out.println(gu.getGoodsName());
        }
    }

    @Test
    public void testSave() {
       GoodUser goodUser=new GoodUser();
       goodUser.setName("yf");
       goodUser.setGoodsName("bike");
       goodUserService.saveGoods(goodUser);
    }

    @Test
    public void testMinus() {
        goodsService.minusNum("bike");
    }

    @Test
    public void testKill() throws InterruptedException {
        Thread[] threads=new Thread[threadNum];
        for(int i=0;i<threadNum;i++){
            System.out.println("循环"+i+"次");
            Thread thread=new Thread(new userRequest("yf"+i));
            threads[i]=thread;
            thread.start();
            cdl.countDown();
        }
        for(Thread thread:threads){
            thread.join();
        }

    }

    class userRequest implements Runnable{
        String userName;
        public userRequest(String userName) {
            this.userName = userName;
        }
        @Override
        public void run() {
            try {
                cdl.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            goodUserService.killGood(userName);
        }
    }
}

1.防止超出秒杀商品数量
update goods set num=num-1 where name=#{name} and num>0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值