Understanding of hibernate and mybatis in Java Web learning tutorial
preface
In Java Web development, the most classic is the SSH framework combination and SSM framework combination. Now many IT companies are willing to use SSM. For H and m here, hibernate and mybatis, it is easy to say today.
teach fish how to swim
In class, I often tell students that when learning any technology, we must understand:
Knowledge explanation
Hibernate and mybatis are very popular ORM (object relational mapping, in other words, it is a technology for mapping with database) persistence layer framework. Its essence is to encapsulate JDBC to facilitate our use and simplify our development. First, understand several problems:
Why can the introduction of persistence layer solve this problem? Analysis: in the persistence layer, a mechanism can be introduced as a translation to translate HQL statements and dynamic query statements into different SQL statements for different underlying databases, which solves the problem that there is no need to change SQL statements after changing the database, and the amount of code will be greatly reduced.
Well said, how do hibernate and mybatis implement the above scheme, that is, how do they implement their ORM?
Hibernate
In Hibernate, this mechanism is a configuration file cfg. XML (in SRC directory)
Once the project changes the database, only a few attributes in the file need to be modified, and the logic code of the business layer does not need to be modified. It is recommended to use hibernate for development:
**Domain object ---- > mapping ---- > database, that is, objects and tables should have a mapping, which can be configured in hibernate in two ways**
1. XML mode (class name. HBM. XML)
Establish the mapping relationship between tables and classes through HBM files
2. Annotation mode
Later, with reverse engineering, it was much simpler to directly generate the corresponding file through the database table.
MyBatis
In mybatis, this mechanism also relies on a configuration file mybatis config XML (in SRC directory)
In this way, you only need to modify the configuration file every time you change the database.
It is recommended to use mybatis for development:
**Build a database and create various tables -- > reverse engineering automatically generates the code required for mybatis execution (mapper.java, mapper.xml, Po..) -- > write the control layer and service layer according to the business logic**
difference
There are all kinds of comparisons on the Internet. I only talk about some differences
1. Hibernate has an object-oriented query language called HQL, which is very powerful. Apes who can't use SQL statements can also add, delete, modify and query the database, but this is also its weakness. SQL statements can't be optimized.
2. Mybatis needs to configure SQL statements, that is, it still needs the basic knowledge of the database to get started, and beginners must encounter a lot of holes in the result mapping.
3. I haven't done a particularly large project. If there are no special requirements for performance, hibernate is still easier to implement functions.
System tuning
Hibernate tuning scheme
Mybatis tuning scheme
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.