[Mybatis] A little insight into the idea of source code design

foreword

MyBatis is a common Java database access layer framework. In daily work, developers mostly use MyBatis ; first of all, it is a very good and flexible ORM framework, so no matter how good it is, it is basically operating database data, that is, JDBC; then let's recall JDBC:

JDBC

First, the basic steps of using JDBC to access the database:

  1.     Load the JDBC driver:
  2.     Define the connection URL, establish the connection: jdbc: subprotocol, and load the server's host name, port, database name (or reference); display the call to commit()
  3.     Create a Statement object: each Statement object can only open one ResultSet object
  4.     Execute query or update: A. Query operation: executeQuery (SQL statement) B. Maintenance operation: executeUpdate (SQL statement)
        C. Batch operation: executeBat
  5.     Result handling: first column index of row in ResultSet is 1, not 0
  6.     Close the connection: in the order of ResultSet, Statement, Connection

JDBC-Statement (prepared statement): divided into three types: static sql, precompiled, stored procedure

The benefit of PreparedStatement: preventing sql injection

  1. Depending on the server's support for precompiled queries, and how efficiently the driver handles raw queries, the performance benefits of prepared statements can vary widely.
  2. Security is another feature of prepared statements. We recommend using prepared statements or stored procedures when accepting user input through HTML forms and then updating the database.
  3. Prepared statements also correctly handle quotes embedded in strings and handle non-character data (such as sending serialized objects to the database)   

Advantages and disadvantages of CallableStatement ( stored procedure statement):

  1. Advantages: Syntax errors can be found at compile time, not at runtime; Database stored procedures can run much faster than regular SQL queries; Programmers only need to know input and output parameters, not table structure. Also, because the database language has access to a few features native to the database (sequences, triggers, multiple cursors), it may be easier to write stored procedures than it is to use the Java programming language.
  2. Disadvantage: The business logic of the stored procedure runs on the database server, not the client or Web server. While the industry trend is to move as much business logic out of the database as possible and put it in JavaBean components (or in larger systems, Enterprise JavaBean components), the main motivation for using this approach in a Web architecture is: the database Access and network I/O are often performance bottlenecks.

Mybatis

So let's talk about the design and implementation process of mybatis corresponding to the steps of JDBC. I understand that it is a sql-driven framework. The steps are as follows:

  1. sqlSession: the most important interface entry
  2. Executor: pay attention to the execution in this jdbc
  3. StatementHandler: Create various Statement objects
  4. parameterHandler: handles parameters
  5. ResultSetHandler: processing results, corresponding to JDBC's ResultSet;
  6. Config configuration file: a major feature of mybatis, not only the configuration of database connections and other related configurations, the configuration of some properties, and the configuration of some plugins; the best feature of mybatis is that you can write some plugin plugins yourself, such as the paging we have all used, etc.
  7. Intercepter: Interceptor, do personal word processing

The implementation principle of the plugin plug-in is mainly by generating proxy objects (static proxy, dynamic proxy) and processing it together with the Intercepter, that is, we can deal with the plug-ins that are wrong in the first five processes above and do what we need. Operation; for example, paging can be done in two ways: one is processed in the resultSet part, but this method is obviously not good, and it cannot optimize the performance of executing sql very well. It just paginates the query results through memory; then it is better to StatementHandler eases processing for paging, that is, before querying sql.

According to the steps, after the ResultHandler alleviates the processing, it returns to the Executor and then returns to the calling interface of the client.

You may see that the step process of mybatis seems to be the opposite of jdbc, why not first Statement, then Executor; this is actually an idea designed by mybatis, adopting the design pattern, the Executor here is not an executor, but a parameter For Statement; Mybatis provides an operation interface class Executor, which provides interface functions such as update, query, flushStatements, commit, and rollback .

There are three main about SqlSession: Default, Manager, Template.

Summarize

So far, this is the general design idea of ​​Mybatis. Students can follow the steps to start research and study from sqlSession.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325468410&siteId=291194637