当前位置:网站首页>Chapter I review of e-commerce spike products

Chapter I review of e-commerce spike products

2022-04-23 14:02:00 Ape feather

Chapter one Review of e-commerce spike products

Project environment and technology

learning environment :

  • Intellij IDEA 2021.3
  • Alibaba cloud ECS Or local Linux virtual machine , operating system centos 7.6
  • MySQL5.6 database ,Redis4.0.1 cache , Message queue rocketmq4.5,phantomjs Headless browser

Technology reserve :

  • understand SSM、SpringBoot Other framework
  • be familiar with Linux Basic commands
  • understand MySQL Common commands
  • understand Redis Common commands

Project framework design

Untitled

The data layer :

  • Business @Transcantional How annotations are handled — Indicates that the method is in a transaction , If any step in a transaction fails , The transaction will roll back
  • Data access layer Dao
  • Local cache 、 Application of centralized cache in product details page , Improve the efficiency of streaming reading

About the process

  • The whole page is based on HTML、CSS, Then based on JavaScript Of jQuery The library sends a request for dynamic interaction , To access layer controller General processing , And then we're based on SpringMVC Of controller The layer will call the corresponding service to the business layer ;
  • The business layer will call the data layer Dao, Manage data through transactions DaoMapper The addition, deletion, modification and query of data fall into the database , Finally to the local computer .

Untitled

  • Data model (Data Object): With the help of Mybatis Of ORM The operation will the table structure of the relational database , adopt XML The way , Defined as Java Of Object structure .
  • Domain model (Domain Model): Have a life cycle of an object ( establish 、 to update 、 Delete 、 Extinction ), It can be combined with a data model , For example, the user object is a domain model , It consists of the basic information of the user + User password information is composed of two data models .
    • Anemia model : The user objects in the project are designed as anemia models : It refers to having various attribute information and get、set Method , But no login is provided 、 Registration and other functions .
  • ViewObject: The model connected with the front end , Aggregation model for presentation .

Detailed design of the project

Untitled

Untitled

problem

Why put the inventory table of goods item_stock And commodity list item Separation ?

  • Inventory operations are time consuming 、 Low performance efficiency , In the course of commodity trading, inventory decreases , If merged into item In the table , Every time d The corresponding row will be locked .
  • If you separate the inventory table , Although each inventory reduction process will still add a line lock , You can split this table into another database , Sub database and sub table , Optimize the effect .

Cross domain request problem

  • Add a sentence supporting cross domain operation before the class with cross domain request problem ( Server solutions ):
@CrossOrigin(origins = {
    "*"}, allowCredentials = "true")

Untitled

  • The problem is : Because we do the design of front and rear end separation ,jQuery There will be cross domain restrictions , use ajax Request the corresponding web page url File location error , Static resource files and jQuery Dynamic requests are deployed separately , So in ajax Add a sentence to the request :
xhrFields:{withCredentials:true}, 
  • Because we need to solve the problem of session The problem of sharing ( client session share )

Untitled

in addition , image Safari The browser needs to close Stop Cross Station tracking and Stop all Cookie The option to , Cross domain requests can be used normally

Untitled

Cross domain awareness session

  • Cross domain awareness session Two problems need to be solved , The first is to solve cross domain problems , The second is to solve cross domain problems cookie Transmission problem

Cross-domain problem

  • Solve the cross domain problem as shown above , Used SpringBoot Self contained @crossOrigin annotation
@CrossOrigin(origins = {
    "*"}, allowCredentials = "true")
  • After the annotation is added , be-all http response On my head Access-Control-Allow-Origin * as well as Access-Control-Allow-Headers * Two heads , This can satisfy CORS Cross domain definition of , our ajax When you see these two headers, it is determined that the corresponding domain name receives any request from or not from the domain

Cross domain delivery cookie problem

  • Cross domain and cross domain delivery cookie It's a matter of two different latitudes , We have solved the cross domain problem in the above way , But to achieve cross domain awareness seesion It needs to be solved that under the premise of cross domain cookie Can also be passed on , At this time, you need to set another head , our @CrossOrigin become
@CrossOrigin(origins = {
    "*"}, allowCredentials = "true", allowedHeaders = "*")
  • Used allowCredentials after Access-Control-Allow-Credentials The head is set to true, At the same time, the front end is set xhrField:{withCredential:true} after , The browser ajax The corresponding... In the request cookie Head and back allowCredentials Work together to solve cross domain transmission cookie The problem of .
  • Because only... Is used in the course get and post Methods , Both of these methods can be used in cross domain requests , therefore allowedHeaders Can not add .

Global exception handler 404,405 problem

  • 404 The problem with the page is URL Can't access , The previous way of exception handling was to define BaseController Layer capture , But for not entering Controller Layer requests cannot be processed .
  • 405 The problem is, for example, what to pass url Parameter not transferred ,url Binding routing problem .

resolvent

  • Definition GlobalExceptionHandler, Add comments before class @ControllerAdvice Section oriented programming .
  • In profile application.properties Add spring.mvc.throw-exception-if-no-handler-found=true and spring.resources.add-mappings=false

Untitled

//  Exception handling class 
@ControllerAdvice
public class GlobalExceptionHandler {
    

    // controller After the layer throws an exception, it will be doError Method and make different judgments according to the exception type 
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public CommonReturnType doError(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception ex) {
    
        ex.printStackTrace();
        Map<String, Object> responseData = new HashMap<>();
        if (ex instanceof BusinessException) {
    
            BusinessException businessException = (BusinessException) ex;
            responseData.put("errCode", businessException.getErrCode());
            responseData.put("errMsg", businessException.getErrMsg());
        } else if (ex instanceof ServletRequestBindingException) {
    
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
            responseData.put("errMsg", "url Binding routing problem ");
        } else if (ex instanceof NoHandlerFoundException) {
    
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
            responseData.put("errMsg", " No corresponding access path found ");
        } else {
    
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
            responseData.put("errMsg", EmBusinessError.UNKNOWN_ERROR.getErrMsg());
        }
        return CommonReturnType.create(responseData, "fail");
    }

}

版权声明
本文为[Ape feather]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231342358549.html