Comparison between MyBatis-plu and JPA

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

提示:这里可以添加本文要记录的大概内容:

It's embarrassing to say that I have never used JPA in so many years of work.

I used querydsl-jpa myself and compared it with the commonly used mybatis-plus.
Overall, they have their own advantages: mybatis-plus is more lightweight, and JPAQuery can implement joint table queries.


1. Let’s talk about the similarities.

1: Both package simple CRUD and can be used directly
2: Both support custom sql (such as: @Select())
3: Both support second-level cache

2. Differences

1. From the perspective of implementation: CURD is implemented in different ways

mybatis-plus :
Mapper (Dao) class extends BaseMapper
implementation class extends ServiceImpl
interface class extends IService

jpa:
The repository class (equivalent to the Mapper/DAO class) extends JpaRepository.
However, after jpa defines the database class (such as the User class), it needs to compile (the QUser class will be produced)

2. On pagination

mybatis-plus :
It has its own paging plug-in and can also support third-party plug-ins. For example, pageHelper
jpa:needs to handle the count query by itself.

3. Snowflake ID

mybatis-plus:You
jpa:need to implement it yourself

4. Pseudo deletion

mybatis-plus:There is @TableLogic
jpa:that needs to be implemented by yourself

5. Subclasses exclude fields of parent classes

mybatis-plus:Redefining fields in the parent class in the
jpa:subclass cannot be solved with @transient.


Summarize

To summarize the understanding of the big guys:
jpa:
1: Use hibernate as ORM by default
2: More object-oriented
3: Support multi-table association (using JPAQuery, not written in xml like mybatis)

mybatis:
1: Avoids almost all JDBC code and manual setting of parameters and obtaining result sets
2: More data-oriented (relationship-oriented), fatal advantages: simple and highly readable

网上一直说的性能:
The efficiency of the two ORM frameworks will not be particularly high (after all, it is very rare to insert tens of thousands of data at a time and require ms level). In normal development, when there are hundreds of pieces of data, both are similar.

到底用哪个
First: Follow the company, after all, the boss has no say.
Second: I personally recommend mybatis-plus, try not to write xml. The joint table logic is implemented in the code (jvm runs the code very quickly, avoiding for internal database checking)
Third: The learning cost of JPA is greater than that of MyBatis-plus

Final statement: This article is not for commercial use and refers to articles written by many experts on the Internet:

Key recommendation: Detailed comparison of the differences between JPA and MyBatis-Plus:
https://zhuanlan.zhihu.com/p/425864811

Which query efficiency is higher, springjpa or mybatis?
https://www.zhihu.com/question/356307466/answer/919908635

JPA,Hibernate,mybatis,mybatis plus,Querydsl
https://blog.csdn.net/qq_44772660/article/details/111840512

MyBatis or JPA? Finally we have the answer!
https://zhuanlan.zhihu.com/p/263043522

I won’t list them one by one.

Guess you like

Origin blog.csdn.net/qq_37700773/article/details/130250153