赞
踩
tmpSql = oSql.substring(oSql.indexOf("<if")) + "</if>";
Element root = DocumentHelper.parseText(tmpSql).getRootElement();
String condition = root.attribute("test").getValue();
Object condObj = Ognl.parseExpression(condition);
Object value = Ognl.getValue(condObj, requestParams);
if (value instanceof Boolean) {
conditionResult = (Boolean) value;
tmpSql = <if test=\"pwd != null and pwd != ''\"> and a.password = #pwd# </if> // 然后将tempSql转成 xml 之后 再获取 test 也就是此时的condition = pwd != null and pwd != '' condObj = (pwd != null) && (pwd != "") //--------------------------------------------------> requestParams 是一个类似于Map的结构 里面保存着 pwd 的值 可能 Mybatis底层 解析 if标签就这么做的吧 但是此时我也没找到一个合适的例子 ------------------------------------》 留作 问题 标注一个
但是我始终没有想到 这个工具类有什么用 可能大佬的想法 我这个应用操作人员用不到
public static void main(String[] args) throws Exception {
JSONObject jsObject = new JSONObject();
// jsObject.put("username","2");
jsObject.put("password","1");
// Object username = Ognl.parseExpression("username !=null && username !=''");
// Object value = Ognl.getValue(username, jsObject);
String username = jsObject.getString("username");
if(null==username){
System.out.println("x");
}
/** * 处理SQL语句 * * @param oldSql * select * from s_a a * where <if test="name != null && name != ''"> * a.name = #name# * </if> * @return */ public String dealSqlIf(String oldSql, JSONObject requestParams) throws DocumentException, OgnlException { StringBuffer newSql = new StringBuffer(); String tmpSql = ""; Boolean conditionResult = false; // 未包含 条件语句 if (!oldSql.contains("<if")) { return oldSql; } String[] oSqls = oldSql.split("</if>"); for (String oSql : oSqls) { logger.debug("处理if 节点,当前处理的oSql=" + oSql + "总的oSqls = " + oSqls); if (StringUtil.isNullOrNone(oSql) || !oSql.contains("<if")) { newSql.append(oSql); continue; } if (!oSql.startsWith("<if")) { newSql.append(oSql.substring(0, oSql.indexOf("<if"))); } tmpSql = oSql.substring(oSql.indexOf("<if")) + "</if>"; Element root = DocumentHelper.parseText(tmpSql).getRootElement(); String condition = root.attribute("test").getValue(); Object condObj = Ognl.parseExpression(condition); Object value = Ognl.getValue(condObj, requestParams); if (value instanceof Boolean) { conditionResult = (Boolean) value; } else { throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, "配置错误,if语句配置错误 " + condition); } if (conditionResult) { newSql.append(root.getText()); } } // mybatis 可能就是通过 ognl来解析这个动态标签 比如说< if>
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。