Java Web (XIV) write a summary of mybookstore project

The reason why I haven't posted a blog these days is that I'm writing a small project of the book city as the final precipitation of web learning. Next, I'm going to study the framework. The project will share the source code link at the end. Students in need can get their own play

                    --WZY

1、 Project introduction

A small project of a training institution found on the Internet is called the bookstore shopping website, which is divided into front and background. The front desk is used to display various types of books and commodities, can be used to join the shopping cart, can buy, pay and place orders, and the background is used to add, delete and modify books and book categories, and process orders. It's not a very complicated project, but I really don't have a clue and can't start before writing. That's because I don't have a general understanding of the project and don't know the hierarchical modules, so it's hard to start writing code. I'll listen to me slowly later, and I'll tell you my writing ideas and your ideas step by step. Before, Let's show you the effect first.

Front page

Before logging in, you can view all book commodities, that is, click all categories, or find books of corresponding categories by categories (12 commodities are displayed on each page, with paging function)

Then register. AJAX is used to verify the registered user name. In order not to duplicate the name, and the account can be activated by sending an activation code email. Users who are not activated cannot log in.

After logging in, add items to the shopping cart

、              

View your own orders (if you already have data in advance). Some orders can be paid before payment. After payment, wait for the background to click shipment, and after shipment, wait for you to click confirm harvest.  

For order payment, select a bank and jump to the page of which bank to pay. As an experiment, only one penny will be paid each time, and a test account will be paid. You can also apply by yourself, using the Yibao payment platform

The front desk is probably like this. Now let's talk about the backstage. The backstage is very simple. Just manage these goods.

Background home page

The functions are clearly written in the list on the left to manage classification, books and orders, including addition, deletion, modification and query. I won't introduce too much here. There is a small logical management for order management, which can ship paid orders. Then wait for the foreground user to click to confirm the harvest. After confirming the harvest, it will become a completed order.

2、 Analysis project structure module

When I first got the front shelf of this project, I can only say that it seems very simple to log in, register, view book information, join the shopping cart, pay and place orders, and click to go. However, I don't have a clue about it, but I've finished writing it, You will have an overall understanding of the project, which is equivalent to a clear feeling when you look at the project from the perspective of God. Let's talk about writing ideas first.

First, understand the database design, clarify the relationship, and then understand which modules the project needs to be divided into. Understand these, basically have a general understanding, you can write code to realize it.

2.1 database design.

Five tables, now analyze them one by one,

The user table is used for login registration, user identification and management.

Uid: unique identification code. UUID is used to obtain a 32-bit string to identify the user.

Username: the user name when logging in. It is unique and cannot be repeated.

Password: the login password is not encrypted. Generally speaking, encryption should be used, so you can't see the original password when viewing the data in the database.

Email: mailbox used to receive activation mail

State: status ID of whether to activate. 1 is active and 0 is inactive

CDKEY: activation code.

In order to understand the email, state and CDKEY fields, you need to know the process of sending activation code e-mail. First, when registering, state is 0 and inactive. Registration will create an activation code, assuming 123, CDKEY = 123, and then send an activation e-mail to the mailbox left by the user through e-mail, The content is a hyperlink with two parameters. One parameter stores the 123 and the other parameter stores the uid to identify the user. Clicking the hyperlink will jump to a servlet processing activation. Compare the obtained activation code parameter content with the CDKEY in the database to see if it is the same. If it is the same, it will be activated. Use the uid to change the user state to 1, If it is different, it means that the user did not click the activation code email and will not be processed.

Category table, the category table of book classification, such as fantasy, romance, etc. The table is relatively simple. You can see from the item introduction above that the category next to the home page is the data obtained from the table.

CID: unique identification code. Also use UUID to obtain a 32-bit string to serve as the value

CNAME: category name.

Book table, book table, used to store all book information.

Bid: unique identification code

BName: Book Name

Price: the book price adopts the decimal type, with 2 as the decimal point. That is, the price is accurate to points

Author: book author

Image: the pictures of books are stored in the path of the pictures, not in the database

CID: refers to the CID field in the category table. It is a foreign key field to identify the category of the book.

Isdel: This is the status of the book. Is it off the shelf? This field is not used in the project I wrote. I didn't write the logic code of this field.

After analysis, the book table is linked with the category table. Each book will have an exclusive category, so that the corresponding books can be queried by category. Think about the corresponding books found by category on the front page for display, which is solved through the relationship between the two tables.

Orders table

Oid: unique identification code

Total: price, that is, there may be multiple goods in this order, and the total price of multiple goods is total

Ordertime: order time, type, time

State: order status. There are four statuses, which are identified by 1, 2, 3 and 4 respectively. 1: unpaid, 2: paid, to be shipped, 3: shipped, to be received, 4: received, order completed,

Address: address. You must have an address for delivery.

Uid: refers to the uid field in the user table. It is a foreign key field that identifies the user of the order.

Through the orders table, the user table will be linked. When the foreground wants to display each user's own order, it will query all their orders through the uid field in the orders table.

Orderitem table, order item table. Because there may be multiple items in an order, an orderitem record must be generated for each item, so that you can find how many items in the order through an order.

Itemid: unique identification code

Count: the purchase quantity of a commodity. For example, if you buy two books of a at a time, because they are two books of a, you don't need to generate two orderitem records.

Subtotal: how much is the total price

Bid: refers to the bid of the book table. It is a foreign key field that identifies which book the order item is

Oid: refers to the oid in the orders table. It is a foreign key field that identifies which order the order item belongs to.  

This table links three tables together: Book table, orders table, orderitem table and user table. Their relationship should also be clear. A record in the orders table is an order, who the order belongs to (through the uid field in the order), how many kinds of goods there are in the order (through the oid field in the orderitem table), and the information of each kind of goods in the order (through the bid field in the orderitem table). In fact, what we are talking about is a one to many correspondence, in which foreign key fields are stored on the many side. You can analyze the relationship between these five tables and find who through who.

2.2 analysis foreground module

User module

Login, register

Register

Ajax asynchronous verification user name

Send activation email

Activation complete

Login

Judge whether the user account and password are activated, and store them in the session

Exit

Destroy session

Category module

Query all classifications.

The classification information is displayed on the home page, and all category information is taken from the category table

Book module

Display all books, query books according to classification, and query book details according to book ID.

Click the category in the classification module to display the books under the category. Click all categories to display all books. Here, you can use the category table and book table to query. Click one of the books to display the detailed information of the book and prompt the purchase button. This button is to add its books to the shopping cart

Shopping Cart module

Add to cart, remove items from cart, empty cart

A map is used here as the container of the shopping cart. LinkedHashMap has the advantage of fast access. Its principle is to create a JavaBean of the shopping cart item, which has three attributes: Book object, purchase quantity account and total amount allmoney. These three attributes are used to identify a commodity, The shopping cart uses map as, map < string, cartitem >. String: bid (the unique identification code of book) is loaded, and cartitem is the shopping cart item loaded.

Add book a to the shopping cart,

First, encapsulate book a into a cartiitem object. The book attribute is all the information of book A. account represents the number of books a added to the shopping cart this time, and allmoney represents the total amount

After it is encapsulated into cartitem, add it to cart (shopping cart, that is, map collection)

Compare the unique identification code bid of book a with all key values in the cart to judge whether book a already exists in the cart

If a Book exists, find the cartitem whose key is bid in the cart, and modify the account and allmoney in the cartitem.

If there is no book a, store the book a in the cart, use the bid of book a as the key value, and use the cartitem encapsulated in book a as the value,

Store the cart (shopping cart, that is, map set) into the session scope. If the session scope is saved, the user's shopping cart cannot be saved permanently. According to the characteristics of the session, closing the browser will save it for 30 minutes by default. If the cookie is not set to the browser for saving, closing the browser will lose the sessionid, Session cannot be found, that is, the content in the shopping cart is lost, so the feature of saving in session is what I said. You can also save the shopping cart in the database, so users can find their own shopping cart whenever and where they log in.

Order module

Generate an order, query the order according to the user, query the order item according to the order ID, online payment function, and modify the order status

After adding the goods to the shopping cart, you can click to make payment. At this time, you will jump to the page of selecting payment method and generate an order. At this time, the order state is 1, which is in unpaid status. After writing the address, you can make payment and jump to the payment page. After payment is successful, you will jump back to our own page, which is 2 It is called paid consignment status. When an order is generated, an order item will also be generated, that is, an order item will be generated for each commodity. Its business operations are completed in the service layer.

2.3 analysis background module

Category management operates on the category table.

1. Query classification and find all classification information

2. Add classification,

3. Modify classification

4. Delete classification

Library management

1. Query books

Find out all the book information

2. Add books

File upload (upload picture information)

3. Modify books

Modify Book pictures or other information

4. Books off the shelf (deleted)

Order management

1. Query orders in all statuses

2. Query orders in various statuses (unpaid, paid to be shipped, shipped to be confirmed for receipt, and confirmed harvest order completion)

3、 Writing process

3.1. Build project environment

It uses a three-tier classic architecture, including web layer, service layer and Dao layer. If you want to use interface programming, you are dividing interface classes and implementation classes.

My package structure

Add all required jar packages.

MySQL: 1 jar package

C3p0 connection pool: 1 Jar

Dbutils framework: 1 Jar

File upload: 2 jars IO and core jars (fileUpload)

Mail sending: 1 Jar JavaMail

Jsp: 2 jars, JSTL and El expressions (because using JSP may require the use of custom tags or other tags, so bring them in first to prevent unexpected needs)

BeanUtils: 2 jars BeanUtils and logging. (I don't use BeanUtils)

3.2. Code writing

1. After the development environment is built, first write the code connecting to the database using the dbutils framework.

2. Write according to the above modules. First, register and log in the user module, and write from JSP to servlet, service layer and Dao layer. It is written in this order.

3. Everything else is done step by step. When you really start writing, everything will go on according to your own ideas, rather than having no way to start as at the beginning.

3.3. Attention,

The reflection mechanism is used when writing servlets to reduce the number of servlets. There are two ways to simplify the number of servlets.

The first.

In the old way, inherit the httpservlet, rewrite the doget and dopost methods, pass the servlet method to be accessed in the form of parameters when sending the request, then judge which method the request parameters need to call in the servlet, and write the method body under the servlet, Just call it in the doget or dopost method.

Second

Using reflection mechanism, reflection loading corresponding method. At this time, we need to understand the three methods experienced in the servlet life cycle. The most important thing is to know that accessing the servlet will go through the service method. Once you know this, it's easy to do everything. Write a passed servlet named baseservlet.

Analysis: write all the methods in a new servlet class, which inherits the baseservlet. When accessing a new servlet class, because the service method needs to be executed but not in the subclass, you can find and execute it in the parent baseservlet. In the parent class, you will get the name of the method to be executed and execute the corresponding method in the subclass through the reflection mechanism. Therefore, the method to be executed and the service can only be separated from the code, All of the subclasses are logical code that needs to be written. There is no need to judge the method to select the corresponding method for execution, which saves a lot of trouble and makes the code more concise and easy to understand.

4、 Summary

Through this project, I have a deeper understanding of the three-tier classic architecture, and consolidated the large and small knowledge points in the web. I have a lot of knowledge about the four scopes in JSP, nine built-in objects, the use of El expression, sending mail, online payment and so on. Moreover, the mode of Servlet + jsp + database is really complex, Thus we know the benefits of using the framework and facilitate development.

Now donate the source code of the project, hoping to help you.

       http://pan.baidu.com/s/1i4W79BZ     67me

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>