Spring SpringMvc Mybatis framework detailed

SpringMVC

Understanding of springmvc:
1. It is based on component technology. All application objects, whether controllers and views, or business objects are java components, and are tightly integrated with other infrastructure provided by spring.
2. Does not depend on Servlet API (the goal is the same, but it is really dependent on Servlet when it is implemented)
3. Various view technologies can be used arbitrarily
4. Mapping strategies that support various requested resources.
5. Easy to expand

The workflow of SpringMVC:
1. The user sends a request to the front-end controller DispatcherServlet 2. The DispatcherServlet
receives the request and calls the HandlerMapping processor mapper.
3. The processor mapper finds the specific processor according to the request url, and generates the processing object and processor Intercept (if any) – and return to DispatcherServlet. 4. DispatcherServlet
invokes the processor through the HandlerAdaper processor adapter
5. Executes the processor (Controler, also called the back-end controller).
6. Controller execution completes Fan Lake ModelAndView
7. HandlerAdaper returns the execution result ModelAndView to DispatcherServlet 8. DispatcherServlet
passes ModelAndView to ViewReslover view parser
9. ViewReslover returns to specific View after parsing 10. DispatchServlet
renders View (that is, fills model data into view)
11. DispatcherServlet responds to users

MyBatis

Understanding
of mybatis 1. Configure
mybatis 2. SqlMapConfig.xml, this file is all the configuration files of mybatis, and configures the operating environment of mybatis and other information.
3. The mapper.xml file is the sql mapping file, and the operation database is configured in the file sql statement. This file needs to be loaded in SqlMapConfig.xml. 4.
Construct SqlSessionFactory or session factory through configuration information such as mybatis environment .
5. Create sqlSession or session by session factory, and operate the database through sqlSession.
The Executor executor interface operates the database. The Executor interface has two implementations, a basic executor and a cache executor.
7. Mapped Statement is also an underlying encapsulation object of mybatis, which wraps mybatis configuration information and sql mapping information, etc. mapper.xml A sql in the file corresponds to a Mapped Statement, which is also an underlying encapsulation object of mybatis. It wraps. It wraps mybatis configuration information and sql mapping information. A sql in the mapper.xml file corresponds to a Mapped Statement object, and the id of the sql is the id of the Mapped statement.
8. Mapped Statement defines the input parameters of sql execution, including HashMap, basic type, and pojo. Executor maps the input java object to sql through Mapped Statement before executing sql. Input parameter mapping is the parameter set for preparedStatement in jdbc programming.
9. Mapped Statement defines the output results of sql execution, including HashMap, basic types, and pojo. Executor maps the output results to java objects after executing sql through Mapped Statement. The output result mapping process is equivalent to the analysis of the results in jdbc programming. processing.

MyBatis programming steps
1. Create SqlSessionFactory
2. Create SqlSession through SqlSessionFactory
3. Perform database operations through sqlsession
4. Call session.commit() to submit things
5. Call session.close() to close the session

Mybatis's first-level cache and second-level cache:
Mybatis first caches the query result set, if not, it checks the database, if there is, it retrieves the returned result set from the cache and does not go to the database. Mybatis internal cache uses HashMap, and the key is HashCode+sqlId +Sql statement.Value is the second level cache of the Java object Mybatis generated from the query map, that is, the query cache. Its scope is the namespace of a mapper. If you record it in the same space, you can get data from the cache by querying sql. Level two Cache can be across SqlSession's

What are the shortcomings of JDBC programming and how MyBatis solves these problems:
1. Frequent creation and release of database links cause waste of system resources and affect system performance. This problem can be solved by using the database link pool.
Solution: Configure the data link pool in SqlMapConfig.xml and use the connection pool to manage database links.
2. Sql statements are written in the code, which makes the code difficult to maintain. The actual application of sql may change greatly, and the change of sql needs to change the java code.
Solution: Configure the Sql statement in the XXXXmapper.xml file to separate it from the java code.
3. It is troublesome to pass parameters to the sql statement, because the where conditions of the sql statement are not necessarily, there may be more or less, and the placeholders need to correspond to the parameters one by one.
Solution: Mybatis automatically maps java objects to sql statements.
4. It is troublesome to parse the result set, the sql change leads to the change of the parsing code, and it needs to be traversed before parsing. It is more convenient if the database records can be encapsulated into pojo objects for parsing.
Solution: Mybatis automatically maps sql execution results to java objects.

The content and order of configuration in SqlMapConfig.xml are as follows:
properties (property)
settings (configuration)
typeAliases (type alias)
typeHandlers (type handler)
objectFactory (object factory)
plugins (plug-in)
environments (environment collection property object)
environment (environment child ) property object)
transactionManager (transaction management)
dataSource (data source)
mappers (mapper)

What are the requirements when calling the mapper interface of MyBatis:
1. The mapper interface method name is the same as the id of each sql defined in mapper.xml
2. The input parameter type of the Mapper interface method and the parameterType of each sql defined in mapper.xml
3. The output parameter type of the Mapper interface method is the same as the resultType of each sql defined in mapper.xml
4. The namespace in the Mapper.xml file is the class path of the mapper interface.

Spring

Spring's ioc, aop (core):

oC:
Inversion of Control Inversion of Control. In our previous development, when using objects of other classes in a class, we used the new method to obtain them directly, or the more advanced method was to obtain the required object instances through reflection. This results in a very high degree of program coupling, and the operation of one class depends heavily on other classes. And there will also be cases of hard coding in the program. The IoC in spring solves this problem very well. When we use other class objects in a class, we only need to define a class member variable of an interface type, and the user injects a specific implementation class object for us when using it. Thereby reducing the coupling degree of the program.
There are only two ideas for implementing IoC: Dependency Injection (DI) and Dependency Lookup.
Dependency injection is used more widely. For example: constructor injection, set method injection, etc.
AOP:
Aspect Oriented Programming Aspect-oriented programming.
The java language we learned before, known as object-oriented programming, has its own advantages. But there are also some disadvantages. For example, in actual development, we all have a main business line, that is, the needs of customers (Party A). What the programmer has to do is to realize the function around the requirements of the main business line (the method of realizing the function is called the business core method). But inevitably, there will be some functions that have nothing to do with the main business line, but must be done, such as permission control, transaction control, log recording, etc. Most of these functions have nothing to do with the main business line, but they are not. It is intertwined with many business core methods, which makes our development cumbersome and redundant code increases. And spring provides an idea to strip out these functions that have nothing to do with the main business line, and when we need to use these public methods, add them to our code in a timely manner, so that programmers can develop , focus more on understanding requirements, implementing core business functions, and making our code concise. This idea is aspect-oriented programming.
Spring implements aspect-oriented programming using dynamic proxy technology, and will choose to use subclass-based or interface-based dynamic proxy according to the actual situation.

A brief introduction to Inversion of Control:
it is one of the core of spring. In other words, it is the basic core of spring, and the rest of the core functions of spring must be supported by IoC.
Inversion of control means that when we acquire objects, we change from active attack to passive reception. That is to say, when writing a class, you only need to provide a class member of an interface type, and you do not need to relate to the specific implementation class, but are provided by the user when using it. This reduces the coupling between classes and classes.
A brief introduction to dependency injection:
First, it is clear that dependency injection is an idea for implementing inversion of control (the other is dependency lookup).
Secondly, in the development process, when we need an instance of a class, the user provides us with an instance of the class. rather than getting it yourself.
Finally, the way to achieve dependency injection can be to use constructor injection or set method injection.
There are many ways to inject in spring, such as constructor-arg element, property element, p namespace and so on.

In the persistence layer, the business layer and the control layer, @Repository, @Service and @Controller are used to annotate the classes in the hierarchy, and @Component is used to annotate those more neutral classes

Spring manages transactions in two ways:
1. Programmatic transactions, hardcoded in the code. (Not recommended)
2. Declarative transactions, configured in the configuration file (recommended)

There are two types of declarative transactions:
a. XML-based declarative transactions
b. Annotation-based declarative transactions

Summarize

SM advantages and disadvantages, usage scenarios:
1. Mybatis is different from hibernate, it is not completely an ORM framework, because MyBatis requires programmers to write Sql statements by themselves, but mybatis can flexibly configure the sql statements to be run through XML or annotations, and The java object and the sql statement are mapped to generate the final executed sql, and finally the result of the sql execution is remapped to generate a java object.

2. Mybatis has a low learning threshold and is easy to learn. Programmers can directly write the original ecological SQL, which can strictly control the SQL execution performance and has high flexibility. It is very suitable for software development that does not require high relational data models, such as Internet software and enterprise operation software. Etc., because the requirements of this type of software change frequently, and once the requirements change, the results are required to be output quickly. However, the premise of flexibility is that mybatis cannot achieve database independence. If you need to implement software that supports multiple databases, you need to customize multiple sets of sql mapping files, which is a lot of work.

3. Hibernate has strong object/relational mapping ability and good database independence. For software with high requirements on relational models (such as customized software with fixed requirements), if you use hibernate to develop it, you can save a lot of code and improve efficiency. However, the learning threshold of Hibernate is high, and the threshold for mastery is higher, and how to design O/R mapping, how to balance performance and object model, and how to use Hibernate well requires strong experience and ability.

  1. In short, according to the needs of users, as long as a software architecture with good maintainability and scalability can be made in a limited resource environment, it is a good architecture, so the framework is the best only if it is suitable.

Note: Any framework that is embedded in the project cannot bring about an increase in efficiency, but a decrease in efficiency. Because of the problem of the core mechanism of java, creating one more object in the memory will cause performance degradation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325759869&siteId=291194637