上一篇博文中,简要介绍了Mybatis动态sql的基本用法和基本设计结构,本篇博文重点阐述一些动态sql的技术细节,#{name}和${name}的区别,将在本篇博文中揭晓。也许读者早已了解它们之间的区别,可是,做为技术内幕,咱们不只要了解它们的区别,还要介绍它们的工做原理,是否是很开森呢?java
#{name}:表示这是一个参数(ParameterMapping)占位符,值来自于运行时传递给sql的参数,也就是XXXMapper.xml里的parameterType。其值经过PreparedStatement的setObject()等方法赋值。node
动态sql中的<bind>标签绑定的值,也是使用#{name}来使用的。mysql
#{name}用在sql文本中。sql
${name}:表示这是一个属性配置占位符,值来自于属性配置文件,好比jdbc.properties,其值经过相似replace方法进行静态替换。好比${driver},将被静态替换为com.mysql.jdbc.Driver。apache
${name}则能够用在xml的Attribute属性,还能够用在sql文本当中。网络
<select id="countAll" resultType="${driver}"> select count(1) from ( select stud_id as studId , name, email , dob , phone from students #{offset}, ${driver} ) tmp </select>
org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode()部分源码。app
public void parseStatementNode() { //... XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant); includeParser.applyIncludes(context.getNode()); // ... }
org.apache.ibatis.builder.xml.XMLIncludeTransformer.applyIncludes(Node, Properties)部分源码。ide
private void applyIncludes(Node source, final Properties variablesContext) { if (source.getNodeName().equals("include")) { // new full context for included SQL - contains inherited context and new variables from current include node Properties fullContext; String refid = getStringAttribute(source, "refid"); // replace variables in include refid value refid = PropertyParser.parse(refid, variablesContext); Node toInclude = findSqlFragment(refid); Properties newVariablesContext = getVariablesContext(source, variablesContext); if (!newVariablesContext.isEmpty()) { // merge contexts fullContext = new Properties(); fullContext.putAll(variablesContext); fullContext.putAll(newVariablesContext); } else { // no new context - use inherited fully fullContext = variablesContext; } applyIncludes(toInclude, fullContext); if (toInclude.getOwnerDocument() != source.getOwnerDocument()) { toInclude = source.getOwnerDocument().importNode(toInclude, true); } source.getParentNode().replaceChild(toInclude, source); while (toInclude.hasChildNodes()) { toInclude.getParentNode().insertBefore(toInclude.getFirstChild(), toInclude); } toInclude.getParentNode().removeChild(toInclude); } else if (source.getNodeType() == Node.ELEMENT_NODE) { NodeList children = source.getChildNodes(); for (int i=0; i<children.getLength(); i++) { applyIncludes(children.item(i), variablesContext); } } else if (source.getNodeType() == Node.ATTRIBUTE_NODE && !variablesContext.isEmpty()) { // replace variables in all attribute values // 经过PropertyParser替换全部${xxx}占位符(attribute属性) source.setNodeValue(PropertyParser.parse(source.getNodeValue(), variablesContext)); } else if (source.getNodeType() == Node.TEXT_NODE && !variablesContext.isEmpty()) { // replace variables ins all text nodes // 经过PropertyParser替换全部${xxx}占位符(文本节点) source.setNodeValue(PropertyParser.parse(source.getNodeValue(), variablesContext)); } }
也就是说,Mybatis在解析<include>标签时,就已经静态替换${name}占位符了。工具
public class PropertyParser { private PropertyParser() { // Prevent Instantiation } public static String parse(String string, Properties variables) { VariableTokenHandler handler = new VariableTokenHandler(variables); GenericTokenParser parser = new GenericTokenParser("${", "}", handler); return parser.parse(string); } private static class VariableTokenHandler implements TokenHandler { private Properties variables; public VariableTokenHandler(Properties variables) { this.variables = variables; } @Override public String handleToken(String content) { if (variables != null && variables.containsKey(content)) { return variables.getProperty(content); } return "${" + content + "}"; } } }
#{name}是ParameterMapping参数占位符,Mybatis将会把#{name}替换为?号,并经过OGNL来计算#{xxx}内部的OGNL表达式的值,做为PreparedStatement的setObject()的参数值。ui
举例:#{item.name}将被替换为sql的?号占位符,item.name则是OGNL表达式,OGNL将计算item.name的值,做为sql的?号占位符的值。
若是只有静态sql,#{name}将在解析xml文件时,完成替换为?占位符。若是有动态sql的内容,#{name}将在执行sql时,动态替换为?占位符。
org.apache.ibatis.scripting.xmltags.XMLScriptBuilder.parseScriptNode()。
public SqlSource parseScriptNode() { List<SqlNode> contents = parseDynamicTags(context); MixedSqlNode rootSqlNode = new MixedSqlNode(contents); SqlSource sqlSource = null; if (isDynamic) { sqlSource = new DynamicSqlSource(configuration, rootSqlNode); } else { sqlSource = new RawSqlSource(configuration, rootSqlNode, parameterType); } return sqlSource; }
public class RawSqlSource implements SqlSource { private final SqlSource sqlSource; public RawSqlSource(Configuration configuration, SqlNode rootSqlNode, Class<?> parameterType) { this(configuration, getSql(configuration, rootSqlNode), parameterType); } public RawSqlSource(Configuration configuration, String sql, Class<?> parameterType) { SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class<?> clazz = parameterType == null ? Object.class : parameterType; // 在这里完成#{xxx}替换为?号 sqlSource = sqlSourceParser.parse(sql, clazz, new HashMap<String, Object>()); } private static String getSql(Configuration configuration, SqlNode rootSqlNode) { DynamicContext context = new DynamicContext(configuration, null); // 建立RawSqlSource时,就完成sql的拼接工做,由于它没有动态sql的内容,Mybatis初始化时,就能肯定最终的sql。 rootSqlNode.apply(context); return context.getSql(); } @Override public BoundSql getBoundSql(Object parameterObject) { return sqlSource.getBoundSql(parameterObject); } }
org.apache.ibatis.builder.SqlSourceBuilder.parse(String, Class<?>, Map<String, Object>)。
public SqlSource parse(String originalSql, Class<?> parameterType, Map<String, Object> additionalParameters) { // 使用ParameterMappingTokenHandler策略来处理#{xxx} ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType, additionalParameters); GenericTokenParser parser = new GenericTokenParser("#{", "}", handler); String sql = parser.parse(originalSql); return new StaticSqlSource(configuration, sql, handler.getParameterMappings()); }
GenericTokenParser.java是通用解析占位符的工具类,它能够解析${name}和#{name},那么,解析到${name}和#{name}后,要如何处理这样的占位符,则由不一样的策略TokenHandler来完成。
GenericTokenParser.java负责解析sql中的占位符${name}和#{name},TokenHandler则是如何处理这些占位符。
ParameterMappingTokenHandler:处理#{xxx}占位符。
private static class ParameterMappingTokenHandler extends BaseBuilder implements TokenHandler { private List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>(); private Class<?> parameterType; private MetaObject metaParameters; public ParameterMappingTokenHandler(Configuration configuration, Class<?> parameterType, Map<String, Object> additionalParameters) { super(configuration); this.parameterType = parameterType; this.metaParameters = configuration.newMetaObject(additionalParameters); } public List<ParameterMapping> getParameterMappings() { return parameterMappings; } @Override public String handleToken(String content) { // 建立一个ParameterMapping对象,并返回?号占位符 parameterMappings.add(buildParameterMapping(content)); return "?"; } //.. }
VariableTokenHandler:处理${xxx}占位符。
private static class VariableTokenHandler implements TokenHandler { private Properties variables; public VariableTokenHandler(Properties variables) { this.variables = variables; } @Override public String handleToken(String content) { if (variables != null && variables.containsKey(content)) { return variables.getProperty(content); } return "${" + content + "}"; } }
DynamicCheckerTokenParser:空实现,动态sql标签,都由它来标识。
BindingTokenParser:用于在注解Annotation中处理${xxx},待研究。
至此,${name}将直接替换为静态Properties的静态属性值,而#{name}将被替换为?号,并同时建立了ParameterMapping对象,绑定到参数列表中。
对于RawSqlSource,因为是静态的sql,Mybatis初始化时就生成了最终能够直接使用的sql语句,即在建立RawSqlSource时,就直接生成。而DynamicSqlSource,则是执行sql时,才动态生成。
public class DynamicSqlSource implements SqlSource { private Configuration configuration; private SqlNode rootSqlNode; public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) { this.configuration = configuration; this.rootSqlNode = rootSqlNode; } @Override public BoundSql getBoundSql(Object parameterObject) { DynamicContext context = new DynamicContext(configuration, parameterObject); // 逐一调用各类SqlNode,拼接sql rootSqlNode.apply(context); SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings()); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) { boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); } return boundSql; } }
BoundSql不只保存了最终的可执行的sql,还保存了sql中?号占位符的参数列表。
public class BoundSql { private String sql; private List<ParameterMapping> parameterMappings; // ... }
最后,在执行sql时,经过org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(PreparedStatement)方法,遍历List<ParameterMapping> parameterMappings = boundSql.getParameterMappings()来逐一对sql中的?号占位符进行赋值操做。
整个sql处理变量占位符的流程就完成了。
咱们就举一个略微复杂一点的ForEachSqlNode的拼接sql原理。
public class ForEachSqlNode implements SqlNode { // OGNL表达式计算器 private ExpressionEvaluator evaluator; //... @Override public boolean apply(DynamicContext context) { Map<String, Object> bindings = context.getBindings(); // 计算集合表达式 final Iterable<?> iterable = evaluator.evaluateIterable(collectionExpression, bindings); if (!iterable.iterator().hasNext()) { return true; } boolean first = true; applyOpen(context); int i = 0; // 遍历拼接sql for (Object o : iterable) { DynamicContext oldContext = context; if (first) { context = new PrefixedContext(context, ""); } else if (separator != null) { context = new PrefixedContext(context, separator); } else { context = new PrefixedContext(context, ""); } int uniqueNumber = context.getUniqueNumber(); // Issue #709 if (o instanceof Map.Entry) { @SuppressWarnings("unchecked") Map.Entry<Object, Object> mapEntry = (Map.Entry<Object, Object>) o; applyIndex(context, mapEntry.getKey(), uniqueNumber); applyItem(context, mapEntry.getValue(), uniqueNumber); } else { applyIndex(context, i, uniqueNumber); applyItem(context, o, uniqueNumber); } contents.apply(new FilteredDynamicContext(configuration, context, index, item, uniqueNumber)); if (first) { first = !((PrefixedContext) context).isPrefixApplied(); } context = oldContext; i++; } applyClose(context); return true; } //... }
Mybatis的所有动态sql内容,至此就所有介绍完了,在实际工做中,绝大多数的sql,都是动态sql。
最后,庆祝中国女排里约奥运夺冠。
版权提示:文章出自开源中国社区,若对文章感兴趣,可关注个人开源中国社区博客(http://my.oschina.net/zudajun)。(通过网络爬虫或转载的文章,常常丢失流程图、时序图,格式错乱等,仍是看原版的比较好)