Home  >  Article  >  php教程  >  The difference between Mybatis and Ibatis

The difference between Mybatis and Ibatis

高洛峰
高洛峰Original
2017-01-05 17:18:232177browse

The difference between Mybatis and Ibatis:
1. Mybatis implements interface binding, which is more convenient to use
In ibatis2.x we need to specify which xml mapping file corresponds to it in the DAO implementation class,
Mybatis implements the binding of the DAO interface and the xml mapping file, and automatically generates the specific implementation of the interface for us, making it more trouble-free and convenient to use.
This can be said to be the most important improvement of Mybatis.

Note:
Although Mybatis supports using annotation configuration directly in the interface to simplify configuration,
It is strongly recommended to still use xml configuration. After all, the annotation configuration method has limited functions and the code is too intrusive. Only by using the xml configuration method can the advantages of Mybatis be reflected.

2. Improvement of object-relational mapping and higher efficiency
I believe that many friends who are using ibatis2.x have not implemented it through the ibatis xml mapping file. Relationship mapping between objects. In fact, there is really no need to do that, because ibatis2.x uses the "nested query" method to realize the relationship between objects through direct assembly of query statements. The effect is the same as encapsulating it in DAO or Service. .
However, this method has the "N+1 query problem".
In summary, the N+1 query problem can be caused by:
? You executed a single SQL statement to get the result list (that is, +1).
? For each record returned, you execute a query to load the details for each (which is N).
This problem will cause hundreds or thousands of SQL statements to be executed. This is usually not expected.

In Mybatis, in addition to being compatible with the "nested query" method in ibatis2. The dto object is automatically encapsulated into the required object.
Please refer to the Mybatis official user manual for specific implementation methods, which will not be described here.

However, in fact, the benefits brought by this improvement are also very limited. Because this method does not work when using paging, or the result set of nested objects does not allow paging. This point has been clearly restricted in the Mybatis framework (line 34 in org.apache.ibatis.executor.resultset.NestedResultSetHandler), and there are many cases where paging is required in actual projects...
Think about it carefully, It is true that one-to-many mapping cannot be paged through the configuration file, because the number of records queried at this time is not equal to the size of the actual returned object, but it is not clear why one-to-one mapping is not allowed. Maybe it's because one-to-one is a special case of one-to-many, and when designing the framework, it was not considered or difficult to handle this special case.

3. MyBatis uses powerful OGNL-based expressions to eliminate other elements
People familiar with struts2 should not be unfamiliar with OGNL expressions.
MyBatis uses OGNL expressions to simplify configuration The complexity of the file is simpler to use.

What you may be more concerned about is that
Mybatis implements interface binding, which is more convenient to use.
iBatis/MyBatis 3 provides a new feature: annotations.


For more articles related to the difference between Mybatis and Ibatis, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn