search

Home  >  Q&A  >  body text

java - 关于mybatis和hibernate的困惑

谢谢

巴扎黑巴扎黑2802 days ago1617

reply all(19)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:34:16

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:34:16

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

    reply
    0
  • PHPz

    PHPz2017-04-18 09:34:16

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:34:16

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:34:16

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:34:16

    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. . .

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:34:16

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

    reply
    0
  • PHPz

    PHPz2017-04-18 09:34:16

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:34:16

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:34:16

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

    reply
    0
  • Cancelreply