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. 秒殺系統業務分析
秒殺系統業務流程如下:
由圖可以發現,整個系統其實是針對庫存做的系統。用戶成功秒殺商品,對于系統的操作就是: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層