";Element root = DocumentHelper.parseText(tmpSql).getRootElement();String condition = root.attribute("test").getValue();Object condObj = Ognl.parseExpression(condition);Object value = Ogn_mybatisplus底层如">
当前位置:   article > 正文

mybatis是如何处理If标签的_mybatisplus底层如何处理

mybatisplus底层如何处理
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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
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标签就这么做的吧 
 但是此时我也没找到一个合适的例子
 ------------------------------------》
 留作 问题  标注一个

 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  但是我始终没有想到 这个工具类有什么用  可能大佬的想法 我这个应用操作人员用不到
    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");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

    /**
     * 处理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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/255667
推荐阅读
相关标签