`
yjhexy
  • 浏览: 327262 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

IBATIS 动态查询 学习

阅读更多

 

BATIS动态查询的实现主要是在iBATIS中使用安全的拼接语句,动态查询

  iBATIS比JDBC的优势之一,安全高效

 

动态查询的标签,可以在 DTD文档 http://ibatis.apache.org/dtd/sql-map-2.dtd 中找到如下:

<!ELEMENT sql (#PCDATA | include | dynamic | iterate | isParameterPresent | isNotParameterPresent | isEmpty | isNotEmpty | isNotNull | isNull | isNotEqual | isEqual | isGreaterThan | isGreaterEqual | isLessThan | isLessEqual | isPropertyAvailable | isNotPropertyAvailable)*>

 

 

 对应到 iBATIS动态查询几个常用属性

  ﹤ isPropertyAvailable ﹥  属性是存在

  ﹤ isNotPropertyAvailable ﹥  属性不存在

  ﹤ isNull ﹥  属性值是null

  ﹤ isEmpty ﹥  判断Collection.size ﹤ 1 或String.length()﹤1

  ﹤isEqual ﹥  等于

  ﹤ isNotEqual ﹥  不等于

  ﹤ isGreaterThan ﹥  大于

  ﹤ isGreaterEqual ﹥  大于等于

  ﹤ isLessThan ﹥  小于

  ﹤ isLessEqual ﹥  小于等于

 

那么这么多标签如何使用可以参考源代码,每一个标签都有一个类相对应,类图如下:

 


 

 

稍微讲解下:

首先看最上面的接口:

SqlTagHandler 包含了三个方法:

 // 一段标签开始的时候做的事情
 public int doStartFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject);

// 一段标签结束的时候做的事情
  public int doEndFragment(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuffer bodyContent);

// 追加到SQL语句 
  public void doPrepend(SqlTagContext ctx, SqlTag tag, Object parameterObject, StringBuffer bodyContent);

 

那么在这里我们主要关注实现类:

 

ConditionalTagHandler 在doStartFragment的时候调用了方法

public abstract boolean isCondition(SqlTagContext ctx, SqlTag tag, Object parameterObject);

 这个方法在各个动态查询的子类实现。方法如果返回true那么此标签内的内容将被追加。

 

到这里其实如果你想了解所有的动态查询到底怎么用,看下各个子类就都知道了。

 


===============================================

 

我将补充讲下:IsPropertyAvailableTagHandler 这个用法

 

我也将源代码贴出来如下:

package com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements;

import com.ibatis.common.beans.Probe;

import com.ibatis.common.beans.ProbeFactory;

import java.util.Map;

public class IsPropertyAvailableTagHandler extends ConditionalTagHandler {

 private static final Probe PROBE = ProbeFactory.getProbe();

public boolean isCondition(SqlTagContext ctx, SqlTag tag, Object parameterObject) {

    if (parameterObject == null) {

      return false;

    } else if (parameterObject instanceof Map) {

      return ((Map)parameterObject).containsKey(tag.getPropertyAttr());      

    } else {

      String property = getResolvedProperty(ctx, tag);

      // if this is a compound property, then we need to get the next to the last

      // value from the parameter object, and then see if there is a readable property

      // for the last value.  This logic was added for IBATIS-281 and IBATIS-293

      int lastIndex = property.lastIndexOf('.');

      if (lastIndex != -1) {

        String firstPart = property.substring(0, lastIndex);

        String lastPart = property.substring(lastIndex + 1);

        parameterObject = PROBE.getObject(parameterObject, firstPart);

        property = lastPart;

      }

      

      if (parameterObject instanceof Map) {

        // we do this because the PROBE always returns true for 

        // properties in Maps and that's not the behavior we want here

        return ((Map) parameterObject).containsKey(property);

      } else {

        return PROBE.hasReadableProperty(parameterObject, property);

      }
    }
  }
}

 

 

如代码所示他有三种用法:

1,当参数是null的时候,将返回false,SQL不被追加

2,当参数是Map的时候,将比对property是否是map中的一个key。如果是,那么追加SQL(是我们比较常用的)

3,当参数是复杂类型的时候,将比对property在这个复杂类型的类中是否存在,并且是可读的,如果是,那么追加SQL

 

 

  • 大小: 23.4 KB
分享到:
评论
1 楼 jc_dreaming 2010-04-16  

相关推荐

Global site tag (gtag.js) - Google Analytics