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. 最简单的入门github

    下载   git clone ***(地址) 上传  1. 先注册一个github账号 2.在github网站上建设一个工程 3. 把想要上传的数据上传到网站去 1 git config --glob ...

  2. 第一次安装ubuntu要设置的东西

    1. 安装网卡驱动 lscpi 查看网卡型号 根据型号找到驱动源码 下载下来并编译 安装 2. 编译安卓源码的时候出现jdk型号不对的情况 把/usr/bin/java 删除,就可以了.

  3. asp的RegExp对象正则表达式功能用法

    RegExp对象提供简单的正则表达式支持功能. RegExp对象的用法: 以下为引用的内容: Function RegExpTest(patrn, strng) Dim regEx, Match, M ...

  4. 【RMQ】 区间最值查询详解

    1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A ...

  5. Batch Sort

    Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. IT项目各阶段管理

  7. 运行第一个SparkKPI程序

    1.复制一个examples中SparkPi.scala到IntelliJ IDEA编辑器,运行,出现错误: “org.apache.spark.SparkException: A master UR ...

  8. Callable、Future和FutureTask区别

    在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...

  9. CSS3中的skew()属性

    刚开始接触CSS3的2D变换属性,就被这个skew()搞的一头雾水,不知道具体是怎么变化的! 研究了一会才发现,CSS3的斜切坐标系和数学中的坐标系完全不一样(设置斜切原点为左上角) <styl ...

  10. Ubuntu + Django + Nginx + uwsgi

    环境 Ubuntu 14.04 Python 2.7 Django 1.8.4 1 安装Nginx     sudo apt-get install nginx 测试  sudo /etc/init. ...