JavaWeb-MVC case

Three-tier optimization:

Comparison of MVC and three-tier structure: 

1. Three-tier architecture

      It is consistent with the goals of the MVC design pattern: all are to understand coupling and improve code reuse:

       Difference, the two have different perspectives on project understanding.

2. Three-layer composition:

Presentation layer (USL, User Show Layer; view layer)

                              a. Foreground: Corresponds to the View in MVC, which is used to interact with the user and display the interface

                                          jsp js html css jquery and other web front-end technology code location: WebContent

                              b.Background: For the Controller in MVC, it is used to control the jump and call the business logic layer

                                         Servlet (SpringMVC Struts2), located xxx. Servlet package in

Business Logic Layer (BLL, Business Logic Layer; Service Layer)

                               a. Receive the request call of the presentation layer

                               b. Assemble the data access layer, logical operation (addition, deletion, modification, deletion: check+delete),

                                      Generally located in the XXX. service package (can also become: XXX. manager, XX. bll)

Data Access Layer (DAL, Data Access Layer; Dao Layer)

                                a. Direct access to the database operation, atomic operation (addition, deletion, modification, and check)

                                      Generally located in the XXX.dao package

3. The relationship between the three layers:

The upper layer passes the request to the lower layer; the lower layer processes it and returns it to the upper layer.

The upper layer depends on the lower layer, and it depends on the understanding of the code-that is, to hold member variables. The premise of A must have B.

The Dao layer is possible only if there is a database, so the Dao layer depends on the database.

1.
    Recommendations for adding interfaces : interface development: first interface-then implementation class-
    service, dao join interface
    -interface and implementation class naming convention
        interface: interface, named I entity class Service IStudentService
                        IStudentDao    
        implementation class: implements named entity class ServiceImpl StudentServiceImpl
                        StudentDaoImpl
        interface: the package name where the entity class layer is located IStudentService,
            the package where the IStudentDao     interface is located: xxx.service xx.dao

        Implementation class: The package name of the entity class layer is Impl StudentServiceImpl,
            the package where the StudentDaoImpl implementation class is: xxx.service.impl xx.dao.impl

    When using the interface/implementation class in the future, it is recommended to write:
    interface x = new implementation class();
    IStudentDao studentDao = new StudentDaoImpl();

2. DBUtil general database helper class, which can simplify the amount of code in the Dao layer

The help class is generally recommended to be written in the xxx.util package


A
{

    a(){
        B.connection
    }
}

B
{
    static Connection connection =..
    b{
        
    }
}

Method refactoring: Extract the common code of multiple methods, write them separately in a method, and then introduce the method
a()
{     ..     c();     .. }



    

b()
{
    ..
    c();
    ..
}

c()
{
        [..
    ..    
    ...        
    ..]
}


Web debugging: the
difference between debugging with java code: the startup method is different

index.jsp ->index_jsp.java ->index_jsp.class 

jsp->java->class
Jsp translated into Java and compiled class files exist in the work directory in tomcat


10000

Pagination: 5 variables (attributes)

1. Total number of data (select count(*) from xxx, check database)
2. Page size (page capacity, number of data items displayed on each page) (user-defined)
3. Total number of pages (automatic calculation)
    800:10= 80
    total pages = total data / page size

    802:10= 800/10 +1;
    total number of pages = total number of data/page size + 1;

    -->General formula
    total number of pages = total number of data% page size==0? Total number of data/page size: total number of data/page size + 1;

Note: Timing of automatic calculation: When the total number of data and page size are assigned, the total number of pages is automatically calculated.


4. Current page number (user-defined)

5. Entity object collection (data collection of the current page): Depends on the database (check database)
    Assumption: 10 items per page (page size=10)

select * from student where id>=start and id<=end;


Number of pages start and end start and end equivalent writing
1 1-10 (number of pages-1)*10+1-number of pages*10
2 11-20
3 21-30


The start and end of a page of data:

    (Number of pages-1)*10+1-Number of pages*10

select * from student where sno>=(number of pages-1)*10+1 and sno<=number of pages*10;
this kind of paging SQL strictly depends on the data of sno. Once there is a gap (crack) in sno, it cannot satisfy every Page 10

-> This SQL conversion: 1. There is rownum 2 can not have rownum>xx
conversion core: Convert rownum from a pseudo column to a normal column of a temporary table.

select *from 
(
    select rownum r,t.*from
    (select s.* from student s order by sno asc) t   
 
) where r>=(页数-1)*10+1 and r<=页数*10;            


optimization:

select *from (
    select rownum r,t.*from
    (select s.* from student s order by sno asc) t
    where rownum<=page number*page size            
 
) where r>=(page number-1)*page size+ 1 ;                
  

1
2
3

6
7
8
9

...
11
12
13
..
21
22
23
..

    

    

The difference between dao and DBUtil:
dao is a database operation class that handles a specific class:
DBUtil is a general database operation class

Guess you like

Origin blog.csdn.net/Qmilumilu/article/details/86484161