DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode); setSqlSource(ms, dynamicSqlSource);
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode); setSqlSource(ms, dynamicSqlSource);
public SqlSource parseScriptNode() { MixedSqlNode rootSqlNode = parseDynamicTags(context); SqlSource sqlSource = null; if (isDynamic) { sqlSource = new DynamicSqlSource(configuration, rootSqlNode); } else { sqlSource = new RawSqlSource(configuration, rootSqlNode, parameterType); } return sqlSource; }
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; }
@Override public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) { // issue #3 if (script.startsWith("<script>")) { XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver()); return createSqlSource(configuration, parser.evalNode("/script"), parameterType); } else { // issue #127 script = PropertyParser.parse(script, configuration.getVariables()); TextSqlNode textSqlNode = new TextSqlNode(script); if (textSqlNode.isDynamic()) { return new DynamicSqlSource(configuration, textSqlNode); } else { return new RawSqlSource(configuration, script, parameterType); } } }
@Override public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) { // issue #3 if (script.startsWith("<script>")) { XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver()); return createSqlSource(configuration, parser.evalNode("/script"), parameterType); } else { // issue #127 script = PropertyParser.parse(script, configuration.getVariables()); TextSqlNode textSqlNode = new TextSqlNode(script); if (textSqlNode.isDynamic()) { return new DynamicSqlSource(configuration, textSqlNode); } else { return new RawSqlSource(configuration, script, parameterType); } } }
/** * 重新设置SqlSource * * @param ms ms * @param parameter 参数 */ public void resetSqlSource(MappedStatement ms, MapperMethod.ParamMap parameter) { Method method = methodMap.get(getMethodName(ms)); if (method.getReturnType() == Void.TYPE) { invoke(method, this, ms, parameter); } else if (SqlNode.class.isAssignableFrom(method.getReturnType())) { SqlNode sqlNode = (SqlNode) invoke(method, this, ms, parameter); DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode); setSqlSource(ms, dynamicSqlSource); } else if (String.class.equals(method.getReturnType())) { String xmlSql = (String) invoke(method, this, ms, parameter); SqlSource sqlSource = createSqlSource(ms, xmlSql); //替换原有的SqlSource setSqlSource(ms, sqlSource); } else { throw new RuntimeException("自定义Mapper方法返回类型错误,可选的返回类型为void,SqlNode,String三种!"); } }
} else if (SqlNode.class.isAssignableFrom(method.getReturnType())) { SqlNode sqlNode = (SqlNode) method.invoke(this, ms); DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode); setSqlSource(ms, dynamicSqlSource); } else if (String.class.equals(method.getReturnType())) {
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode); setSqlSource(ms, dynamicSqlSource);
/** * 处理MapperClass内的匹配NamedMethod的MappedStatement以及SqlSource * * @param configuration MyBatis配置对象 * @param mapperClass Mapper的类型 */ public void namedSupport(Configuration configuration, Class<?> mapperClass) { if (ObjectUtils.isEmpty(mapperClass)) { throw new EnhanceFrameworkException("处理NamedMethod MappedStatement时,参数[mapperClass]为必填项."); } // 获取Mapper内定义的方法不包含上级接口 Method[] methods = mapperClass.getDeclaredMethods(); // 遍历Mapper内定义的每一个方法 for (Method method : methods) { // MappedStatement Id String statementId = mapperClass.getName() + "." + method.getName(); // 如果匹配方法规则查询 // 执行创建MappedStatement对象 // 调用对应的Provider,执行重载SqlSource if (!configuration.hasStatement(statementId) && NamedMethodHelper.isMatchNamed(method.getName())) { logger.debug("Invoke Named Reload : {} SqlSource.", statementId); MappedStatement statement = new MappedStatement.Builder(configuration, statementId, new DynamicSqlSource(configuration, new TextSqlNode("this is empty.")), getSqlCommandType(method.getName())).build(); // 将MappedStatement添加到Configuration configuration.addMappedStatement(statement); // 会自动根据方法规则匹配对应的Provider BaseProvider provider = ProviderHelper.getMethodProvider(statementId); provider.invokeNamedProviderMethod(statement); } } }
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode); setSqlSource(ms, dynamicSqlSource);