MyBatis -> What are the differences between MyBatis and Hibernate?

How is MyBatis different from Hibernate?

The debate between SQL and ORM will never end

Comparison of development speed:

  • HibernateThe real mastery of Mybatis is more difficult.

  • MybatisThe framework is relatively simple and easy to use, but it is also relatively crude.

  • Compared with the development speed of the two, it is not only necessary to consider the characteristics and performance of the two, but also to consider which one is more suitable for project development according to the project requirements

    • For example: there are basically no complex queries used in a project , just simple addition, deletion, modification and query , so the efficiency of choosing hibernate is very fast.
    • Because the basic sql statement has been encapsulated , you don’t need to write sql statement at all, which saves a lot of time
    • But for a large-scale project, there are many complex statements , so it is not a good choice to choose hibernate
    • Choosing mybatis will speed up a lot, and the management of statements is also more convenient.

Comparison of development workload:

  • HibernateAnd MyBatisboth have corresponding code generation tools, which can generate simple and basic DAO layer methods.
  • For advanced queries, Mybatis needs to manually write SQL statements and ResultMap.
  • Hibernate has a good mapping mechanism, developers do not need to care about SQL generation and result mapping, and can focus more on business processes

SQL optimization aspects:

  • Hibernate's query will query all , which will consume performance .

  • Hibernate can also write its own SQL to specify the fields that need to be queried, but this destroys the simplicity of Hibernate development.

  • The SQL of Mybatis is written manually , so the query fields can be specified as required.

  • The tuning of Hibernate HQL statements needs to print out the SQL, and the SQL of Hibernate is disliked by many people because it is too ugly.

  • The SQL of MyBatis is written manually, so it is easy to adjust.

  • But Hibernate has its own log statistics. Mybatis itself does not have log statistics and uses Log4j for logging .

Comparison of object management:

  • Hibernate is a complete object/relational mapping solution

    • It provides the function of object state management (state management), so that developers no longer need to care about the details of the underlying database system .
    • In other words, compared to the common JDBC/SQL persistence layer solution that needs to manage SQL statements
    • Hibernate adopts a more natural object-oriented perspective to persist data in Java applications .
  • In other words, developers using Hibernate should always pay attention to the state of the object (state), regardless of the execution of SQL statements.

  • This part of the details has been taken care of by Hibernate, and only developers need to understand it when performing system performance tuning.

  • However, MyBatis has no documentation in this area, and users need to manage the objects themselves in detail.

Cache mechanism comparison:

  • 相同点: You can implement your own cache or use other third-party caching solutions, and create adapters to completely cover the caching behavior.

  • 不同点

    • Hibernate's second-level cache configuration is configured in detail in the configuration file generated by SessionFactory, and then what kind of cache is configured in the specific table-object mapping.
    • The second-level cache configuration of MyBatis is configured in detail in each specific table-object mapping, so that different caching mechanisms can be customized for different tables. And Mybatis can share the same cache configuration and instance in the namespace through Cache-ref.
  • Comparison between the two: Because Hibernate has a good management mechanism for query objects, users do not need to care about SQL. Therefore, if there is dirty data when using the second-level cache, the system will report an error and prompt.

  • In this regard, MyBatis needs to be very careful when using the second-level cache. If you cannot fully determine the scope of data update operations, avoid blind use of Cache. Otherwise, the appearance of dirty data will bring great hidden dangers to the normal operation of the system .

  • Hibernate has powerful functions, good database independence , and strong O/R mapping capabilities. If you are quite proficient in Hibernate and have properly encapsulated Hibernate, then the entire persistence layer code of your project will be quite simple. There is very little code to write, and the development speed is very fast, which is very cool.

  • HibernateThe disadvantage is that the learning threshold is not low , and the mastery threshold is higher, and how to design O/R mapping, how to balance performance and object model, and how to make good use of Hibernate requires your experience and ability. Only strong strength will do.

  • MyBatis is easy to learn and use immediately after learning. It provides automatic object binding function for database query, and continues the good experience of using SQL. It is perfect for projects that do not have such high object model requirements. .

  • MyBatis的缺点Even though the framework is still relatively simple, the functions are still lacking. Although the data binding code is simplified, the entire underlying database query actually needs to be written by yourself, the workload is relatively large, and it is not easy to adapt to rapid database modification . .

Guess you like

Origin blog.csdn.net/rod0320/article/details/123389319