Github: github.com/nnngu Project source: github.com/nnngu/nguSe…


This is a highly concurrent commodity kill project integrating IDEA+Maven+SSM framework. We will be divided into the following articles for detailed explanation:

  • Business analysis and DAO layer of Java high concurrency kill project
  • 02 Java High concurrency kill project Service layer
  • Java Web layer for high concurrency kill projects
  • High concurrency optimization for Java high concurrency SEC kill projects

Renderings of the project

Seconds kill list of goods

The second kill prompt page is displayed

Seconds kill end prompt interface

Create a New Maven project

To use IntelliJ IDEA as an example, go to File > New > Project > Maven

Then click Next to continue;

Fill in the relevant information and click Next;

Finally, click Finish to complete the creation.

If the following prompt pops up in the lower right corner, click Enable auto-import

Create the WebApp directory

Go to File > Project Structure

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6

Step 7

Step 8

Build the POM file

Now that the Maven project is created, we need to add some jar dependencies, namely the coordinates of the various open source components in POm. XML.

<?xml version="1.0" encoding="UTF-8"? >
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nnngu</groupId>
    <artifactId>nguSeckill</artifactId>
    <version>1.0 the SNAPSHOT</version>

        <! -- Code omitted, please refer to the project source code -->. .Copy the code

For the complete code, see the pom.xml file in the project

At this point, the initialization of our project is complete.

Analyze system services

The system service flow is as follows:

As can be seen from the figure, the whole system is actually made for inventory. If the user succeeds in killing goods in seconds, the operation of our system is:

  1. Inventory reduction

  2. Record the purchase details of users. Let’s take a look at our users’ business analysis of inventory:

Record the user’s SEC kill success information, we need to record: 1, who bought successfully. 2. Time/validity period of successful purchase. 3. Payment/shipment information. This data makes up the user’s second kill message, which is the user’s purchase behavior.

Why do we need transactions in our system? Look at the following faults: 1. If the user successfully kills the product in seconds, we record the purchase details but do not reduce the inventory. Lead to overselling of goods. 2. Reduced inventory without recording user purchase details. Lead to less sales of goods. For the above two failures, without the support of transactions, the biggest loss is undoubtedly our users and businesses. In MySQL, its built-in transaction mechanism can accurately help us complete the process of reducing inventory and recording user purchase details.

When user A kills the item whose ID is 10, MySQL needs to perform the following operations: 1. 2. Update the inventory information of goods. 3. Add the user’s purchase details, including the product ID of the user and the information that uniquely identifies the user, such as telephone number, etc. Commit transaction. If another user B is also waiting for the item whose ID is 10, he needs to wait until user A successfully kills the item and MySQL successfully commits the transaction before he can get the lock of the item whose ID is 10 and execute the transaction. However, it is impossible for user B to wait at the same time. There must be many, many users waiting for this row-level lock. The difficulty of seckilling is here, how to deal with these competition efficiently? How to efficiently complete transactions? How to optimize high concurrency is explained in the fourth module.

What do we need to accomplish with this system? Let’s take a look at a second kill inventory system on Tmall:

Do you think it’s complicated? Of course, don’t worry, we just realize some of the functions of seckill: 1, seckill interface exposure. 2. Execute the seckill operation. 3, related queries, such as list queries, details page queries. We can implement these three functions. The next specific coding work, the first is to establish the database and Dao layer coding.

Set up a database

Create a database
CREATE DATABASE ngu_seckill;

-- Use database
USE ngu_seckill;

-- ellipsis.... .Copy the code

The complete database SQL code is ngu_seckill.sql in the project SQL folder

Creating an entity Class

To create the first seconds kill commodity type com/nnngu/entity/Seckill. Java

package com.nnngu.entity;

import java.io.Serializable;
import java.time.LocalDateTime;

/** ** ** /
public class Seckill implements Serializable {

    private static final long serialVersionUID = 2912164127598660137L;
    / * primary key ID * /
    private long seckillId;
    /* Seckill commodity name */
    private String name;
	
	/* Code omitted, please refer to the project source code */. .Copy the code

Create com/nnngu/seconds kill state entity/SuccessKilled Java

package com.nnngu.entity;

import java.io.Serializable;
import java.time.LocalDateTime;

/** * The status after seconds */
public class SuccessKilled implements Serializable {
    private static final long serialVersionUID = 1834437127882846202L;

    private long seckillId;
    
    /* User's mobile phone number */
    private long userPhone;
	
	/* Code omitted, please refer to the project source code */. .Copy the code

Create the corresponding Mapper interface, also known as the DAO interface, for the entity class

com/nnngu/dao/SeckillMapper.java

package com.nnngu.dao;

import com.nnngu.entity.Seckill;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface SeckillMapper {
    /* Code omitted, please refer to the project source code */. . }Copy the code

com/nnngu/dao/SuccessKilledMapper.java

package com.nnngu.dao;

import com.nnngu.entity.SuccessKilled;
import org.apache.ibatis.annotations.Param;

public interface SuccessKilledMapper {
    /* Code omitted, please refer to the project source code */. . }Copy the code

Create the correspondingmapper.xml

Create the com.nNngu. Dao package in the Resources directory, then create seckillMapper. XML and SuccessKilledMapper. XML as shown below:

com.nnngu.dao/SeckillMapper.xml

<! -- code omitted here -->
<! -- Please refer to the project source code -->
Copy the code

com.nnngu.dao/SuccessKilledMapper.xml

<! -- code omitted here -->
<! -- Please refer to the project source code -->
Copy the code

Create the Mybatis configuration file Mybatis -config.xml

The contents of the mybatis-config. XML configuration file refer to the project source code

Create a configuration file to connect to the databasejdbc.properties

Note: The properties in jdbc.properties should be modified to suit your needs.

Create the Spring DAO configuration file

createapplicationContext-dao.xmlThe diagram below:

Refer to the project source code for the applicationContext-dao.xml file.

test

Create a test class com/nnngu/dao/SeckillMapperTest. Java figure as follows:

Please refer to the project source code for the specific code of this file.

The test results

QueryAll () tests the method to queryAll items

The test results are as follows:

At this point, we have successfully completed the Dao layer development and testing. The Service layer for Java High Concurrency kill Projects