1.1 introduction of ORM

JDBC is the unified API for Java and database interaction. The operation steps of traditional JDBC programming are as follows:

  • (1) Register database driver, clearly specify database URL address, database user name, password and other connection information
  • (2) DriverManager opens the database connection
  • (3) Create a Statement object from the database
  • (4) Execute SQL statements through the Statement object to obtain a ResultSet object
  • (5) Read data through ResultSet and convert the data into JavaBean objects
  • (6) Close ResultSet and Statement objects and database connections to release related resources

Object-relational Mapping (ORM) transforms Javabeans and database objects into each other.

1.2 Common Persistence Frameworks

Hibernate, MyBatis, JPA, Spring JDBC

2.0 introduction of Mybatis

MyBatis, formerly known as iBATIS, is an open source project initiated by Clinton-Begin in 2001. It initially focused on the development of cryptographic software, and later developed into a Java-based persistence layer framework. Clinton donated iBATIS ‘name and source code to the Apache Software Foundation in 2004, and over the next six years, the world of open source software has changed dramatically, with all development practices, infrastructure, licensing, and even database technology completely transformed. In 2010 the core development team decided to leave the Apache Software Foundation and rename iBATIS to MyBatis. MyBatis is an excellent persistence layer framework that supports custom SQL queries, stored procedures, and advanced mappings, eliminating almost all manual setting of JDBC code and parameters, as well as retrieval of result sets. MyBatis can be configured and mapped using XML or annotations. MyBatis maps parameters to the configured SQL to form the final EXECUTED SQL statement, and finally maps the results of SQL execution to Java objects to return. Unlike other ORM (Object relational Mapping) frameworks, MyBatis does not associate Java objects with database tables. Instead, MyBatis associates Java methods with SQL statements. MyBatis allows users to take full advantage of the capabilities of a database, such as stored procedures, views, complex queries, and proprietary features of a database. MyBatis is a good choice if you want to work with legacy databases, non-standard databases, or have complete control over SQL execution. Compared to JDBC, MyBatis simplifies the code so that SQL statements can be executed in a single line of code. MyBatis provides a mapping engine that declaratively maps the execution results of SQL statements to object trees. SQL statements can be generated dynamically by using a built-in XML-like expression language.

2.1 Overall architecture of Mybatis

MyBatis is divided into three layers: basic support layer, core processing layer and interface layer

2.2 Mybatis SQL execution process

The execution of SQL statements involves various components, of which the most important are Executor, StatementHandler, ParameterHandler, and ResultSetHandler. Executor is responsible for level 1 and level 2 caching and provides transaction management operations. It delegates database operations to StatementHandler. StatementHandler uses ParammeterHandler to bind SQL arguments. The java.sql.Statement object is used to execute the SQL Statement and obtain the ResultSet. Finally, the ResultSet is mapped through the ResultSetHandler to obtain the object and return it. As shown below:

2.3 Mybatis source

  • Mybatis source address

2.4 Recommended Mybatis books

  • MyBatis from Entry to Mastery by Liu Zenghui
  • Xu Junming “MyBatis technology insider” source code

2.5 Mybatis url recommendation

  • MyBatis3 official Chinese document
  • Big bull zu Dajun’s blog

Mybatis series of articles from some big blog and Mybatis technical books, mainly used to record, summarize and combine some of their own ideas. Share and learn together

Out of control, Forrest Gump, happy to share, record every bit