【转帖】IBatis模糊查詢
上一篇 /
下一篇 2012-12-10 17:46:47
/ 个人分类:java
<!--
在ibatis中使用安全的拼接语句,动态查询
ibatis比JDBC的优势之一,安全高效
说明文字在注释中
-->
<selectid="selectAllProducts"parameterClass="Product"resultMap="ProductResult">
select id,note from Product
<dynamicprepend="WHERE">
<!--isNotNull判断参数是否存在,Integer类型-->
<isNotNullproperty="id">
<!--isGreaterThan判断参数是否大于compareValue,isGreaterEquals是大于等于-->
<isGreaterThanprepend=" and "property="id"compareValue="0">
id = #id#
</isGreaterThan>
</isNotNull>
<!--isNotEmpty判断字串不为空,isEmpty可以判断字串为空-->
<isNotEmptyprepend=" and "property="note">
<!--模糊查询不能用#,#在是用prepareStatement的?插入参数,$是文本替换-->
note like '%$note$%'
</isNotEmpty>
</dynamic>
</select>
用Map传参数
<selectid="selectAllProducts"parameterClass="java.util.HashMap"resultMap="ProductResult">
select id,note from Product
<dynamicprepend="WHERE">
<!--isPropertyAvailable判断属性是否有效-->
<isPropertyAvailableproperty="id">
<isNotNullproperty="id">
<!--isLessThan判断参数是否小于compareValue,isLessEquals是小于等于-->
<isLessThanprepend=" and "property="id"compareValue="10">
id = #id#
</isLessThan>
</isNotNull>
</isPropertyAvailable>
</dynamic>
</select>
<!--------------------------------------------------------------------几个常用属性-->
<isPropertyAvailable>属性是存在
<isNotPropertyAvailable>属性不存在
<isNull>属性值是null
<isEmpty>判断Collection.size<1或String.length()<1
<isEqual>等于
<isNotEqual>不等于
<isGreaterThan>大于
<isGreaterEqual>大于等于
<isLessThan>小于
<isLessEqual>
收藏
举报
TAG: