What is MyBatis
MyBatis is an open source project for Apache Ibatis, which was migrated to Google code by the Apache Software Foundation in 2010 and renamed MyBatis. The term ibatis is derived from the combination of "Internet" and "Abatis" and is a Java-based persistence layer framework. Ibatis provides a durable layer framework that includes SQL maps and Data Access Objects (DAO).
MyBatis Reference website: https://mybatis.github.io/mybatis-3/zh/index.html
The official website is more authoritative for the introduction of MyBatis:
[Java]View PlainCopyprint?
- MyBatis is an excellent persistence layer framework that supports the customization of SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually set parameters and extracts the result set. MyBatis uses simple XML or annotations to configure and map primitives, mapping interfaces and Java POJOs (Plain old Java Objects, ordinary Java objects) to records in a database.
MyBatis is an upgraded version of Ibatis, there are many similarities in usage, but MyBatis has made significant improvements. For example:
1, MyBatis realizes the interface binding, the use is more convenient.
In ibatis2.x we need to specify which XML mapping file is specific to the DAO implementation class, and MyBatis implements the binding of the DAO interface to the XML mapping file, which automatically generates the concrete implementation of the interface for us, which makes it more convenient and easy to use.
2. Improved object-relational mapping, higher efficiency
3. MyBatis uses powerful OGNL-based expressions to eliminate other elements.
For the difference between Ibatis and MyBatis no longer say, interested friends can study.
MyBatis Frame Architecture
See MyBatis frame diagram, you can clearly see the overall core object of MyBatis, I prefer to use their own diagram to express the mybatis of the entire implementation process. As shown in the following:
Explanation of principle:
The MyBatis application is created from an XML configuration file based on the configuration, the configuration is from two places, one is the configuration file, one is the Java code annotation, gets a sqlsession. Sqlsession contains all the methods required to execute SQL, you can run the mapped SQL statements directly through the Sqlsession instance, complete the additions and deletions of the data and transaction commits, etc., and then close the sqlsession.
Advantages and disadvantages of MyBatis
Advantages:
1. Easy and easy to learn
The mybatis itself is small and simple. No third-party dependencies, the simplest installation as long as two jar files + configuration of several SQL mapping files easy to learn, easy to use, through the documentation and source code, you can compare the complete grasp of its design ideas and implementation.
2. Flexible
MyBatis does not impose any impact on the existing design of an application or database. SQL is written in XML to facilitate unified management and optimization. With SQL you can basically implement all the features that we can do without using the data access framework, perhaps more.
3. Decoupling SQL from program code
By providing a DAL layer that separates business logic from data access logic, the system is designed to be clearer, easier to maintain, and easier to unit test. The separation of SQL and code improves maintainability.
4, provide mapping label, support ORM field relationship mapping between object and database
5, provide the object relation mapping tag, support the object relation establishment maintenance
6, provide XML tags, support to write dynamic SQL.
Disadvantages:
1, the work of writing SQL statement is very large, especially the field many, the association table A long time, more so.
2, SQL statements rely on the database, resulting in poor database portability, can not replace the database.
3, the framework is relatively humble, the function is still missing, although the data binding code is simplified, but the entire underlying database query is actually to write their own, the workload is relatively large, and it is not easy to adapt to rapid database modification.
4, level Two cache mechanism is poor
Summarize
MyBatis's advantages are also mybatis shortcomings, because the mybatis is simple to use, the reliability of data, the integrity of the bottleneck is more dependent on the use of SQL programmer to the level. SQL written in XML, although easy to modify, optimize and unified browsing, but the readability is very low, debugging is very difficult, also very limited.
MyBatis is not as powerful as hibernate, but the biggest advantage of MyBatis is that it is simple, small, easy to navigate, and easy to browse and modify SQL statements.
MyBatis Introduction and principle