Summary of SSH framework interview knowledge points

Time:2021-11-29

Differences among JDBC, hibernate and ibatis

JDBC is a set of database access standards, which provides a set of interfaces. It is used by java language to access databases

Hibernate is a set of ORM (object relational mapping) mapping framework, which provides access to data in the form of objects, but the underlying object still needs to be mapped to JDBC for access.

Ibatis is also a database access framework, but it extracts SQL statements, so that there is no need to modify the code when changing statements in the future, and the underlying layer is JDBC

How hibernate works and why?

Principle:

  1. Read and parse configuration files

  2. Read and parse the mapping information to create sessionfactory

  3. Open session

  4. Create transaction transition

  5. Persistent operation

  6. Commit transaction

  7. Close session

  8. Close the sessionfactory

Why use:
  1. The code of JDBC accessing database is encapsulated, which greatly simplifies the cumbersome repetitive code of data access layer.

  2. Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. It greatly simplifies the coding of Dao layer

  3. Hibernate uses java reflection mechanism instead of bytecode enhancement program to achieve transparency.

  4. Hibernate performs very well because it is a lightweight framework. The flexibility of mapping is excellent. It supports a variety of relational databases, from one-to-one to many to many complex relationships.

How does hibernate delay loading?
  1. Hibernate2 deferred loading implementation: a) entity object B) collection

  2. Hibernate 3 provides deferred loading of properties

When hibernate queries the data, the data does not exist in memory. When the program really operates on the data, the object exists in memory, which realizes delayed loading. It saves the memory overhead of the server and improves the performance of the server.

3. How to implement the relationship between classes in Hibernate? (e.g. one to many and many to many relationships)

The relationship between classes is mainly reflected in the operation of the relationship between tables. They operate on objects. All tables and classes are mapped together in our program through many to one, one to many, many to many

4. Let’s talk about hibernate’s caching mechanism
  1. The internal cache exists in Hibernate, which is also called level-1 cache and belongs to application transaction level cache

  2. L2 cache:

a) Application and cache

b) Distributed cache

Conditions: the data will not be modified by the third party, the data size is within the acceptable range, the data update frequency is low, the same data is frequently used by the system, and it is non critical data

c) Implementation of third-party cache

5. Query method of Hibernate

Sql、Criteria,object comptosition

Hql:

1. Attribute query

2. Parameter query, named parameter query

3. Association query

4. Paging query

5. Statistical function

6. How to optimize Hibernate?

1. Use two-way one to many association instead of one-way one to many association

2. Flexible use of one-way one to many Association

3. Instead of one-to-one, use many to one instead

4. Configure object cache instead of collection cache

5. Bag is used for one to many sets and set is used for many to many sets

  1. Inheritance classes use explicit polymorphism

  2. There should be fewer table fields. Don’t be afraid of more table associations. It is supported by L2 cache

7. How does struts work? Why use struts?

Working mechanism:

Struts workflow:

When the web application starts, it will load and initialize the actionservlet from

Read the configuration information from the struts-config.xml file and store them in various configuration objects

When actionservlet receives a customer request, it will execute the following process

-(1) Retrieve the actionmapping instance matching the user’s request. If it does not exist, return the invalid request path information;

-(2) If the ActionForm instance does not exist, create an ActionForm object and save the form data submitted by the customer to the ActionForm object;

-(3) Determine whether form validation is required according to the configuration information. If validation is required, call the validate () method of ActionForm;

-(4) If the validate () method of ActionForm returns null or returns an actuaberrors object that does not contain actionmessage, the form verification is successful;

– (5) ActionServlet decides which Action to forward to the request according to the mapping information contained in ActionMapping. If the corresponding Action instance does not exist, create the instance first, then call the execute () method of Action;

-(6) The execute () method of action returns an actionforward object. The actionservlet forwards the customer request to the JSP component pointed to by the actionforward object;

-(7) The actionforward object points to the JSP component to generate a dynamic web page and return it to the customer;

Why use:

The emergence of JSP, servlet and JavaBean technology makes it possible for us to build a powerful enterprise application system. However, the systems built with these technologies are very messy, so on this basis, we need a rule and a rule to organize these technologies. This is the framework, and struts came into being.

The application developed based on Struts consists of three types of components: controller component, model component and view component

How is the validate framework of struts validated?

Configure the specific error prompt in the struts configuration file, and then call the validate () method in the formbean.

Let’s talk about the design pattern of struts

MVC mode: the actionservlet will be loaded and initialized when the web application starts. When the user submits the form, a configured ActionForm object is created and filled with the corresponding data of the form. The actionservlet determines whether the form verification is required according to the settings configured in the struts-config.xml file. If necessary, call the validate() of ActionForm to verify and select which action to send the request to. If the action does not exist, ActionServlet will create the object first and then call the execute () method of Action. Execute () obtains data from the ActionForm object, completes the business logic, and returns an actionforward object. The actionservlet forwards the customer request to the JSP component specified by the actionforward object. The JSP specified by the actionforward object generates a dynamic web page and returns it to the customer.

Spring working mechanism and why?

1. Spring MVC requests that all requests be submitted to the dispatcher servlet, which will entrust other modules of the application system to be responsible for the real processing of the requests.

2. Dispatcher servlet queries one or more handlermapping and finds the controller that handles the request

3. Please submit the dispatcher servlet request to the target controller

4. After business logic processing, the controller will return a modelandview

5. The dispatcher queries one or more viewresolver view parsers to find the view object specified by the modelandview object

6. The view object is responsible for rendering and returning to the client.

Why:

{AOP allows developers to create non behavioral concerns, called crosscutting concerns, and insert them into application code. With AOP, public services (such as logs, persistence, transactions, etc.) can be decomposed into aspects and applied to domain objects without increasing the complexity of the object model of domain objects.

IOC allows you to create an application environment that can construct objects, and then pass their collaboration objects to these objects. As word inversion shows, IOC is like JNDI in reverse. Instead of using a bunch of abstract factories, service locators, singletons and straight construction, each object is constructed with its collaboration object. Therefore, the container manages the collaborator.

Even if spring is an AOP framework, it is also an IOC container. The best thing about spring is that it helps you replace objects. With spring, just add dependencies (collaboration objects) with JavaBean properties and configuration files. Collaboration objects with similar interfaces can then be easily replaced when needed.}

The spring framework is a layered architecture consisting of seven well-defined modules. The spring module is built on top of the core container, which defines how beans are created, configured, and managed, as shown in Figure 1.

Each module (or component) that makes up the spring framework can exist alone or be implemented jointly with one or more other modules. The functions of each module are as follows:

☆ core container: the core container provides the basic functions of spring framework. The main component of the core container is beanfactory, which is the implementation of the factory pattern. Beanfactory uses the inversion of control (IOC) pattern to separate the configuration and dependency specifications of the application from the actual application code.

☆ spring context: Spring context is a configuration file that provides context information to the spring framework. The spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, checksum and scheduling capabilities.

☆ spring AOP: through the configuration management feature, the spring AOP module directly integrates aspect oriented programming functions into the spring framework. Therefore, you can easily make any object managed by the spring framework support AOP. The spring AOP module provides transaction management services for objects in spring based applications. By using spring AOP, declarative transaction management can be integrated into applications without relying on EJB components.

☆ spring Dao: the JDBC Dao abstraction layer provides a meaningful exception hierarchy, which can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written (such as opening and closing connections). Spring Dao’s JDBC oriented exceptions follow a common Dao exception hierarchy.

☆ spring ORM: the spring framework inserts several ORM frameworks to provide ORM object relationship tools, including JDO, hibernate and ibatis SQL map. All of this follows spring’s common transaction and Dao exception hierarchy.

☆ spring web module: the Web context module is built on the application context module and provides context for web-based applications. Therefore, the spring framework supports integration with Jakarta struts. The web module also simplifies processing multipart requests and binding request parameters to domain objects.

☆ spring MVC framework: MVC framework is a full-featured MVC implementation for building web applications. Through the policy interface, the MVC framework becomes highly configurable. MVC accommodates a large number of view technologies, including JSP, velocity, tiles, iText and poi.

The functions of the spring framework can be used in any J2EE server, and most of them are also applicable to an unmanaged environment. The core point of spring is to support reusable business and data access objects that are not bound to specific J2EE services. There is no doubt that such objects can be reused between different J2EE environments (web or EJB), stand-alone applications, and test environments.

IOC and AOP

The basic concept of the inversion of control pattern (also known as dependency intervention) is not to create objects, but to describe how to create them. The code does not directly connect with objects and services, but describes which component needs which service in the configuration file. The container (IOC container in the spring framework) is responsible for linking these together.

In a typical IOC scenario, the container creates all objects and sets the necessary properties to connect them together to decide when to call the method. The following table lists one implementation mode of IOC.

The IOC container of spring framework is implemented with type 2 and type 3.

Aspect oriented programming

Aspect oriented programming, or AOP, is a programming technology that allows programmers to modularize the behavior of crosscutting concerns or typical responsibility boundaries (such as log and transaction management). The core construct of AOP is aspect, which encapsulates the behaviors that affect multiple classes into reusable modules.

AOP and IOC are complementary technologies. They both use a modular approach to solve complex problems in enterprise application development. In a typical object-oriented development mode, logging statements may be placed in all methods and Java classes to realize the logging function. In AOP mode, log services can be modularized in turn and applied to components requiring logs in a declarative manner. Of course, the advantage is that Java classes do not need to know the existence of logging services, nor do they need to consider the relevant code. Therefore, the application code written in spring AOP is loosely coupled.

AOP functionality is fully integrated into the context of spring transaction management, logging, and various other features.

IOC container

The core of spring design is the org. Springframework. Beans package, which is designed to be used with JavaBean components. This package is usually not used directly by the user, but by the server as the underlying intermediary for most other functions. The next highest level of abstraction is the beanfactory interface, which is an implementation of the factory design pattern that allows objects to be created and retrieved by name. Beanfactory can also manage relationships between objects.

Beanfactory supports two object models.

□ the singlet model provides shared instances of objects with specific names, which can be retrieved during query. Singleton is the default and most commonly used object model. Ideal for stateless service objects.

□ the prototype model ensures that a separate object is created for each retrieval. When each user needs his own object, the prototype model is most suitable.

The concept of bean factory is the foundation of spring as IOC container. IOC shifts responsibility for handling things from the application code to the framework. As I will demonstrate in the next example, the spring framework uses JavaBean properties and configuration data to indicate the dependencies that must be set.

Beanfactory interface

Because org.springframework.beans.factory.beanfactory is a simple interface, it can be implemented for various underlying storage methods. The most common definition of beanfactory is xmlbeanfactory, which loads beans according to the definition in the XML file, as shown in Listing 1.

Listing 1. Xmlbeanfactory

BeanFactory factory = new XMLBeanFactory(new FileInputSteam(“mybean.xml”));

The bean defined in the XML file is passively loaded, which means that the bean itself will not be initialized before it is needed. To retrieve beans from beanfactory, simply call the getBean () method and pass in the name of the bean to be retrieved, as shown in Listing 2.

Listing 2. GetBean ()

MyBean mybean = (MyBean) factory.getBean(“mybean”);

Each bean can be defined as a POJO (property definition initialized with class name and JavaBean) or a factorybean. The factorybean interface adds an indirect level to applications built using the spring framework.

IOC example

The easiest way to understand inversion of control is to look at its practical application. In concluding part 1 of the three part spring series, I used an example to demonstrate how to inject dependencies into applications through the spring IOC container (rather than build them in).

I use the use case of opening an online credit account as a starting point. For this implementation, opening a credit account requires the user to interact with the following services:

☆ credit rating service to query the user’s credit history information.

☆ remote information link service, which inserts customer information and connects customer information with credit card and bank information for automatic debit (if necessary).

☆ e-mail service, which sends e-mail about credit card status to users.

Three interfaces

For this example, I assume that services already exist, ideally integrating them in a loosely coupled manner. The following listing shows the application interfaces for the three services.

Listing 3. Creditratinginterface

public interface CreditRatingInterface {

public boolean getUserCreditHistoryInformation(ICustomer iCustomer);

}

The credit rating interface shown in Listing 3 provides credit history information. It requires a customer object that contains customer information. The implementation of this interface is provided by the creditrating class.

Listing 4. Creditlinking interface

public interface CreditLinkingInterface {

public String getUrl();

public void setUrl(String url);

public void linkCreditBankAccount() throws Exception ;

}

The credit link interface connects the credit history information with the bank information (if necessary) and inserts the user’s credit card information. The credit link interface is a remote service, and its query is performed through the geturl () method. The URL is set by the bean configuration mechanism of the spring framework, which I will discuss later. The implementation of this interface is provided by the creditlinking class.

Listing 5. Emailinterface

public interface EmailInterface {

public void sendEmail(ICustomer iCustomer);

public String getFromEmail();

public void setFromEmail(String fromEmail) ;

public String getPassword();

public void setPassword(String password) ;

public String getSmtpHost() ;

public void setSmtpHost(String smtpHost);

public String getUserId() ;

public void setUserId(String userId);

This work adoptsCC agreement, reprint must indicate the author and the link to this article

article!! Started on my blogStray_Camel(^U^)ノ~YO