Everybody is good! Why choose MyBatis as the intermediate layer between application and database? MyBatis plays the role of connecting the preceding and the following. It is not difficult to find that in most architecture and code design, the scalability of software can be improved by adding a layer of abstraction to shield details on both sides. But MyBatis isn’t the only architecture that offers this functionality, so why choose it?

background

In most online application scenarios, data is stored in relational databases. Although other persistent storage (such as ElasticSearch, HBase, and MongoDB) is sometimes used as secondary storage, relational databases are still the main force for data storage in today’s big data era.

In the development process, applications are often stateless, and the provision of diversified services usually depends on data, so the development process often needs to deal with data, and relational database products including SQL Server, MySQL, Oracle and so on. In the process of business development, the following processes are often carried out:

  • Using object-oriented thinking to implement business logic;
  • When designing database tables, consider first, second, and third paradigms;
  • In the operation of database records, the use of SQL statements and collective thinking to consider the table of connection, conditional statements, sub-queries, etc.

It can be found that there are a lot of repeatable parts in the above process. In order to improve the reusability, the process was abstracted and the difference of actual operation was shielded by design pattern, so that the whole process could deal with different database products through Object Relational Mapping (ORM) framework. Improve the robust of the software.

Why MyBatis

The iBatis project in the Apache Foundation is the predecessor of MyBatis, which changed its name to MyBatis in 2010 and migrated the source code to GitHub in 2013.

advantages

  • Can help Java developers encapsulate repetitive JDBC code. Map ResultSet results to Java objects using the Mapper mapping configuration file and associated annotations (the built-in Default objectFactory is used when there is no custom objectFactory). Other mapping rules and necessary subqueries can be nested within a specific mapping rule, which makes it easy to implement the logic of complex mappings and to implement one-to-one, one-to-many, many-to-many relational mappings and corresponding bidirectional relational mappings.

  • MyBatis is more flexible, lightweight and controllable than Hibernate and various JPA implementation frameworks. We can directly write native SQL statements in the Mapper mapping file of MyBatis, and apply the functions of the underlying database products, so that we can directly optimize THE SQL statements, so that they can be used in accordance with the rules of the database, so that the native SQL statements choose the index we expect, so as to ensure the performance of the service. This is especially suitable for large data volume, high concurrency and other need to optimize SQL to the extreme scenarios;

  • MyBatis provides powerful dynamic SQL capabilities to help us developers get rid of this repetitive work. We only need to write dynamic SQL statements in the mapping configuration file and MyBatis can piece together a complete, executable SQL statement based on the actual parameter values passed in during execution.

  • Powerful plug-in support, so that programmers do not need to learn MyBatis framework can be quickly used. For example, MyBatis Generator.

conclusion

By freeing programmers from repetitive code, MyBatis focuses more on the realization of business logic, and leaves interfaces for database operation optimization, so that business needs can be met to the maximum extent, coupled with powerful plug-in support, greatly improving our development efficiency. But we still need to learn from frameworks to write better business code and improve our capabilities.

This article is part of the “Gold Nuggets For Free!” Event, click to view details of the event