Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Java second kill system

2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to achieve Java second kill system", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve Java second kill system"!

Content:

We will adopt the popular mvc development mode to realize the function of "display of goods list and its details to be killed in seconds". It is worth mentioning that the main database tables involved in this function module are "commodity information table item" and "commodity information item_kill to be killed".

First, the actual combat of the code of "list of goods to be killed in seconds"

(1) the first step is to develop the request method of "getting the list of items to be killed in seconds" in the ItemController controller. The source code is as follows:

/ / get product list @ RequestMapping (value = {"/", "/ index", prefix+ "/ list", prefix+ "/ index.html"}, method = RequestMethod.GET) public String list (ModelMap modelMap) {try {/ / get the list of items to be killed List list=itemService.getKillItems (); modelMap.put ("list", list); log.info ("get list of items to be killed-data: {}", list) } catch (Exception e) {log.error ("get the list of items to be killed-exception occurred:", e.fillInStackTrace ()); return "redirect:/base/error";} return "list";}

After getting the list information of the products to be killed, the controller will return the data list in the form of modelMap to the front-end page list.jsp for rendering! Among them, itemService.getKillItems () is mainly used to obtain the list information of items to be killed in seconds. The source code is as follows:

@ Autowiredprivate ItemKillMapper itemKillMapper;// gets the list of items to be killed @ Overridepublic List getKillItems () throws Exception {return itemKillMapper.selectAll ();}

(2) the next step is to develop the itemKillMapper.selectAll () method, which is mainly based on Mybatis to write dynamic Sql in the configuration file. The function of the Sql is to "get the list of items to be killed". The source code is as follows:

SELECT A. BETWEEN a.start_time AND a.end_time AND a.total, b.name AS itemName, (CASE WHEN (now () BETWEEN a.start_time AND a.end_time AND a.total > 0) THEN 1 ELSE 0 END) AS canKill FROM item_kill AS a LEFT JOIN item AS b ON b.id = a.item_id WHERE a.is_active = 1

The Sql,Debug here uses the Left Join left correlation query to obtain the list information in order to obtain the commodity information in the "Commodity Information Table", such as "Commodity name" and so on.

It is worth mentioning that here Debug also uses a trick, that is, to use a field canKill to indicate whether the current "product to be killed" can be killed / snapped up! The criteria for its judgment are:

When the remaining quantity / inventory of the goods to be killed, that is, the value of the total field is greater than 0, and "the current server time now () is within the range of the start time and end time of rush buying of goods to be killed in seconds", the value of canKill will be 1, which means it can be snapped up or killed in seconds. Otherwise, the value of canKill will be 0.

(3) at this point, the back-end code development of the functional module "waiting for seconds to kill goods list" has been completed! After the frontend initiates a request, the request will first arrive at controller and be called by mapping to a method through the request path url. The method of controller will first perform the most basic data verification, then obtain the real business data by calling the interface provided by service, and finally execute the real data query or data operation logic at the dao layer in service, and finally complete the operation of the entire business flow.

(4) the next step is to develop a page list.jsp to display "the list of items to be killed". Part of the core source code of the page is shown below, as shown below:

As you can see from this code, when the value of the canKill field is 1, you can click "details" to view it; otherwise, you will be prompted for the corresponding information! That is to say, the logical Debug of "judging whether it can be killed in seconds" is put on the back end to realize it!

(5) at this point, the front and rear code of the functional module "getting the list of goods to be killed in seconds" is finished, click to run the entire project, run the entire system in an external tomcat server, and observe the output information of the console. If there is no error, it shows that there is no problem with the code of the whole system at the syntax level. The following figure shows the home page of the entire second kill system and the project after it is running:

Although it is not very beautiful, Debug thinks it will make do with it. Haha!

Second, the actual combat of the code of "details of goods to be killed in seconds"

(1) the next step is to click "details" to view "details of goods to be killed". For this functional module, it is actually relatively simple, and its core is mainly based on the "primary key" for query. By the same token, you first need to develop a functional method to receive front-end requests in the ItemController controller, and the source code is as follows:

/ * get the details of the item to be killed in seconds * @ return * / @ RequestMapping (value = prefix+ "/ detail/ {id}", method = RequestMethod.GET) public String detail (@ PathVariable Integer id,ModelMap modelMap) {if (id==null | | id 0) THEN 1 ELSE 0 END) AS canKill FROM item_kill AS a LEFT JOIN item AS b ON b.id = a.item_id WHERE a.is_active = 1 AND a.id = # {id}

From the Sql, it is not difficult to see that it is actually adding "accurate query of the primary key" to the Sql of "getting the list of items to be killed"!

(3) finally, the detailed information is displayed in the info.jsp rendering of the page, which is shown in the following figure as part of the core source code of the page:

As can be seen from part of the core source code of the page, in order to avoid someone from "skipping the request of the page and directly maliciously swiping the back-end interface", a judgment is still made on the page (when the "panic purchase / second kill" request is executed later, the back-end interface will judge again, all for security reasons! )

(4) at this point, the front and rear code of the function of "detailed display of goods to be killed in seconds" has been completed! Once again, run the entire system / project in an external tomcat server, and click the "details" button in the list page to see the details of the items to be killed, as shown in the following figure:

At this point, I believe that everyone on "how to achieve Java second kill system" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report