Mybatis source parsing --SqlNode & SqlSource

SqlNode&SqlSource

sql xml file node is initialized to be resolved at MappedStatement objects, wherein the sql statement is parsed into SqlSource objects, dynamic Sql represented by respective nodes like interface implementation class SqlNode

public interface SqlSource {

  BoundSql getBoundSql(Object parameterObject);

}

Which, DynamicSqlSource responsible for handling dynamic SQL statements, RawSqlSource responsible for handling static statements, will eventually be packaged into SQL statements StaticSqlSource Objects (SQL statement may contain? Placeholder)

Combined mode

SqlNode implementation class uses a combination of patterns, such that if the user operates the operation mode of the whole tree node as a single, hides the complexity of the object

DynamicContext

Mainly for recording segments dynamic statement parsed SQL statement generated in the process, use StringBuilder achieved by adding SQL fragments append () method to obtain a final statement by getSql () method

SqlNode

public interface SqlNode {
  boolean apply(DynamicContext context);
}

Providing an apply () method only, as various different implementation class corresponding to dynamic SQL nodes, such as {} $ TextSqlNode responsible comprising a dynamic node, IfSqlNode responsible <if>nodes, etc., through SqlNode.apply () before and after treatment the following SQL statement:
prior to:

<select>
select * from user 
	<where> 
		id = #{id} 
	</where>
</select>

after that:

select * from user where id = #{id}

SqlSourceBuilder

SqlSourceBuilder task is binding GenericTokenParse SQL statements in #{id}various attributes of the parsed id, and packaged into ParamterMapping ParamterMappings objects into the collection, after which #{id}become
ParamterMapping object structure comprising: a
parameter name, in / out parameters, java type , JDBC type parameter corresponds typeHandler object parameter corresponds ResultMapId, jdbcTypeName etc.
is parsed into:

select * from user where id = ?

DETAILED attribute information parameter in the set ParamterMappings

DynamicSqlSource

Using rootSqlNode field (MixedSqlNode type, a set of stored SqlNode, Apply () call goes through all SqlNode.apply () method within the set) dynamic SQL parsing node, then processing using SqlSourceBuilder placeholder, and copy the parameters DynamicContext.Bindings to save the collection of information additionalParameters

RawSqlSource

If the node contains only #{}does not contain dynamic SQL node or as resolved ${}, for the static statements, use the implementation class to parse and create StaticSqlSource objects, and DynamicSqlSource achieve roughly the same, less bindings steps

Published 98 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/Mutou_ren/article/details/102811875