What is the difference between Mybatis and Spring Data JPA in SpringBoot?

I think why so many people say that Mybatis is good, the main reason is that Mybatis is easy to use. Now that you learn programming and database, you have basically learned SQL. The SQL-based model of Mybatis for DB is relatively intuitive and friendly.

JPA is based on ORM, which separates the code from the DB, which is equivalent to adding a new level between the code and the DB. A new set of standards is used to indirectly operate the DB. Compared with the SQL model, it is not direct enough. It is easy to control and increases the cost of learning. When encountering problems like this, it is easy for people to give up.

Especially during team development, it is difficult for you to control everyone's understanding of JPA's deep mechanisms. In order to avoid the barrel effect, it is better to use Mybatis altogether. In fact, from a functional point of view, the two must learn from each other. Spring Data JPA can also execute native SQL and stored procedures. Mybatis also has some ORM features. Choosing Spring Data JPA or Mybatis will not affect the final business. The realization of logic. On the premise of not affecting the business realization, the selection is mainly to choose the efficiency during development and maintenance.

In this regard, I think Mybatis is not as convenient and fast as Spring Data JPA or Hibernate's forward engineering when the table structure is frequently modified during the development stage. After considering the version compatibility with Spring Family Bucket, our team finally chose Spring Data JPA.

Some of our team’s early projects used MyBatis, and all subsequent projects have switched to Spring Data JPA. Because in the early use of Spring Data JPA, it solved its biggest pain point, that is, the problem that people often say cannot cope with complex business scenarios and is inconvenient to write complex dynamic SQL.

I wrote a Spring Data JPA extension library Fenix , which is fully compatible with Spring Data JPA and can be considered as JPA Plus in the JPA world. The early purpose of the extension library was to solve complex dynamic SQL. Later, more functions have been extended, such as providing more efficient batch addition, deletion and modification methods, returning custom JavaBeans, and non-null attribute increments. Update and so on.

Therefore, you can also write complex JPQL (or native SQL) statements in XML like MyBatis. The essence of XML is to decouple SQL and Java code, making it easy to maintain and manage SQL separately. In order to increase the dynamics of SQL statements, you can also write procedural logic control syntax such as #{} expressions, if/else, and foreach. For common dynamic SQL fragments, a large number of common XML SQL semantic tags are also built-in. Compared with some dynamic tags of pure if/else and foreach in MyBatis, the code is more readable and can achieve more extreme reusability. Sex. In addition to supporting dynamic JPQL (or SQL) writing in the form of XML, there are three other ways to write query statements, namely: JPQL (or SQL)-based Java chain API method, Specification-based Java API method and JavaBean ( VO) The way of annotation. Because the latter two are based on the Specification method, you don't even need to write any methods in the Repository interface.


The core reasons for choosing Spring Data JPA are as follows:

  • Fundamentally speaking, it is a change in the development model. A new project does not need to design a complete database and table structure in advance. The database table structure is automatically generated during the development process, which can be quickly developed and iterated, and gradually improved.
  • In enterprise-level projects, you can enjoy the benefits of cross-database types, and can shield some SQL differences in the underlying database. Customers in different regions may require the use of a specific database, so there is almost no cost for modification.
  • The persistence layer has less code and is easier to maintain. When adding or modifying fields in the future, most of the time, you only need to maintain the annotation mapping relationship of the Java Entity.

Because it solves the problem of Spring Data JPA's inconvenience to write complex dynamic SQL, its remaining biggest shortcoming is the problem of high complexity , because it is based on Hibernate, the encapsulation is high, and the learning cost is naturally higher. Moreover, related materials and blogs are much less than MyBatis. MyBatis has an official Chinese document from a long time ago. The official document of Spring Data JPA is too simple to deal with some simple business scenarios and complex scenarios. , The introduction of advanced functions is too little. This also dissuaded a lot of people. Therefore, I often hear some beginners ask questions about whether you can write multi-table queries in JPA.

In addition, it is not recommended to use the one-to-many and many-to-many functions between Entity entities in JPA, which means that entities should be as independent as possible. When related queries are required, query or update through JPQL or SQL can be used to avoid Some implicit operations between cascading relationships make the code meaning more transparent, which is convenient for subsequent expansion or targeted optimization and transformation.

Guess you like

Origin blog.csdn.net/weixin_55932383/article/details/115279536