Skip to content

Commit 30ed9f1

Browse files
authored
SSM+Redis+MySQL实现的秒杀系统
1 parent 101fdb3 commit 30ed9f1

38 files changed

Lines changed: 1879 additions & 0 deletions

pom.xml

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.seckill</groupId>
5+
<artifactId>seckill</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>seckill Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
<!--使用junit4(注解),junit3(编程的方式)-->
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.12</version>
16+
<scope>test</scope>
17+
</dependency>
18+
19+
<!--补全依赖-->
20+
<!--1.Java日志:slf4j,log4j,logback,common-logging
21+
sl4j是规范/接口
22+
日志实现log4j,logback,common-logging
23+
使用slf4j+logback
24+
-->
25+
<dependency>
26+
<groupId>org.slf4j</groupId>
27+
<artifactId>slf4j-api</artifactId>
28+
<version>1.7.12</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>ch.qos.logback</groupId>
32+
<artifactId>logback-core</artifactId>
33+
<version>1.1.2</version>
34+
</dependency>
35+
<!--实现slf4j接口并整合-->
36+
<dependency>
37+
<groupId>ch.qos.logback</groupId>
38+
<artifactId>logback-classic</artifactId>
39+
<version>1.1.2</version>
40+
</dependency>
41+
<!--2.数据库相关依赖-->
42+
<dependency>
43+
<groupId>mysql</groupId>
44+
<artifactId>mysql-connector-java</artifactId>
45+
<version>5.1.38</version>
46+
<scope>runtime</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>c3p0</groupId>
50+
<artifactId>c3p0</artifactId>
51+
<version>0.9.1.2</version>
52+
</dependency>
53+
54+
<!--DAO框架:Mybatis依赖-->
55+
<dependency>
56+
<groupId>org.mybatis</groupId>
57+
<artifactId>mybatis</artifactId>
58+
<version>3.2.8</version>
59+
</dependency>
60+
<!--Mybatis自身实现的Spring整合依赖-->
61+
<dependency>
62+
<groupId>org.mybatis</groupId>
63+
<artifactId>mybatis-spring</artifactId>
64+
<version>1.2.2</version>
65+
</dependency>
66+
67+
<!--3.Servlet Web相关依赖-->
68+
<dependency>
69+
<groupId>taglibs</groupId>
70+
<artifactId>standard</artifactId>
71+
<version>1.1.2</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>jstl</groupId>
75+
<artifactId>jstl</artifactId>
76+
<version>1.2</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.fasterxml.jackson.core</groupId>
80+
<artifactId>jackson-databind</artifactId>
81+
<version>2.8.5</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>javax.servlet</groupId>
85+
<artifactId>javax.servlet-api</artifactId>
86+
<version>3.1.0</version>
87+
</dependency>
88+
<!--4.Spring依赖-->
89+
<!--1)Spring核心依赖-->
90+
<dependency>
91+
<groupId>org.springframework</groupId>
92+
<artifactId>spring-core</artifactId>
93+
<version>4.1.6.RELEASE</version>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.springframework</groupId>
97+
<artifactId>spring-beans</artifactId>
98+
<version>4.1.6.RELEASE</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.springframework</groupId>
102+
<artifactId>spring-context</artifactId>
103+
<version>4.1.6.RELEASE</version>
104+
</dependency>
105+
<!--2)Spring dao层依赖-->
106+
<dependency>
107+
<groupId>org.springframework</groupId>
108+
<artifactId>spring-jdbc</artifactId>
109+
<version>4.1.6.RELEASE</version>
110+
</dependency>
111+
<dependency>
112+
<groupId>org.springframework</groupId>
113+
<artifactId>spring-tx</artifactId>
114+
<version>4.1.6.RELEASE</version>
115+
</dependency>
116+
<!--3)Spring web层依赖-->
117+
<dependency>
118+
<groupId>org.springframework</groupId>
119+
<artifactId>spring-web</artifactId>
120+
<version>4.1.6.RELEASE</version>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.springframework</groupId>
124+
<artifactId>spring-webmvc</artifactId>
125+
<version>4.1.6.RELEASE</version>
126+
</dependency>
127+
<!--4)Spring test相关依赖-->
128+
<dependency>
129+
<groupId>org.springframework</groupId>
130+
<artifactId>spring-test</artifactId>
131+
<version>4.1.6.RELEASE</version>
132+
</dependency>
133+
<!--redis客户端-->
134+
<dependency>
135+
<groupId>redis.clients</groupId>
136+
<artifactId>jedis</artifactId>
137+
<version>2.9.0</version>
138+
</dependency>
139+
<!--protostuff依赖-->
140+
<dependency>
141+
<groupId>com.dyuproject.protostuff</groupId>
142+
<artifactId>protostuff-core</artifactId>
143+
<version>1.0.8</version>
144+
</dependency>
145+
<dependency>
146+
<groupId>com.dyuproject.protostuff</groupId>
147+
<artifactId>protostuff-runtime</artifactId>
148+
<version>1.0.8</version>
149+
</dependency>
150+
151+
<dependency>
152+
<groupId>commons-collections</groupId>
153+
<artifactId>commons-collections</artifactId>
154+
<version>3.2.1</version>
155+
</dependency>
156+
157+
</dependencies>
158+
159+
160+
<build>
161+
<finalName>seckill</finalName>
162+
</build>
163+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.seckill.dao;
2+
3+
import com.dyuproject.protostuff.*;
4+
import com.dyuproject.protostuff.runtime.*;
5+
import org.seckill.entity.*;
6+
import org.slf4j.*;
7+
import redis.clients.jedis.*;
8+
9+
/**
10+
* Created by pc on 2017/3/8.
11+
*/
12+
public class RedisDao {
13+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
14+
private final JedisPool jedisPool;
15+
16+
private RuntimeSchema<Seckill> schema =RuntimeSchema.createFrom(Seckill.class);
17+
public RedisDao(String ip,int port){
18+
jedisPool = new JedisPool(ip, port);
19+
}
20+
21+
public Seckill getSeckill(long seckillId){
22+
try{
23+
Jedis jedis=jedisPool.getResource();
24+
try {
25+
String key="seckill:"+seckillId;
26+
//没有实现内部序列化
27+
//get->byte[]->反序列化->Object(Seckill)
28+
//采用自定义序列化
29+
//protostuff:pojo
30+
byte[] bytes=jedis.get(key.getBytes());
31+
if(bytes!=null){
32+
Seckill seckill=schema.newMessage();
33+
ProtostuffIOUtil.mergeFrom(bytes,seckill,schema);
34+
//seckill被反序列化
35+
return seckill;
36+
}
37+
}finally {
38+
jedis.close();
39+
}
40+
}catch (Exception e){
41+
logger.error(e.getMessage(),e);
42+
}
43+
return null;
44+
}
45+
46+
public String putSeckill(Seckill seckill){
47+
//set Object(Seckill)->序列化->byte[]
48+
try{
49+
Jedis jedis=jedisPool.getResource();
50+
try {
51+
String key="seckill:"+seckill.getSeckillId();
52+
byte[] bytes=ProtostuffIOUtil.toByteArray(seckill, schema,
53+
LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
54+
//超时缓存
55+
int timeout=60*60;
56+
String result=jedis.setex(key.getBytes(),timeout, bytes);
57+
return result;
58+
}finally {
59+
jedis.close();
60+
}
61+
}catch (Exception e){
62+
logger.error(e.getMessage(),e);
63+
}
64+
return null;
65+
}
66+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.seckill.dao;
2+
3+
import org.apache.ibatis.annotations.*;
4+
import org.seckill.entity.*;
5+
6+
import java.util.*;
7+
8+
/**
9+
* Created by pc on 2017/3/5.
10+
*/
11+
public interface SeckillDao {
12+
/**减库存
13+
* @param seckillId
14+
* @param killTime
15+
* @return 影响行数
16+
*/
17+
int reduceNumber(@Param("seckillId") long seckillId,@Param("killTime") Date killTime);
18+
19+
/**根据id查询秒杀对象
20+
* @param seckillId
21+
* @return
22+
*/
23+
Seckill queryById(long seckillId);
24+
25+
/**根据偏移量查询秒杀商品列表
26+
* @param offset
27+
* @param limit
28+
* @return
29+
*/
30+
List<Seckill> queryAll(@Param("offset") int offset,@Param("limit") int limit);
31+
32+
/**
33+
* 使用存储过程执行秒杀
34+
* @param paramMap
35+
*/
36+
void killByProcedure(Map<String,Object> paramMap);
37+
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.seckill.dao;
2+
3+
import org.apache.ibatis.annotations.*;
4+
import org.seckill.entity.*;
5+
6+
/**
7+
* Created by pc on 2017/3/5.
8+
*/
9+
public interface SuccessKilledDao {
10+
/**
11+
* 插入购买明细,可过滤重复
12+
* @param seckillId
13+
* @param userPhone
14+
* @return
15+
*/
16+
int insertSuccessKilled(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
17+
18+
/**
19+
* 根据id查询SuccessKilled,并携带秒杀产品对象实体
20+
* @param seckillId
21+
* @return
22+
*/
23+
SuccessKilled queryByIdWithSeckill(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
24+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.seckill.dto;
2+
3+
/**
4+
* 暴露秒杀地址DTO:与业务无关实体
5+
* Created by pc on 2017/3/6.
6+
*/
7+
public class Exposer {
8+
//是否开启秒杀
9+
private boolean exposed;
10+
//一种加密措施
11+
private String md5;
12+
//id
13+
private long seckillId;
14+
//系统当前时间
15+
private long now;
16+
//秒杀开启时间
17+
private long start;
18+
//秒杀结束时间
19+
private long end;
20+
21+
public Exposer(boolean exposed, String md5, long seckillId) {
22+
this.exposed = exposed;
23+
this.md5 = md5;
24+
this.seckillId = seckillId;
25+
}
26+
27+
public Exposer(long seckillId,boolean exposed, long now, long start, long end) {
28+
this.seckillId=seckillId;
29+
this.exposed = exposed;
30+
this.now = now;
31+
this.start = start;
32+
this.end = end;
33+
}
34+
35+
public Exposer(long seckillId, boolean exposed) {
36+
this.seckillId = seckillId;
37+
this.exposed = exposed;
38+
}
39+
40+
public boolean isExposed() {
41+
return exposed;
42+
}
43+
44+
public void setExposed(boolean exposed) {
45+
this.exposed = exposed;
46+
}
47+
48+
public String getMd5() {
49+
return md5;
50+
}
51+
52+
public void setMd5(String md5) {
53+
this.md5 = md5;
54+
}
55+
56+
public long getSeckillId() {
57+
return seckillId;
58+
}
59+
60+
public void setSeckillId(long seckillId) {
61+
this.seckillId = seckillId;
62+
}
63+
64+
public long getNow() {
65+
return now;
66+
}
67+
68+
public void setNow(long now) {
69+
this.now = now;
70+
}
71+
72+
public long getStart() {
73+
return start;
74+
}
75+
76+
public void setStart(long start) {
77+
this.start = start;
78+
}
79+
80+
public long getEnd() {
81+
return end;
82+
}
83+
84+
public void setEnd(long end) {
85+
this.end = end;
86+
}
87+
88+
@Override
89+
public String toString() {
90+
return "Exposer{" +
91+
"exposed=" + exposed +
92+
", md5='" + md5 + '\'' +
93+
", seckillId=" + seckillId +
94+
", now=" + now +
95+
", start=" + start +
96+
", end=" + end +
97+
'}';
98+
}
99+
}

0 commit comments

Comments
 (0)