Comparison of spring-data-jpa and mybatis

Comparison of Spring Data JPA and MyBatis

Spring Data JPA is a submodule of Spring Data. Using Spring Data, JPA implementation based on the concept of "repositories" is simpler and easier. The goal of Spring Data JPA is to greatly simplify the coding of data access layer code. As a user, we only need to write our own repository interface, which contains some personalized query methods, and Spring Data JPA will automatically implement the query methods.
JPA uses hibernate as the ORM implementation by default, so hibernate is generally used when using Spring Data JPA. Let's take a look at the official concept of hibernate. Hibernate is an open-source object-relational mapping framework. It encapsulates JDBC with very lightweight objects. It establishes a mapping relationship between POJOs and database tables. It is a fully automatic orm. Framework, hibernate can automatically generate SQL statements and execute them automatically, so that Java programmers can use object programming thinking to manipulate the database at will.

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (Plain Old Java Objects, ordinary Java objects) to records in the database.

Looking at it this way, the comparison between Spring Data JPA and MyBatis starts with the comparison between hibernate and MyBatis. So, let's directly compare the latter two.

From the perspective of basic concepts and framework goals, the two frameworks are still very different. Hibernate is a more automated and more advanced framework. After all, at the java code level, most of the sql writing is omitted, and instead, the data of the relational database is manipulated in an object-oriented way. MyBatis is a persistence layer framework that can flexibly write SQL statements and map SQL input parameters and query results into POJOs. Therefore, on the surface, hibernate is more convenient and more automated, while MyBatis is more flexible and free in writing SQL statements.

But this is only the difference between the two from the perspective of use, and does not involve the essence. But if you look at the problem, it is worth looking at the superficial and superficial problems, you will not be able to understand the essence of technology, and you will not be able to maximize the effectiveness of technology. Therefore, if you look at a higher level of abstraction, hibernate is object-oriented for data manipulation, while MyBatis is relation-oriented. Of course, you can also write relational code and systems with hibernate, but you can't get all the benefits of relational orientation. The biggest one is the flexibility of writing SQL, and it also loses object-oriented meaning and benefits - in a word, nondescript . So, what is the difference between object-oriented and relational models, and where is it reflected? In fact, the fields and problems to be solved by the two are fundamentally different: object-oriented is devoted to solving computer logic problems, while relational model is devoted to solving the problem of efficient access to data. Let's compare the difference between object-oriented conceptual principles and relational databases:

  1. Object-oriented considers the entire life cycle of an object, including object creation, persistence, state change and behavior, etc. Object persistence is only a state of an object, while the concept of relational database is more concerned with data efficiency store and read;
  2. Object-oriented emphasizes the encapsulation of object state. The object encapsulates its own state (or data) and does not allow external objects to be modified at will, and only exposes some legal behavior methods for external objects to call; while relational databases are open and can be used by users. Read and modify the relationship at will, and can arbitrarily associate with other tables (as long as the sql correctly allows it);
  3. Object-oriented attempts to model the dynamic world, what he wants to describe is the process and laws of the world, and then adapt to development and changes. Object-oriented always deals with various changes in changes. The relational model models the static world. It records the state of the world at a certain time through data snapshots. It is static.

From the comparison of the above two basic concepts and ideas, it can be concluded that the focus of the two frameworks hibernate and MyBatis are completely different. Therefore, when we choose the two frameworks, we need to choose different frameworks according to different project requirements. In the use of the framework, we must also consider the advantages and disadvantages of the framework, make full use of the strengths and avoid weaknesses, and maximize the effectiveness of the framework in order to truly improve the efficiency of project research and development and complete the goals of the project. But on the contrary, if you use ORM frameworks such as Spring Data JPA and hibernate instead of analyzing and designing the system with object-oriented thinking and methods, but complaining that the framework cannot flexibly manipulate SQL query data, then you want the dog to help you get mice .

So, back to the topic, when using two frameworks, pay attention to the best steps and processes. Let's discuss separately, the general use steps of hibernate are as follows:

  1. Analyze, abstract and summarize the business concepts in the system, and sort out the relationship between the various business concepts - create a conceptual model
  2. Based on the conceptual model, further refine the object classes and class dependencies in the design system - create a design model
  3. Map the designed classes to the tables and fields of the database and configure them
  4. Hibernate can automatically generate database tables according to the configuration information. At this time, you can also concentrate on sorting out table relationships to see if the table structure is reasonable, and appropriately adjust the mapping relationship between classes and tables to regenerate the table structure.

After completing the above steps, the design of the main business concept classes and table structure in the system is basically completed, but the starting point for completing the table structure design is how to persist the objects of the system, while taking into account the association of database tables, fields, field types, and tables Rationality and compliance of relationships, not mere table design. There is still a big difference in the thinking and focus of the two. In addition, it should be pointed out that this is only the most common step of using hibernate, and the actual operation process still needs to be arranged according to the specific project situation.

MyBatis emphasizes less on object-oriented concepts, and is more suitable for flexibly adding, deleting, modifying, and checking data. Therefore, in the process of system analysis and design, to maximize the effectiveness of MyBatis, the general use steps are the same as hibernate makes a difference:

  1. Synthesize the entire system to analyze the data items that the system needs to store, draw the ER relationship diagram, and design the table structure
  2. Create databases and tables according to the table structure designed in the previous step
  3. Write MyBatis's SQL mapping file, Pojos, and interface methods corresponding to database operations

In this way, MyBatis is more suitable for relation-oriented (or data-oriented, or process-oriented) system design methods. Such systems are generally referred to as "transaction script" systems (Transaction Script) from the enterprise written by Martin Fowler in 2004. Application Architecture Patterns (Patterns of Enterprise Application Architecture). And hibernate (also can say Spring Data JPA) is more suitable for the system of building domain model classes. Of course, we can't say that MyBatis can't build a domain model-driven system, and hibernate can't build a transaction footstep system. It's just that building a domain model with MyBatis requires more, dirty, and tiring work; while building a transaction scripting system with hibernate is overkill, and the data query is not so flexible.

Combining all the descriptions and comparisons above, we should have an understanding of the essential differences between the two frameworks. We understand these differences, which can help us choose a more suitable framework. At the same time, we can also use different frameworks to make them do more suitable things. This is also called making the best use of things. service".

Guess you like

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