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
分享到:
相关推荐
ibatis_动态查询条件详解及需要注意的地方
IBATIS动态查询语句.doc
这个文件中包含了许多ibatis的查询语句,能够让你很清楚的知道,如何实现动态的查询。
ibatis动态多条件组合查询 实例 说明
IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习
Ibatis复杂查询语句.doc
NULL 博文链接:https://yhq1212.iteye.com/blog/1772214
Ibatis多表查询
自己整理的非常好用 好东西向大家分享 └ ^o^ ┘
操作数据库 iBATIS查询,java 和spring的配置方法
ibatis学习 ibatis总结 ibatis ibatis ibatis
ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记
Ibatis查询Id列表.doc
iBATIS入门学习
NULL 博文链接:https://raymondwang1314.iteye.com/blog/1195820
Ibatis多表查询,一个小小的多表查询实例教你如何用ibatis进行多表查询
ibatis in action的文档,详细介绍ibatis的查询等
ibatis动态注入
ibatis开发指南 ibatis资料文档 ibatis2.x_jar包 ibatis源码 ibatis实践项目
1.iBatis2学习笔记:基本原理和配置.doc 2.iBatis2学习笔记:与Spring2的整合.doc 3.iBatis2学习笔记:单表映射 .doc 4.iBatis2学习笔记:SqlMap的配置总结(18条).doc 5.iBatis2学习笔记:入参和返回值的问题.doc ...