ibatis core principle of analytic

Find the cause of a recent production problems, it requires in-depth study of the source code ibatis framework. Although the cause of the problem has nothing to do with the final proof ibatis, but the process to deepen the understanding of the principles ibatis framework.

This article will say something about the principles ibatis framework.

Now many people may no longer use or ibatis also heard ibatis, but be sure to find out about Mybatis. ibatis is the predecessor Mybatis framework, although ibatis framework has been relatively old, but consistent with its core function and Mybatis.

ibatis solve pain points

We look at an example of using JDBC query.

QueryDaoByJDBC1.png

Using native JDBC query, there are two pain points:

  1. Use very complicated and need to deal with a variety of database exception, and also need to close all kinds of resources.
  2. Data into trouble. Needs to be set before the query from Java object property values to PreparedStatement, the following query returns they need from the ResultSetget set to return to the return object.

Ibatis encapsulated in these complex database query code is connected, and a variety of abnormalities, and closed processing resources. In addition ibatis automatically processed automatically translate between Java objects and database types, so do decoupling between the business code and SQL code.

Data type conversion principle

Data types into mainly divided into two, Java objects data into SQL query is converted into data types. Database information is mapped to the second query returns a Java object.

ibatis SQL needs to be defined in the configuration file, a SQL query statement configuration is as follows:


      <select id="queryName" parameterClass="com.query.QueryDO"  resultClass="com.query.QueryDO" >
		select  * from  TEST_QUERY where ID=#id#

      </select>
复制代码

frame parsing process will start ibatis profile, generated MappedStatementsubclass. The configuration will generate corresponding select SelectStatementobjects.

MappedStatement Class follows FIG.

MappedStatement.png

In MappedStatementsaving presence will be two important objects, ParameterMapand ResultMap, through these two objects will complete the conversion of Java types to each other with the database type.

Java objects back into the database type

To select the above configuration, for example, we need to do here it is from the incoming com.query.QueryDOget property values in the object, and then PreparedStatement.setxxset to the query parameters.

When ibatis parsing configuration SQL statements, will get the content between the # will replace it ?. Then in order to save an ParameterMapping[]array, the array will be saved to ParameterMapthe object.

ParameterMapping will save resolution fields related information.

ParameterMapping.png

After the final SQL resolves to:


select  * from  TEST_QUERY where ID=?

复制代码

The SQL you can connection.prepareStatement("select * from TEST_QUERY where ID=?");generate PreparedStatementobjects.

Then ibatis based ParameterMappingand parameterClasscreate the appropriate specified type dataExchangeand parameterPlanobjects.

In which parameterPlanthe object will follow the ParameterMappingpreservation of order in the variable array setter and getter methods array.

dataExchangeWill follow ParameterMappinga reflective obtain parameterPlan getter method returns a value generation procedure array parametersarray.

The last cycle ParameterMapping array, the TypeHandlercall PreparedStatement.setxxset correlation value.

TypeHandler.png

TypeHandler There are many sub-categories, these subclasses to correctly handle the Java object into the database type.

The timing diagram for the conversion:

Java object into a database object

Timing Diagram from: www.ibm.com/developerwo...

Database fields are mapped to Java objects

After the implementation of SQL query will return results where the SQL query will return results into the results com.query.QueryDO. It should be used in the above-mentioned ResultMapobjects.

When SQL execution ended return ResultSetafter the object, use ResultSet.getMetaData()to obtain return information metadata object ResultSetMetaData.

From the ResultSetMetaDatareturn result field name, type and other information may be acquired, and stored in order of ResultMappingthe array.

Then follow ResultMappingthe array using a TypeHandlercall to ResultSet.getxxget the actual return data, save it to columnValuesthe array.

In ResultMapthe object based on ResultMappingthe resultClassspecified type suitable dataExchangeand resultPlanobjects. resultPlanThe above objects parameterPlanas objects are also variable holds an array of setter and getter methods.

Finally, according to the first resultClassretroreflective generating object, and then use the reflection to invoke resultPlanthe setter method, correlation values are sequentially provided.

The timing diagram mapping returns an object:

Database fields are mapped to Java objects

Timing Diagram from: www.ibm.com/developerwo...

ibatis boilerplate code

The above principle ibatis finished converting data types, and then we look at the ibatis call JDBC boilerplate code.

Case of a query using ibatis, such as queryForObjectcalls to SqlMapExecutorDelegate. In SqlMapExecutorDelegatethe premise will do some preparation, such as preparing the transaction, the final will be entrusted to the SQL statement SqlExecutorexecuted.

image.png

As used herein, delegator mode, a subject receiving a request to delegate the request to another object processing. The advantage of this model is that the decoupling of the link with the business code of the actual code execution, that hide the real execution of foreign objects, easy to expand.

In the SqlExecutor#executeQueryimplementation process is divided into the following three steps.

image.png

The first step, acquiring PreparedStatement, using conn.prepareStatement(sql)get.

image.png

The second step calls the PreparedStatement.setxxxmethod to set the parameters. Java objects in the above type is converted into SQL type done here.

The third step is to call PreparedStatement.execute()execute SQL statements.

The fourth step, using ResultSetcapture the return value, will complete the conversion of the database type and Java type in this step.

Help link

Depth analysis of the system architecture and mapping principle iBATIS framework

Other platforms .png

Guess you like

Origin juejin.im/post/5d4e5730518825415d05fb61