In the previous section, we completed the detailed page of the product and used Hibernate's secondary cache to load the detailed page to improve the performance of the system. Click the article to view:. In this section we start doing the shopping cart part.
1. Add a new table
First, we add several tables to the database: user table, order status table, order table (shopping cart table) and shopping item table. The user's basic information is stored in the user table. The order status table mainly stores the status of the order, such as shipped. The order table mainly stores the user's information and the status of the order, so it is related to the user table and the order status table. The shopping item table stores a certain product and the order it belongs to, so it is related to the product table and the order table. For specific table information, see the following SQL statement:
/*======================================================================================================================================================================/ create table user ( /* User number, automatic growth*/ id int primary key not null auto_increment, /* User login name*/ login varchar(20), /* User real name*/ name varchar(20), /* User login password*/ pass varchar(20), /* User gender*/ sex varchar(20), /* User phone*/ phone varchar(20), /* User email */ email varchar(20) ); /*=====================================================================================================================/ /* Table: Shopping cart (order) table structure*/ /*======================================================================================================================================================================================================================================================================================= create table forder ( /* Order number, automatic growth*/ id int primary key not null auto_increment, /* Recipient name*/ name varchar(20), /* Recipient phone number*/ phone varchar(20), /* Delivery information*/ remark varchar(20), /* Order date*/ date timestamp default CURRENT_TIMESTAMP, /* Total order amount*/ total decimal(8,2), /* Recipient postal code*/ post varchar(20), /* Recipient postal code*/ address varchar(200), /* Order status*/ sid int default 1, /* Member number*/ uid int, constraint sid_FK foreign key(sid) references status(id), constraint uid_FK foreign key(uid) references user(id) ); /*=========================================================================================================================================================================================================================================================================================================================================================================================================================================================================== Price of the product at the time of purchase*/ price decimal(8,2), /* Quantity purchased*/ number int not null, /* Product number*/ pid int, /* This line item, the order number*/ fid int, constraint pid_FK foreign key(pid) references product(id), constraint fid_FK foreign key(fid) references forder(id) );
Then we convert these tables into POJO through reverse engineering, so we won't go into details.
2. Backstage logic of shopping cart
2.1 Logic of Service Layer
When a user adds a product to the shopping cart, we first need to obtain the product information through the product id, and then add the product to the shopping cart. Before adding, we must first determine whether there is a shopping cart in the current session. If not, we must first create a shopping cart. If so, we will add the current shopping item to the shopping cart. Before adding, we need to determine whether the shopping item already exists in the shopping cart. If it exists, just increase the corresponding shopping quantity. If it does not exist, add it, then calculate the total shopping price, and finally save the shopping cart to the session. See the diagram below for the entire process:
Next, let’s implement the specific logic, first create two new Service interfaces: SorderService and ForderService. There are two methods in SorderService: convert the items added by the user into shopping items, and then add the shopping items to the shopping cart; ForderService mainly defines the method of calculating the total price of the shopping cart, as follows:
//SorderService interface public interface SorderService extends BaseService<Sorder> { //Add shopping item and return a new shopping cart public Forder addSorder(Forder forder, Product product); //Convert product data into shopping item public Sorder productToSorder(Product product); } //ForderService interface public interface ForderService extends BaseService<Forder> { //Calculate the total shopping price public double cluTotal(Forder forder); } Then we implement these two interfaces in detail:
//SorderServiceImpl implementation class @Service("sorderService") public class SorderServiceImpl extends BaseServiceImpl<Sorder> implements SorderService { @Override public Forder addSorder(Forder forder, Product product) { boolean isHave = false; // Used to mark whether there are duplicate shopping items//Get the current shopping item Sorder soorder = productToSorder(product); //Judge whether the current shopping item is duplicate. If it is duplicate, add the quantity for(Sorder old: forder.getSorders()) { if(old.getProduct().getId().equals(sorder.getProduct().getId())) { //There are duplicates for shopping items, add the quantity old.setNumber(old.getNumber() + soorder.getNumber()); isHave = true; break; } } //The current shopping item does not exist in the shopping cart, just add it if(!isHave) { forder.getSorders().add(sorder); } return forder; } @Override public Sorder productToSorder(Product product) { Sorder soorder = new Sorder(); soorder.setName(product.getName()); soorder.setNumber(1); soorder.setPrice(product.getPrice()); soorder.setProduct(product); return soorder; } } //ForderServiceImpl implementations @Service("forderService") public class ForderServiceImpl extends BaseServiceImpl<Forder> implements ForderService { @Override public double cluTotal(Forder forder) { double total = 0.0; for(Sorder soorder : forder.getSorders()) { total += soorder.getNumber() * soorder.getPrice(); } return total; } } Then we need to inject these two beans into BaseAction for use by SorderAction:
@Controller("baseAction") @Scope("prototype") public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> { //Omit other irrelevant code... @Resource protected ForderService forderService; @Resource protected SorderService sorderService; } OK, the logic of the Service layer is finished, and next we are going to do the Action part:
2.2 Logic of Action
We create a new SorderAction and follow the process shown on the above logic diagram to complete the logic of adding shopping carts. The code is as follows:
@Controller @Scope("prototype") public class SorderAction extends BaseAction<Sorder> { public String addSorder() { //1. Obtain the corresponding product data according to product.id Product product = productService.get(model.getProduct().getId()); //2. Determine whether there is a shopping cart at the current session. If not, create if(session.get("forder") == null) { //Create a new shopping cart and store it in session session.put("forder", new Forder(new HashSet<Sorder>())); } //3. Convert product information into soorder and add it to the shopping cart (judging whether the shopping item is duplicated) Forder forder = (Forder) session.get("forder"); forder = soorderService.addSorder(forder, product); //4. Calculate the total price of shopping forder.setTotal(forderService.cluTotal(forder)); //5. Store the new shopping cart in session session.put("forder", forder); return "showCart"; } } Configure the struts.xml file:
<action name="sorder_*" method="{1}"> <result name="showCart">/showCart.jsp</result> </action>Then jump to the shopping cart display page showCart.jsp. The front-end program about the shopping cart part in showCart.jsp is as follows:
3. Jump of front desk link
All the background parts are done. Next, add the front desk detail.jsp page to the link address of the shopping cart to access SorderAction:
This will jump correctly. Let’s take a look at the specific effect of the shopping cart display page:
In this way, the basic functions of our shopping cart will be completed, and we will make some improvements to it later.
Original address: http://blog.csdn.net/eson_15/article/details/51418350
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.