java - 关于mybatis和hibernate的困惑
巴扎黑
巴扎黑 2017-04-18 09:32:16
0
19
1628
  • 由于java视频里以前好多都是SSH。但是在实际工作中没见过用hibernate的,基本都用mybatis。

  • 我有一些疑问:

    • hibernate碰到什么样的业务会变成致命的弱点,都说hibernate效率低,能否举个例子(例如同一个查询用mybatis查询耗时1秒,用hibernate查询耗时10秒)

    • 可能是有部分人遇到了hibernate的瓶颈,但是这部分人的业务量我觉得,不是大部分使用hibernate的人都能遇到的。一般的小项目,不超过100个用户,这样的项目综合考虑的话更适合用mybatis还是hibernate?主要从开发效率上来说的话。为什么?

谢谢

巴扎黑
巴扎黑

reply all(19)
巴扎黑

There are similar questions on Zhihu.
The answers I saw with high votes basically said, hibernatemybatisIt is more difficult to master and the control of SQL is weaker.

黄舟

Writing mb sql into xml also requires parsing, and the efficiency is not that high

PHPzhong

Compare the relationship between the two and give considerations from three aspects: development efficiency, maintainability, and performance. In terms of its importance, in the project engineering development process, development efficiency > maintainability > performance. Perhaps for some businesses, performance is more critical, but looking at the overall situation, I personally feel that development efficiency > maintainability > performance.

First of all, development efficiency. Personally, I feel that if you are doing some simple CMS-like projects with only simple additions, deletions, modifications and queries, then using Hibernate is more advantageous. You don’t need to master too much Hibernate knowledge, you only need to know some basic queries and some configurations (Hibernate is really To learn, I personally feel that the amount of knowledge is much greater than that of mybatis). hiberbate uses java objects to write sql, while mybatis writes native sql directly. Hibernate provides a good mapping mechanism, and mybatis also needs to write resultmap paramMap itself. Hibernate and MyBatis both have corresponding code generation tools. Simple and basic DAO layer methods can be generated. For advanced queries, Mybatis requires manual writing of 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. To sum up, from the perspective of development efficiency, Hibernate> mybatis.

Maintainability: hiberbate uses java objects to write sql, while mybatis writes native sql directly. Writing SQL directly is easier to maintain, but the former requires viewing SQL through java objects. You have to print the SQL using System.out.println(sql); before you can see it. Hibernate's query will query all fields in the table, 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 needed. Tuning Hibernate HQL statements requires printing out the SQL, and Hibernate's SQL is disliked by many people because it is too ugly. The SQL of MyBatis is written manually by myself, 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.

Performance: Both provide caching and session mechanisms. Small projects are almost the same. Large projects mybatis>hibernate.

黄舟

Looking at everyone’s discussion, I feel that mybaits is better, but I always thought hibernate was more suitable for large-scale development, so I just went with hibernate. At that time, the project was quite urgent and I used it like mybatis. Later, after playing for a long time, I discovered a lot of interesting things. The experience provided by hibernate simplified a lot of workload and compatibility, but these definitely require experience to understand, and later It is useful to use hibernate search and full-text search, so there is no way to change it. Anyway, you can write native sql or hql. There are also xml configuration tools, and the auxiliary annotations are also flexible. It can even generate one2many and many2many data structures that are different from native sql. , in short, he is a very comprehensive guy. If you haven’t regretted it yet, just keep learning. Generally, hibernate tools are used to automatically create the Java and XML mappings of all tables to perform general searches, deletions, and modifications. Then, the Java code is copied and renamed, and associations are manually created using annotations to delete unnecessary fields.

Ty80

1. Question 1: Under what circumstances will Hibernate be 10 times slower than MyBatis?

When Hibernate is used incorrectly, it will appear:P
For example, the loading of embedded object B in Hibernate object A is changed from the default lazy loading to preloading (assuming that you did not understand the impact and changed the configuration ), if you want to query the list of object A, N+1 queries will appear, but most of your business logic does not need to read the embedded object B, and the performance will be very poor.
In general, MyBatis, this The query needs to be written by hand. The probability of this happening is small.

JPA (Hibernate) covers a really wide range of content and has a lot of optimization techniques. It takes a lot of money to get familiar with it. (The Hibernate In Action book I have is very thick and daunting, and it took me a long time to finish it. ), I don’t know how many programmers love reading and have the patience.

However, MyBatis is much easier to get started. Although MyBatis's performance will be many times worse than Hibernate if used incorrectly. But MyBatis is not that complicated, so the chance of making mistakes is much smaller.

2. Question 2: Should I use MyBatis or Hibernate for small projects?

If you are very interested in Hibernate and want to be familiar with it, I suggest you give it a try. After all, most projects need to produce results quickly.
Hibernate is much simpler than MyBatis in this regard (of course MyBatis has a way to automatically generate code, but it is still a lot more troublesome).
If the subsequent project develops well and requires multiple people to collaborate on development, be sure to keep novices away from the persistence layer code logic.

黄舟

Simply put, Hibernate is object-oriented and MyBatis is SQL-oriented. Which one you choose depends on which one your staff is familiar with, whether your business will have many complex queries, and whether it has high performance requirements. In addition, many times people are not familiar with Hibernate, so they feel that Hibernate cannot do this, or it is very complicated to do it. Hibernate said that I will not bear this responsibility. . .

迷茫

Personally, I also think hibernate is easy to use. It automatically creates tables. The performance problem is related queries.

PHPzhong

Now we use logical foreign keys during development without table association, so the development efficiency is almost the same. Mybatis is simple and clear, SQL is more versatile, and collaboration is slightly more convenient.

巴扎黑

Technology is not good or bad, only whether it is suitable or not and can solve specific problems under specific circumstances. Different people using hibernate will produce different results. If you are proficient in hibernate, performance is not a problem. I saw so many comparisons between the two mentioned above, but they really don’t make much sense.

左手右手慢动作

In general, hibernate is not flexible enough and the encapsulation is too deep. Mybatis can configure the desired operations as you wish

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template