What is Mybatis?

In fact, MyBatis is an open source project of Apache iBatis. In 2010, this project was migrated to Google Code by Apache Software Foundation and renamed as MyBatis. The term iBATIS comes from the combination of “Internet” and “Abatis”. IBATIS is a Java-based persistence layer framework. The persistence layer framework provided by iBATIS includes SQL Maps and Data AccessObjects (DAO).

Mybatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all of the JDBC code and manual parameter setting and result set extraction. MyBatis uses simple XML or annotations to configure and map the base body, mapping interfaces and Java’s POJOs(Plain Old) to records in the database.


MyBatis is an updated version of iBatis. There are many similarities in usage, but MyBatis has significant improvements. Such as:
1. Mybatis implements interface binding, which is more convenient to use.
In ibatis2.x, we need to specify which XML mapping file corresponds to in the IMPLEMENTATION class of DAO, while Mybatis realizes the binding of DAO interface and XML mapping file, which automatically generates the specific implementation of the interface for us, making it more convenient and convenient to use.
2, object relational mapping improvement, higher efficiency
3. MyBatis uses powerful OGNL based expressions to eliminate other elements.
The difference between IBatis and MyBatis is no longer said, interested friends can study.
MyBatis framework architecture
Seeing the frame diagram of Mybatis, you can clearly see the overall core object of Mybatis. I prefer to use my own diagram to express the whole execution process of Mybatis.

Principle details:

SqlSessionFactory = SqlSessionFactory = SqlSessionFactory = SqlSessionFactory = SqlSessionFactory = SqlSessionFactory SqlSession contains all the methods required to execute SQL, you can directly run the MAPPED SQL statement through SqlSession instance, complete the data add, delete, change, check and transaction submission, etc., and then close THE SqlSession.

Advantages and disadvantages of MyBatis

Advantages:
1. Simple and easy to learn
Mybatis itself is small and simple. Without any third party dependence, the simplest installation as long as two JAR files + configuration several SQL mapping files easy to learn, easy to use, through the documentation and source code, can be more fully grasp its design ideas and implementation.
2, flexible,
Mybatis does not impose any impact on the existing design of the application or database. SQL is written in XML for unified management and optimization. You can do almost everything with SQL that you can do without a data access framework, and maybe more.
3. Uncouple SQL from program code
By providing a DAL layer, the business logic and data access logic are separated, making the system design clearer, easier to maintain, and easier to unit test. Separation of SQL and code improves maintainability.
4. Provide mapping labels to support orM field relational mapping between objects and databases
5, provide object relationship mapping label, support object relationship maintenance
6, provide XML tags, support the preparation of dynamic SQL.

Disadvantages:

1, write SQL statements when the workload is very large, especially fields, associated table, more so.
2, SQL statements rely on the database, resulting in poor database portability, can not replace the database.
3, the framework is still relatively simple, the function is still missing, although the data binding code is simplified, but the whole underlying database query is actually to write their own, the workload is also relatively large, and it is not easy to adapt to rapid database modification.
4. Poor second-level caching mechanism.

conclusion

The advantages of MyBatis are also the disadvantages of MyBatis. Because myBatis is simple to use, the bottleneck of data reliability and integrity depends more on the level of SQL used by programmers. SQL written in XML makes it easy to modify, optimize, and browse uniformly, but the readability is poor, and debugging is difficult and limited. Mybatis is not as powerful as Hibernate, but the biggest advantage of MyBatis is simple and small, easy to use, easy to browse and modify SQL statements.


Reprinted source: http://blog.sina.com.cn/s/blog_175f7bfc70102xj8g.html