SqlNode
SqlNode代表我们写的动态sql里的每个节点,那么什么叫动态sql呢?动态sql就是会根据我们的入参,来动态生成最终的sql,比如下面这个例子
<select id="findPost" resultType="org.apache.ibatis.domain.blog.Post">
SELECT *
FROM POST P
<where>
<choose>
<when test="id != null">id = #{id}</when>
<when test="author_id != null">AND author_id = #{author_id}</when>
<otherwise>
<if test="ids != null">
AND id IN
<foreach item="item_id" index="index" open="(" close=")" separator="," collection="ids">#{ids[${index}]}
</foreach>
</if>
<trim prefix="AND">
<include refid="byBlogId">
<property name="prefix" value="blog"/>
</include>
</trim>
</otherwise>
</choose>
</where>
</select>
DynamicContext
在介绍SqlNode之前,首先介绍下SqlNode中常用的DynamicContext
DynamicContext从名字可以看出,它使动态sql解析过程中的上下文,其主要作用有两个:
- 将动态sql解析之后的sql片段保存下来
- 持有解析过程中使用到的参数
public class DynamicContext {
public static final String PARAMETER_OBJECT_KEY = "_parameter";
public static final String DATABASE_ID_KEY = "_databaseId";
static {
// 设置当通过ognl来访问ContextMap类型的对象时,使用ContextAccessor来进行方法
// 使用Cont