SSM

SSM framework is the integration of Spring MVC, Spring and Mybatis framework. It is a standard MVC pattern, which divides the whole system into presentation layer, Controller layer, service layer, The fourth layer of DAO layer uses Spring MVC to implement business object management and MyBatis as the persistence engine of data objects

Compared with SSH (Struts+Spring+Hibernate), SSM is more lightweight and flexible, and is the mainstream Java Web development framework at present.

Spring MVC

  • Spring MVC separates the roles of controller, model object, dispatcher, and handler object, which makes them easier to customize.
  • It has controllers that act like Struts, receiving external requests and parsing parameters to the service layer

Spring

  • Spring is a lightweight Inversion of Control (IoC) and AOP oriented container framework
  • Containers are coordination contexts that manage dependencies between objects and provide transaction mechanisms
  • Inversion of control and dependency injection create objects that are managed by the container to achieve decoupling
  • The Spring framework is a layered architecture consisting of seven well-defined modules. Spring modules are built on top of the core container, which defines how beans are created, configured, and managed, and each module (or component) that makes up the Spring framework can exist on its own or in conjunction with one or more other modules.

mybatis

  • MyBatis is a Java-based persistence layer framework. MyBatis eliminates almost all manual setting of JDBC code and parameters as well as retrieval of result sets. MyBatis uses simple XML or annotations for configuration and raw mapping to map interfaces and Java’s POJOs (Plain Old Java Objects) to records in the database.
  • It is part of the ORM persistence layer framework that unites business entities with data tables
  • Mainly used to operate database (add, delete, change and check database)

IOC: Inversion of control

Is a design idea to reduce the coupling relationship between objects; Also called dependency injection. Using the factory pattern to hand objects over to the container for management, you simply configure the corresponding beans and set the related properties in the Spring configuration file, and let the Spring container generate instance objects and managed objects of the class. When the Spring container starts, spring initializes all the beans you configured in the configuration file, and when you need to call them, it assigns those beans to the class that you need to call them (assuming the class name is A). The allocation method is to call the setter method of A to inject, without requiring you to new the bean in A. Examples to deepen understanding. Example: to rent a house, before renting a house needs to find a house, time-consuming and laborious, and now join a housing agency, tell the agency the type of house you need, you can directly choose the house you need, the agency is equivalent to the Spring container.

AOP: Aspect oriented programming

It is a supplement of object-oriented development, which allows developers to dynamically modify the model to meet new requirements without changing the original model. For example, dynamically add logs, security, and exception handling. AOP reduces the degree of coupling between various parts of business logic, improves program reusability and improves development efficiency.

AOP is just a feature of Spring. Like OOP(object-oriented programming),AOP is a programming idea, not a technology. AOP can be said to complement and perfect OOP. OOP introduces concepts such as encapsulation, inheritance, and polymorphism to build an object hierarchy that simulates a collection of common behaviors. OOP is helpless when we need to introduce common behavior to discrete objects. That is, OOP allows you to define top-to-bottom relationships, but not left-to-right relationships. For example, the logging function. Logging code tends to be spread horizontally across all object hierarchies, regardless of the core functionality of the object to which it is spread. In OOP design, it leads to a lot of code duplication, which is not conducive to reuse of modules. Encapsulate the cross-cutting business logic (such as security, logging, transaction, etc.) in an application into a facet that is then injected into the target object (concrete business logic).

AOP technology, mainly divided into two categories: one is the use of dynamic proxy technology, using the interception of the message to decorate the message, to replace the original object behavior implementation; The other is static weaving, which introduces special syntax to create “aspects” so that the compiler can weave code about “aspects” at compile time.

The principle of

For SpringMVC:

1. The client sends the request to DispacherServlet (dispenser) 2. Controller DispacherServlet query HanderMapping and find the Controller that handles the request. 3.Controller DispacherSerclet (” ModelAndView “) Locate view 5 specified by ModelAndView. The view is responsible for displaying the results to the client

Spring:

The IOC container, which can load beans (that is, our classes in Java, and of course the Service DAO), is probably the most commonly used in development. With this mechanism, we do not need to initialize the class every time we use it. We rarely see the keyword new. In addition, Spring AOP, transaction management and so on are often used;

Mybatis:

Mybatis is the encapsulation of JDBC, which makes the underlying operation of the database transparent. Mybatis operates around an instance of sqlSessionFactory. Mybatis is associated with the Mapper file of each entity class through the configuration file, and the Mapper file configures the SQL statement mapping required by each class to the database. Every time you interact with the database, get a sqlSession through sqlSessionFactory, and then execute SQL commands.

Transaction management

Transactions have four features: ACID Atomicity, Consistency, Isolation, and Persistence.

MyBatis’s Transaction design focuses on the Transaction interface, which consists of four methods: get connection, commit, rollback, and close connection.

There are two forms of transaction management: 1. Use JDBC transaction management mechanism: that is, use java.sqL. Connection to complete the operation of things; 2. Using MANAGED transaction management mechanism, MyBatis will not implement transaction management itself, but let containers such as WebLogic, JBOSS to implement transaction management.

Usage:

To complete a function: 1. First write the entity class entity, define the attributes of the object, (can refer to the database table field to set, database design should be before all the coding started). 2. Write mapper. XML (Mybatis), which defines your functions, corresponding to the database operations, such as insert, selectAll, selectByKey, delete, update, etc. 3. Write mapper. Java and map the operations in mapper. XML to Java functions based on their IDS. 4. Write service. Java, provide services for the control layer, accept parameters of the control layer, complete corresponding functions, and return to the control layer. 5. Write controller. Java, connect page request and service layer, obtain parameters of page request, map different URL to corresponding processing function through automatic assembly, obtain parameters, process parameters, and then pass to service layer. 6. Write JSP page call, request which parameters, need to obtain what data

SSH

SSH framework is the integration of Struts2, Spring and Hibernate frameworks, wherein Struts2 acts as controller, Spring manages components of each layer, and Hibernate is responsible for persistence layer.

Struts2 framework steps (Struts2 uses Filter embedding) :

1. The client initializes a request to the Servlet container (such as Tomcat)

2. The request goes through a series of filters (including an optional Filter called ActionContextCleanUp, which is useful for Struts2 integration with other frameworks)

FilterDispatcher is then called, and the FilterDispatcher asks ActionMapper to determine if the request requires an Action

4. If ActionMapper decides it needs to invoke an Action, FilterDispatcher hands the request processing to ActionProxy

5. ActionProxy queries the Configuration file of the framework through the Configuration Manager to find the Action class to be invoked

ActionProxy Create an ActionInvocation instance.

7. The ActionInvocation uses named mode, which involves Intercepter invocation before and after the Action invocation.

8. Once the Action is executed, the ActionInvocation is responsible for finding the return result based on the Struts. XML configuration. The result is usually (but not always, can be another Action chain) a JSP or FreeMarker template that needs to be represented.

9. Return the processing result to the client

Hibernate:

Advantage:

  1. The code of JDBC access database is encapsulated, which greatly simplifies the tedious 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 the DAO layer
  3. Hibernate uses Java reflection rather than bytecode enhancers 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.

Principle:

1. Through the Configuration (). The configure (); Read and parse the hibernate.cfg. XML configuration file 2. Read and parse the mapping information from hibernate.cfg. XML resource=”com/xx/ user.hbm. XML “/> 3. Through the config. BuildSessionFactory (); / / create a SessionFactory 4. SessionFactory. OpenSession (); / / open Sesssion 5. Session. BeginTransaction (); Persistent operate 7.session.getTransaction().commit(); Close Session 9. Close SesstionFactory

contrast

1. Struts is too heavy and cumbersome, SpringMVC is light

2.Hibernate is too heavy, want to do too many things, I personally feel, like Hibernate transactions, completely useless, and cache, most of the time, need to use the scene, can be developed by themselves, lighter, and data relationships become complex, Hibernate more difficult to control, Mybatis is more flexible when it comes to distributed transactions. If necessary, write SQL directly. Mix it up by business type.

3. Springboot, essentially or Spring, pit more, upgrade version must be careful, if the transaction processing is more, do not recommend using Springboot hibernate/ Mybatis packaged, direct use of native will step less pit, familiar with the development of operation and maintenance are very smooth, glue role.

4.Hibernate is a kind of O/R relational type, that is, to complete the mapping between the database table and persistent class, and MyBitas is for SQL-maping, personal understanding is a kind of Hibernate to encapsulate the database, can call the corresponding database operation statement HQL, MyBitas, on the other hand, uses raw database operation statements to reduce query fields. Hibernate optimization is more difficult than MyBitas.

5. For advanced queries, Mybatis needs to manually compile SQL statements and ResultMap. Hibernate has a good mapping mechanism, so developers don’t need to worry about SQL generation and result mapping, and can focus more on business processes.

Hibernate database portability is good, MyBatis database portability is not good, different databases need to write different SQL.

7. The SSM is lighter and more flexible than SSH. SSH, however, is more encapsulated and easier to develop

8.SSM differs from SSH mainly in MVC implementation and ORM persistence (Hiibernate vs. Mybatis). SSM is more and more lightweight configuration, annotation development to the extreme, and ORM implementation is more flexible, SQL optimization is easier; SSH, on the other hand, pays more attention to configuration development. Hiibernate is more object-oriented in the complete encapsulation of JDBC and more automatic in the maintenance of data adding, deleting, changing, and checking. However, IT is weak in SQL optimization and has a higher threshold for entry.

JPA Hibernate/Mybatis are all SQL oriented, for noSQL, such as Mongo/ElasticSearch /solr, general CRUD, JPA is very efficient, a set of methods to play the day, of course, too fine control JPA will not work. For example, elasticSearch/Solr wants to be highlighted. In fact, most noSQL services do not require high performance and transaction requirements. If not necessary, nodeJS, Python, etc are more efficient.

10.net TY, can take over SSH network services (under MVC, network socket part), early also need to start a light tomcat/jboss/ Glassfish and so on, now most directly start netty. Jersey is a more efficient restful alternative to SpringMVC