forked from lewky/seckill
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload the project & update the README.md
1.upload the project 2.update the README.md
- Loading branch information
Showing
37 changed files
with
2,852 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.lewis</groupId> | ||
<artifactId>seckill</artifactId> | ||
<packaging>war</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>seckill Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!--补全项目依赖 --> | ||
<!--1.日志 java日志有:slf4j,log4j,logback,common-logging slf4j:是规范/接口 日志实现:log4j,logback,common-logging | ||
使用:slf4j+logback --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.7.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-core</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
<!--实现slf4j接口并整合 --> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
|
||
<!--2.数据库相关依赖 --> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.35</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>c3p0</groupId> | ||
<artifactId>c3p0</artifactId> | ||
<version>0.9.1.1</version> | ||
</dependency> | ||
|
||
<!--3.dao框架:MyBatis依赖 --> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
<version>3.3.0</version> | ||
</dependency> | ||
<!--mybatis自身实现的spring整合依赖 --> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis-spring</artifactId> | ||
<version>1.2.3</version> | ||
</dependency> | ||
|
||
<!--4.Servlet web相关依赖 --> | ||
<dependency> | ||
<groupId>taglibs</groupId> | ||
<artifactId>standard</artifactId> | ||
<version>1.1.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>jstl</groupId> | ||
<artifactId>jstl</artifactId> | ||
<version>1.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.5.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!--5:spring依赖 --> | ||
<!--1)spring核心依赖 --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-beans</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<!--2)spring dao层依赖 --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-tx</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<!--3)springweb相关依赖 --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
<!--4)spring test相关依赖 --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>4.1.7.RELEASE</version> | ||
</dependency> | ||
|
||
<!--添加redis依赖 --> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>2.7.3</version> | ||
</dependency> | ||
|
||
<!--prostuff序列化依赖 --> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-core</artifactId> | ||
<version>1.0.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-runtime</artifactId> | ||
<version>1.0.8</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>seckill</finalName> | ||
<resources> | ||
<!--打包时包含源代码包下的资源文件,默认情况下只会打包src/main/java下的源代码 --> | ||
<resource> | ||
<directory>src/main/java</directory> | ||
<includes> | ||
<include>**/*.xml</include> | ||
</includes> | ||
<filtering>false</filtering> | ||
</resource> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
</resource> | ||
</resources> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
-- 数据库初始化脚本 | ||
|
||
-- 创建数据库 | ||
CREATE DATABASE seckill; | ||
-- 使用数据库 | ||
use seckill; | ||
CREATE TABLE seckill( | ||
`seckill_id` BIGINT NOT NUll AUTO_INCREMENT COMMENT '商品库存ID', | ||
`name` VARCHAR(120) NOT NULL COMMENT '商品名称', | ||
`number` int NOT NULL COMMENT '库存数量', | ||
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | ||
`start_time` TIMESTAMP NOT NULL COMMENT '秒杀开始时间', | ||
`end_time` TIMESTAMP NOT NULL COMMENT '秒杀结束时间', | ||
PRIMARY KEY (seckill_id), | ||
key idx_start_time(start_time), | ||
key idx_end_time(end_time), | ||
key idx_create_time(create_time) | ||
)ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT='秒杀库存表'; | ||
|
||
-- 初始化数据 | ||
INSERT into seckill(name,number,start_time,end_time) | ||
VALUES | ||
('1000元秒杀iphone6',100,'2016-01-01 00:00:00','2016-01-02 00:00:00'), | ||
('800元秒杀ipad',200,'2016-01-01 00:00:00','2016-01-02 00:00:00'), | ||
('6600元秒杀mac book pro',300,'2016-01-01 00:00:00','2016-01-02 00:00:00'), | ||
('7000元秒杀iMac',400,'2016-01-01 00:00:00','2016-01-02 00:00:00'); | ||
|
||
-- 秒杀成功明细表 | ||
-- 用户登录认证相关信息(简化为手机号) | ||
CREATE TABLE success_killed( | ||
`seckill_id` BIGINT NOT NULL COMMENT '秒杀商品ID', | ||
`user_phone` BIGINT NOT NULL COMMENT '用户手机号', | ||
`state` TINYINT NOT NULL DEFAULT -1 COMMENT '状态标识:-1:无效 0:成功 1:已付款 2:已发货', | ||
`create_time` TIMESTAMP NOT NULL COMMENT '创建时间', | ||
PRIMARY KEY(seckill_id,user_phone),/*联合主键*/ | ||
KEY idx_create_time(create_time) | ||
)ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='秒杀成功明细表'; | ||
|
||
-- SHOW CREATE TABLE seckill;#显示表的创建信息 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.lewis.dao; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
|
||
import com.lewis.entity.Seckill; | ||
|
||
public interface SeckillDao { | ||
|
||
/** | ||
* 减库存 | ||
* | ||
* @param seckillId | ||
* @param killTime | ||
* @return 更新的记录行数,如果返回值<1则表示更新失败 | ||
*/ | ||
int reduceNumber(@Param("seckillId") long seckillId, @Param("killTime") Date killTime); | ||
|
||
/** | ||
* 根据id查询秒杀商品 | ||
* | ||
* @param seckillId | ||
* @return | ||
*/ | ||
Seckill queryById(long seckillId); | ||
|
||
/** | ||
* 根据偏移量查询秒杀商品列表 | ||
* | ||
* @param offset | ||
* @param limit | ||
* @return | ||
*/ | ||
List<Seckill> queryAll(@Param("offset") int offset, @Param("limit") int limit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.lewis.dao; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
|
||
import com.lewis.entity.SuccessKilled; | ||
|
||
public interface SuccessKilledDao { | ||
|
||
/** | ||
* 插入购买明细,可过滤重复 | ||
* | ||
* @param seckillId | ||
* @param userphone | ||
* @return 插入的行数,如果返回值<1则表示插入失败 | ||
*/ | ||
int insertSuccessKilled(@Param("seckillId") long seckillId, @Param("userPhone") long userPhone); | ||
|
||
/** | ||
* 根据id查询SuccessKilled并携带秒杀商品对象实体 | ||
* | ||
* @param seckillId | ||
* @return | ||
*/ | ||
SuccessKilled queryByIdWithSeckill(@Param("seckillId") long seckillId, @Param("userPhone") long userPhone); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.lewis.dao.cache; | ||
|
||
import com.dyuproject.protostuff.LinkedBuffer; | ||
import com.dyuproject.protostuff.ProtostuffIOUtil; | ||
import com.dyuproject.protostuff.runtime.RuntimeSchema; | ||
import com.lewis.entity.Seckill; | ||
|
||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisPool; | ||
|
||
public class RedisDao { | ||
private final JedisPool jedisPool; | ||
|
||
public RedisDao(String ip, int port) { | ||
jedisPool = new JedisPool(ip, port); | ||
} | ||
|
||
private RuntimeSchema<Seckill> schema = RuntimeSchema.createFrom(Seckill.class); | ||
|
||
public Seckill getSeckill(long seckillId) { | ||
// redis操作逻辑 | ||
try { | ||
Jedis jedis = jedisPool.getResource(); | ||
try { | ||
String key = "seckill:" + seckillId; | ||
// 并没有实现哪部序列化操作 | ||
// 采用自定义序列化 | ||
// protostuff: pojo. | ||
byte[] bytes = jedis.get(key.getBytes()); | ||
// 缓存重获取到 | ||
if (bytes != null) { | ||
Seckill seckill = schema.newMessage(); | ||
ProtostuffIOUtil.mergeFrom(bytes, seckill, schema); | ||
// seckill被反序列化 | ||
|
||
return seckill; | ||
} | ||
} finally { | ||
jedis.close(); | ||
} | ||
} catch (Exception e) { | ||
|
||
} | ||
return null; | ||
} | ||
|
||
public String putSeckill(Seckill seckill) { | ||
try { | ||
Jedis jedis = jedisPool.getResource(); | ||
try { | ||
String key = "seckill:" + seckill.getSeckillId(); | ||
byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema, | ||
LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); | ||
// 超时缓存 | ||
int timeout = 60 * 60;// 1小时 | ||
String result = jedis.setex(key.getBytes(), timeout, bytes); | ||
|
||
return result; | ||
} finally { | ||
jedis.close(); | ||
} | ||
} catch (Exception e) { | ||
|
||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.