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.
Using native JDBC query, there are two pain points:
- Use very complicated and need to deal with a variety of database exception, and also need to close all kinds of resources.
- Data into trouble. Needs to be set before the query from Java object property values to
PreparedStatement
, the following query returns they need from theResultSet
get 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 MappedStatement
subclass. The configuration will generate corresponding select SelectStatement
objects.
MappedStatement
Class follows FIG.
In MappedStatement
saving presence will be two important objects, ParameterMap
and 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.QueryDO
get property values in the object, and then PreparedStatement.setxx
set 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 ParameterMap
the object.
ParameterMapping will save resolution fields related information.
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 PreparedStatement
objects.
Then ibatis based ParameterMapping
and parameterClass
create the appropriate specified type dataExchange
and parameterPlan
objects.
In which parameterPlan
the object will follow the ParameterMapping
preservation of order in the variable array setter and getter methods array.
dataExchange
Will follow ParameterMapping
a reflective obtain parameterPlan getter method returns a value generation procedure array parameters
array.
The last cycle ParameterMapping array, the TypeHandler
call PreparedStatement.setxx
set correlation value.
TypeHandler
There are many sub-categories, these subclasses to correctly handle the Java object into the database type.
The timing diagram for the conversion:
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 ResultMap
objects.
When SQL execution ended return ResultSet
after the object, use ResultSet.getMetaData()
to obtain return information metadata object ResultSetMetaData
.
From the ResultSetMetaData
return result field name, type and other information may be acquired, and stored in order of ResultMapping
the array.
Then follow ResultMapping
the array using a TypeHandler
call to ResultSet.getxx
get the actual return data, save it to columnValues
the array.
In ResultMap
the object based on ResultMapping
the resultClass
specified type suitable dataExchange
and resultPlan
objects. resultPlan
The above objects parameterPlan
as objects are also variable holds an array of setter and getter methods.
Finally, according to the first resultClass
retroreflective generating object, and then use the reflection to invoke resultPlan
the setter method, correlation values are sequentially provided.
The timing diagram mapping returns an object:
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 queryForObject
calls to SqlMapExecutorDelegate
. In SqlMapExecutorDelegate
the premise will do some preparation, such as preparing the transaction, the final will be entrusted to the SQL statement SqlExecutor
executed.
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#executeQuery
implementation process is divided into the following three steps.
The first step, acquiring PreparedStatement
, using conn.prepareStatement(sql)
get.
The second step calls the PreparedStatement.setxxx
method 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 ResultSet
capture 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