1.介绍

 <select id="getUsers"

 parameterClass="user"

 resultMap="get-user-result"> 

 select

 id,

 name,

 sex

 from t_user

 <dynamic prepend="WHERE">

    <isNotEmpty prepend="AND" property="name">

        name like #name#

    </isNotEmpty>

    <isNotEmpty prepend="AND" property="address">

        address like #address#

    </isNotEmpty>

 </dynamic>

 </select>

通过dynamic 节点,可以定义了一个动态的WHERE 子句。此WHERE 子句中将可能包含两个针对name 和address 字段的判断条件。而这两个字段是否加入检索取决于用户所提供的查询条件。

这个节点对应的语义是,如果参数类的"name"属性非空(isNotEmpty,即非空字符串””),则在生成的SQL Where字句中包括判定条件(name like #name#),其中#name#将以参数类的name属性值填充。

name属性对应的isNotEmpty节点,由于ibatis会自动判定是否需要追加prepend前缀,这里(name like #name#)是WHERE 子句中的第一个条件子句,无需AND 前缀,所以自动省略。

判定节点并非仅限于isNotEmpty,ibatis中提供了丰富的判定定义功能。可以分两类:

一、 一元判定

一元判定是针对属性值本身的判定,如属性是否为NULL,是否为空值等。

上面示例中isNotEmpty就是典型的一元判定。

一元判定节点有:

<isPropertyAvailable> 参数类中是否提供了此属性

<isNotPropertyAvailable> 与<isPropertyAvailable>相反

<isNull> 属性值是否为NULL

<isNotNull> 与<isNull>相反

<isEmpty> 如果属性为Collection或者String,其size是否<1,

如果非以上两种类型,则通过

String.valueOf(属性值)

获得其String类型的值后,判断其size是否<1

<isNotEmpty> 与<isEmpty>相反。

二、二元判定

二元判定有两个判定参数,一是属性名,而是判定值,如

<isGreaterThan prepend="AND" property="age"

compareValue="18">

(age=#age#)

</isGreaterThan>

其中,property="age"指定了属性名”age”,compareValue=”18”指明

了判定值为”18”。

上面判定节点isGreaterThan 对应的语义是:如果age 属性大于

18(compareValue),则在SQL中加入(age=#age#)条件。

二元判定节点有:

节点名 属性值与compareValues的关系

<isEqual> 相等。

<isNotEqual> 不等。

<isGreaterThan> 大于

<isGreaterEqual> 大于等于

<isLessThan> 小于

<isLessEqual> 小于等于

2.列子

对于一些特殊符号,如大于号>、小于号< 等需要写在<![CDATA[]]中方可有效,否则失效。
1、动态SQL片段
通过SQL片段达到代码复用
 <!-- 动态条件分页查询 -->
<sql id="sql_count">
select count(*)
</sql>
<sql id="sql_select">
select *
</sql>
<sql id="sql_where">
from icp
<dynamic prepend="where">
<isNotEmpty prepend="and" property="name">
name like '%$name$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="path">
path like '%path$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="area_id">
area_id = #area_id#
</isNotEmpty>
<isNotEmpty prepend="and" property="hided">
hided = #hided#
</isNotEmpty>
</dynamic>
<dynamic prepend="">
<isNotNull property="_start">
<isNotNull property="_size">
limit #_start#, #_size#
</isNotNull>
</isNotNull>
</dynamic>
</sql>
<select id="findByParamsForCount" parameterClass="map" resultClass="int">
<include refid="sql_count"/>
<include refid="sql_where"/>
</select>
<select id="findByParams" parameterClass="map" resultMap="icp.result_base">
<include refid="sql_select"/>
<include refid="sql_where"/> </select>
2、数字范围查询
所传参数名称是捏造所得,非数据库字段,比如_img_size_ge、_img_size_lt字段
 <isNotEmpty prepend="and" property="_img_size_ge">
<![CDATA[
img_size >= #_img_size_ge#
]]>
</isNotEmpty>
<isNotEmpty prepend="and" property="_img_size_lt">
<![CDATA[
img_size < #_img_size_lt#
]]>
</isNotEmpty>

多次使用一个参数也是允许的

 <isNotEmpty prepend="and" property="_now">
<![CDATA[
execplantime >= #_now#
]]>
</isNotEmpty>
<isNotEmpty prepend="and" property="_now">
<![CDATA[
closeplantime <= #_now#
]]>
</isNotEmpty>

3、时间范围查询

 <isNotEmpty prepend="" property="_starttime">
<isNotEmpty prepend="and" property="_endtime">
<![CDATA[
createtime >= #_starttime#
and createtime < #_endtime#
]]>
</isNotEmpty>
</isNotEmpty>

4、in查询

 <isNotEmpty prepend="and" property="_in_state">
state in ('$_in_state$')
</isNotEmpty>

5、like查询

 <isNotEmpty prepend="and" property="chnameone">
(chnameone like '%$chnameone$%' or spellinitial like '%$chnameone$%')
</isNotEmpty>
<isNotEmpty prepend="and" property="chnametwo">
chnametwo like '%$chnametwo$%'
</isNotEmpty>

6、or条件

 <isEqual prepend="and" property="_exeable" compareValue="N">
<![CDATA[
(t.finished='11' or t.failure=3)
]]>
</isEqual> <isEqual prepend="and" property="_exeable" compareValue="Y">
<![CDATA[
t.finished in ('10','19') and t.failure<3
]]>
</isEqual>

7、where子查询

 <isNotEmpty prepend="" property="exprogramcode">
<isNotEmpty prepend="" property="isRational">
<isEqual prepend="and" property="isRational" compareValue="N">
code not in
(select t.contentcode
from cms_ccm_programcontent t
where t.contenttype='MZNRLX_MA'
and t.programcode = #exprogramcode#)
</isEqual>
</isNotEmpty>
</isNotEmpty> <select id="findByProgramcode" parameterClass="string" resultMap="cms_ccm_material.result">
select *
from cms_ccm_material
where code in
(select t.contentcode
from cms_ccm_programcontent t
where t.contenttype = 'MZNRLX_MA'
and programcode = #value#)
order by updatetime desc
</select>

9、函数的使用

 <!-- 添加 -->
<insert id="insert" parameterClass="RuleMaster">
insert into rulemaster(
name,
createtime,
updatetime,
remark
) values (
#name#,
now(),
now(),
#remark#
)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<!-- 更新 -->
<update id="update" parameterClass="RuleMaster">
update rulemaster set
name = #name#,
updatetime = now(),
remark = #remark#
where id = #id#
</update>

10、map结果集

 <!-- 动态条件分页查询 -->
<sql id="sql_count">
select count(a.*)
</sql>
<sql id="sql_select">
select a.id vid,
a.img imgurl,
a.img_s imgfile,
b.vfilename vfilename,
b.name name,
c.id sid,
c.url url,
c.filename filename,
c.status status
</sql>
<sql id="sql_where">
From secfiles c, juji b, videoinfo a
where
a.id = b. videoid
and b.id = c.segmentid
and c.status = 0
order by a.id asc,b.id asc,c.sortnum asc
<dynamic prepend="">
<isNotNull property="_start">
<isNotNull property="_size">
limit #_start#, #_size#
</isNotNull>
</isNotNull>
</dynamic>
</sql>
<!-- 返回没有下载的记录总数 -->
<select id="getUndownFilesForCount" parameterClass="map" resultClass="int">
<include refid="sql_count"/>
<include refid="sql_where"/>
</select>
<!-- 返回没有下载的记录 -->
<select id="getUndownFiles" parameterClass="map" resultClass="java.util.HashMap">
<include refid="sql_select"/>
<include refid="sql_where"/>
</select>

ibatis的动态sql的更多相关文章

  1. IBatis.net动态SQL语句

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...

  2. IBatis.net动态SQL语句(六)

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...

  3. Ibatis.Net 动态SQL语句学习(六)

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数吧. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&qu ...

  4. ibatis 中动态SQL查询和动态标签嵌套的使用

    ibatis 动态查询对于从事 Java EE 的开发人员来说,iBatis 是一个再熟悉不过的持久层框架了,在 Hibernate.JPA 这样的一站式对象 / 关系映射(O/R Mapping)解 ...

  5. 【ibatis】IBatis的动态SQL的写法

    Ⅰ .动态SQL的写法 开始 <dynamic 条件成立时前面要加的字符串 prepend ="字符串"> prepend="字符串" 判断条件的对 ...

  6. 值得注意的ibatis动态sql语法格式

    一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...

  7. 转:ibatis动态sql

    转:ibatis动态sql 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的 IF-ELSE条件语句和一连串 ...

  8. IBATIS动态SQL(转)

    直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...

  9. IBATIS动态SQL

    转自:http://www.cnblogs.com/phoebus0501/archive/2011/05/16/2048126.html 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值 ...

随机推荐

  1. 编译在android 平台上跑的C应用程序

    Android 用的是 Bionic C, 而不是通常的glibc,因此简单使用交叉工具链并不能够编译出适合运行在android 设备上的 C/C++ 程序. 交叉工具链可以很轻松在 Android ...

  2. android全屏和取消全屏 旋转屏幕

    全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 取消全屏 getWindow().clearFlags(Win ...

  3. 常见html标签

    1.flash嵌入标签 <object> <embed allowscriptaccess="always" allowfullscreen="true ...

  4. C# 截取字符串某个字符分割的最后一部分

    例如 string s1="123.456.789",想截取得到的新字符串为“789” 代码如下: string s1 = "123.456.789"; str ...

  5. cookie使用随笔

    cookie是一种浏览器缓存,可以理解为存放在浏览器上的一种小文件. 大小数目限制: 不同浏览器对于大小和数目均有不一样的限制,大小一般为4k,数目从30~50个(视浏览器类型),以键值对的形式存储, ...

  6. Htttp协议

    我 们在浏览器的地址栏里输入的网站地址叫做URL(UniformResourceLocator,统一资源定位符).就像每家每户都有一个门牌地址一样, 每个网页也都有一个Internet地址.当你在浏览 ...

  7. html 背景透明文字不透明

    .alpha{ width: 100px; height: 100px; color: #fff; background: rgba(0, 0, 0, .3); filter: progid:DXIm ...

  8. 错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 C:\Users\Administ

    错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 C:\Users\Administ 这两个fatal error是因为从低版本的WTL到高版本的WTL转变后产生的 ...

  9. xv6的设计trick(不断更新)

    1.每个进程通过时钟中断出发trap.c中的 if(proc && proc->state == RUNNING && tf->trapno == T_IR ...

  10. angular初始用——简易购物车

    <html> <head> <meta charset="utf-8"> <script src="js/angular.js& ...