1.用Maven创建我们的项目seckill
利用Maven去创建工程然后导入Idea中并完成相关配置,这里的注意点:
- 1利用Maven创建web项目命令:mvn archetype:generate -DgroupId=org.seckill -DartifactId=seckill -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeCatalog=local
然后使用IDEA打开该项目,在IDEA中对项目按照Maven项目的标准骨架补全我们项目的相应文件包,最后的工程结构如下:
main包下进行我们项目的代码编写及相关配置文件,test包下进行我们junit单元测试。
打开WEB-INF下的web.xml,它默认为我们创建servlet版本为2.3
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true"> <!--用maven创建的web-app需要修改servlet的版本为3.0-->
</web-app>
然后打开pom.xml,在里面添加我们需要的第三方jar包的坐标配置信息,如SSM框架、数据库、日志,如下:
<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>org.seckill</groupId>
<artifactId>seckill</artifactId>
<packaging>war</packaging>
<version>1.0-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>
<!-- slf4j是规范,接口 -->
<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>
<!-- 数据库相关的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 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>
<!-- 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>
</dependency>
<!-- 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、spring web依赖 -->
<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>
<!-- 3、spring test依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>seckill</finalName>
</build>
</project>
到此,我们项目的初始化工作完成。
2. 秒杀系统业务分析
秒杀系统业务流程如下:
Paste_Image.png由图可以发现,整个系统其实是针对库存做的系统。用户成功秒杀商品,对于系统的操作就是:1.减库存。2.记录用户的购买明细。
下面看看我们用户对库存的业务分析:
记录用户的秒杀成功信息,需要记录:1.谁购买成功了。2.购买成功的时间/有效期。3.付款/发货信息。这些数据组成了用户的秒杀成功信息,也就是用户的购买行为。
为什么系统需要事务?看如下这些故障:1.若是用户成功秒杀商品我们记录了其购买明细却没有减库存。导致商品的超卖。2.减了库存却没有记录用户的购买明细。导致商品的少卖。对于上述两个故障,若是没有事务的支持,损失最大的无疑是我们的用户和商家。在MySQL中,它内置的事务机制,可以准确的帮我们完成减库存和记录用户购买明细的过程。
MySQL实现秒杀的难点分析:当用户A秒杀id为10的商品时,此时MySQL需要进行的操作是:1.开启事务。2.更新商品的库存信息。3.添加用户的购买明细,包括用户秒杀的商品id以及唯一标识用户身份的信息如电话号码等。4.提交事务。若此时有另一个用户B也在秒杀这件id为10的商品,他就需要等待,等待到用户A成功秒杀到这件商品然后MySQL成功的提交了事务他才能拿到这个id为10的商品的锁从而进行秒杀,而同一时间是不可能只有用户B在等待,肯定是有很多很多的用户都在等待拿到这个行级锁。秒杀的难点就在这里,如何高效的处理这些竞争?如何高效的完成事务?在后面模块如何进行高并发的优化。
3. Dao层设计开发
首先创建数据库数据表,通过手写sql。
-- 创建数据库
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(11) 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)
value
('2000元秒杀iphone7', 200,'2017-01-05 00:00:00','2017-01-06 00:00:00'),
('500元秒杀华为P9', 300,'2017-01-05 00:00:00','2017-01-06 00:00:00'),
('2000元秒杀华为MATE9',400,'2017-01-05 00:00:00','2017-01-06 00:00:00'),
('2500元秒杀小米MIX', 100,'2017-01-05 00:00:00','2017-01-06 00:00:00');
-- 秒杀成功明细表
create table seccess_killed(
seckill_id bigint(20) not null comment '秒杀商品id',
user_phone bigint(20) not null comment '用户手机',
state tinyint not null default -1 comment '状态标示:-1:无效 0:成功 1:已付款 2:已发货',
create_time timestamp not null default current_timestamp comment '创建时间',
primary key (seckill_id,user_phone),
key idx_create_time (create_time)
) engine=innodb charset=utf8 comment='秒杀成功明细表';
然后创建对应表的实体类。
Seckill.java,代码如下:
package org.seckill.entity;
import java.util.Date;
/**
* Created by joshul on 2016/12/30.
*/
public class Seckill {
private long seckillId;
private String name;
private int number;
private Date startTime;
private Date endTime;
private Date createTime;
public long getSeckillId() {
return seckillId;
}
public void setSeckillId(long seckillId) {
this.seckillId = seckillId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Seckill{" +
"seckillId=" + seckillId +
", name='" + name + '\'' +
", number=" + number +
", startTime=" + startTime +
", endTime=" + endTime +
", createTime=" + createTime +
'}';
}
}
SuccessKilled.java,代码如下:
package org.seckill.entity;
import java.util.Date;
public class SuccessKilled {
private long seckilled;//成功秒杀过的id
private long userPhone;//用户电话
private short state; //用来记录交易状态
private Date createTime;//记录创建时间
private Seckill seckill;//变通,多对一
public long getSeckilled() {
return seckilled;
}
public void setSeckilled(long seckilled) {
this.seckilled = seckilled;
}
public long getUserPhone() {
return userPhone;
}
public void setUserPhone(long userPhone) {
this.userPhone = userPhone;
}
public short getState() {
return state;
}
public void setState(short state) {
this.state = state;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Seckill getSeckill() {
return seckill;
}
public void setSeckill(Seckill seckill) {
this.seckill = seckill;
}
@Override
public String toString() {
return "SuccessKilled{" +
"seckilled=" + seckilled +
", userPhone=" + userPhone +
", state=" + state +
", createTime=" + createTime +
'}';
}
}
针对实体创建出对应dao层的接口,在com.joshul.seckill.dao包下
Seckill.java:
package org.seckill.dao;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.seckill.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(@Param("seckillId")long seckillId);
/**
* 根据偏移量查询秒杀商品列表
* @param offet
* @param limit
* @return
*/
List<Seckill> queryAll(@Param("offset") int offet,@Param("limit")int limit);
}
SuccessKilled.java:
package org.seckill.dao;
import org.apache.ibatis.annotations.Param;
import org.seckill.entity.SuccessKilled;
public interface SuccessSeckilledDao {
/**
* 插入购买明细,可过滤重复
* @param seckillId
* @param userPhone
* @return 插入的行数
*/
int insertSuccessSeckilled(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
/**
* 根据id查询SuccessKilled并携带秒杀产品对象实例
* @param seckillId
* @return
*/
SuccessKilled queryByIdWithSeckill(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
}
接下来基于MyBatis来实现之前设计的Dao层接口。
在mybatis-config.xml加入如下配置信息:
<?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>
<!--使用jdbc的getGeneratedKeys 获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列别名替换列名 默认:true select name as title from table-->
<setting name="useColumnLabel" value="true"/>
<!--开起驼峰命名转换:Table(create_time) ->Entity(createTime) –>-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
配置文件创建好后需要关注的是Dao接口该如何实现,mybatis为提供了mapper动态代理开发的方式为自动实现Dao的接口。在mapper包下创建对应Dao接口的xml映射文件,里面用于编写操作数据库的sql语句,SeckillDao.xml和SuccessKilledDao.xml。
SeckillDao.xml:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.seckill.dao.SeckillDao">
<!--目的:为DAO借口方法提供sql配置-->
<update id="reduceNumber">
update
seckill
set
number=number -1
where seckill_id = #{seckillId}
and start_time <![CDATA[ <= ]]> #{killTime}
and end_time >= #{killTime}
and number > 0;
</update>
<select id="queryById" resultType="Seckill" parameterType="long">
select seckill_id,name,number,start_time, end_time, create_time
from seckill
where seckill_id=#{seckillId}
</select>
<select id="queryAll" resultType="Seckill">
select seckill_id,name,number,start_time,end_time,create_time
from seckill
order by create_time DESC
limit #{offset},#{limit}
</select>
</mapper>
SuccessKilledDao.xml:
<?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">
<!-- 命名空间,用于对SQL的分类管理 -->
<mapper namespace="org.seckill.dao.SuccessSeckilledDao">
<insert id="insertSuccessSeckilled">
insert ignore into success_skilled(seckill_id,user_phone,state)
values(#{seckillId},#{userPhone},0)
</insert>
<!--根据id查询SuccessKilled并携带Seckill实体-->
<!--如何告诉Mybatis把结果映射到SuccessKilled同时映射seckill属性-->
<!--可以自由控制SQL-->
<select id="queryByIdWithSeckill" resultType="Successkilled">
select
sk.seckill_id,
sk.user_phone,
sk.create_time,
sk.state,
<!--通过别名的方式来将查询结果封装到Successkilled的seckill属性中-->
s.seckill_id "seckill.seckill_id",
s.name "seckill.name",
s.number "seckill.number",
s.start_time "seckill.start_time",
s.end_time "seckill.end_time",
s.create_time "seckill.create_time"
from success_skilled sk
inner join seckill s on sk.seckill_id=s.seckill_id
where sk.seckill_id=#{seckillId} and sk.user_phone=#{userPhone}
</select>
</mapper>
接下来开始MyBatis和Spring的整合,整合目标:
1.更少的编码:只写接口,不写实现类。
2.更少的配置:别名、配置扫描映射xml文件、dao实现。
3.足够的灵活性:自由定制SQL语句、自由传结果集自动赋值。
在resources包下创建一个spring包,里面放置spring对Dao、Service、transaction的配置文件。
在spring包下创建一个spring配置dao层对象的配置文件spring-dao.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-aop-4.3.xsd
http://www.springframework.org/schema/c
http://www.springframework.org/schema/c/spring-aop-4.3.xsd ">
<!-- 引入db.properties-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<!-- 配置c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库的基本配置 -->
<property name="driverClass" value="${db.driver}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<!-- 配置c3p0的属性 -->
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="10"></property>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="1000"></property>
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 配置 SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis全局配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--扫描entity包 使用别名-->
<property name="typeAliasesPackage" value="org.seckill.entity"></property>
<!--扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 配置扫描dao接口包,动态实现dao接口,注入spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--当sqlSessionFactory bean启动的时候,properties还未加载,所以通过BeanName后处理的方式-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--给出需要扫描Dao接口包-->
<property name="basePackage" value="org.seckill.dao"></property>
</bean>
</beans>
这样便完成了Dao层编码的开发,接下来就可以利用junit进行Dao层编码的测试了。首先测试SeckillDao.java,利用IDEA快捷键shift+command+T
对SeckillDao.java进行测试,然后IDEA会自动在test包的java包下为生成对SeckillDao.java中所有方法的测试类SeckillDaoTest.java,内容如下:
package org.seckill.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.seckill.entity.Seckill;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
/**
* Created by joshul on 2017/1/3.
*/
//junit启动时加载springIOC容器
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class SeckillDaoTest {
//注入Dao实现类依赖
@Resource
private SeckillDao seckillDao;
@Test
public void queryById() throws Exception {
long id = 1000L;
Seckill seckill = seckillDao.queryById(id);
System.out.println(seckill.getNumber());
System.out.println(seckill);
}
@Test
public void queryAll() throws Exception {
/**
* java不会保存形参的记录,queryAll(int offset,int limit)会被解释成queryAll(int arg0,int arg1)
* 所以会出现绑定错误,需要在接口的形参中使用@Param注解
*/
List<Seckill> seckills = seckillDao.queryAll(0,100);
for (Seckill seckill:seckills) {
System.out.println(seckill);
}
}
@Test
public void reduceNumber() throws Exception {
Date killTime = new Date();
int updateCount = seckillDao.reduceNumber(1000L,killTime);
System.out.println(updateCount);
}
}
SuccessSeckilledDaoTest
package org.seckill.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.seckill.entity.SuccessKilled;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.junit.Assert.*;
/**
*
*/
//junit启动时加载springIOC容器
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class SuccessSeckilledDaoTest {
@Resource
private SuccessSeckilledDao successSeckilledDao;
@Test
public void insertSuccessKilled() throws Exception {
Long id = 1001L;
long phone = 13877511732L;
int insertCount = successSeckilledDao.insertSuccessSeckilled(id,phone);
System.out.println("insertCount = " + insertCount);
}
@Test
public void queryByIdwithSeckill() throws Exception {
Long id = 1001L;
long phone = 13877511732L;
SuccessKilled successKilled = successSeckilledDao.queryByIdWithSeckill(id,phone);
System.out.println(successKilled);
System.out.println(successKilled.getSeckill());
}
}
到此,成功完成了Dao层开发及测试,接下来我们将进行Service层的开发工作。
下篇文章Java高并发秒杀API之Service层
网友评论