欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

初识BoundSQL

程序员文章站 2022-04-22 07:59:12
...

参考文章
MyBatis原理系列(六)-手把手带你了解BoundSql的创建过程

什么是BoundSQL

原码:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.ibatis.mapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.session.Configuration;

public class BoundSql {

	// 最终解析的SQL,Mybatis将#{}和${}解析后的sql,其中#{}会被解析为?
    private String sql;
    // 参数映射
    private List<ParameterMapping> parameterMappings;
    // 参数对象
    private Object parameterObject;
    // 额外的参数
    private Map<String, Object> additionalParameters;
    // 元数据参数
    private MetaObject metaParameters;

    public BoundSql(Configuration configuration, String sql, List<ParameterMapping> parameterMappings, Object parameterObject) {
        this.sql = sql;
        this.parameterMappings = parameterMappings;
        this.parameterObject = parameterObject;
        this.additionalParameters = new HashMap();
        this.metaParameters = configuration.newMetaObject(this.additionalParameters);
    }

    public String getSql() {
        return this.sql;
    }

    public List<ParameterMapping> getParameterMappings() {
        return this.parameterMappings;
    }

    public Object getParameterObject() {
        return this.parameterObject;
    }

    public boolean hasAdditionalParameter(String name) {
        PropertyTokenizer prop = new PropertyTokenizer(name);
        String indexedName = prop.getIndexedName();
        return this.additionalParameters.containsKey(indexedName);
    }

    public void setAdditionalParameter(String name, Object value) {
        this.metaParameters.setValue(name, value);
    }

    public Object getAdditionalParameter(String name) {
        return this.metaParameters.getValue(name);
    }
}

BoundSql就是对解析后的SQL的描述,包括对动态标签的解析,并且将**#{}解析为占位符?**,还包含参数的描述信息。可以看做是解析后的sql描述对象。

to be continued…