ibatis的动态sql
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.列子
1、动态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>
<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的更多相关文章
- IBatis.net动态SQL语句
在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...
- IBatis.net动态SQL语句(六)
在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...
- Ibatis.Net 动态SQL语句学习(六)
在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数吧. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&qu ...
- ibatis 中动态SQL查询和动态标签嵌套的使用
ibatis 动态查询对于从事 Java EE 的开发人员来说,iBatis 是一个再熟悉不过的持久层框架了,在 Hibernate.JPA 这样的一站式对象 / 关系映射(O/R Mapping)解 ...
- 【ibatis】IBatis的动态SQL的写法
Ⅰ .动态SQL的写法 开始 <dynamic 条件成立时前面要加的字符串 prepend ="字符串"> prepend="字符串" 判断条件的对 ...
- 值得注意的ibatis动态sql语法格式
一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...
- 转:ibatis动态sql
转:ibatis动态sql 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的 IF-ELSE条件语句和一连串 ...
- IBATIS动态SQL(转)
直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...
- IBATIS动态SQL
转自:http://www.cnblogs.com/phoebus0501/archive/2011/05/16/2048126.html 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值 ...
随机推荐
- Java获取来访者IP
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- php array_walk_recursive函数的使用
<?phpfunction myfunction($value,$key) {echo "The key $key has the value $value<br />&q ...
- [转]动态添加Fragments
本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...
- 简易控制中心,angular的简单使用
<html> <head> <meta charset='utf-8'> <script src="js/angular.js">& ...
- java 类与对象
class XiyoujiRenwu { float height,weight; String head, ear; void speak(String s) { System.out.printl ...
- My workbench draft
System Linux / Ubuntu 14.04.5 LTS (Trusty Tahr) + ROS Indigo Linux / Ubuntu 16.04.1 LTS (Xenial Xeru ...
- Zencart批量删除无图片产品
Zencart批量删除无图片产品 2012-04-23 07:26:18| 分类: 默认分类 |字号 订阅 转自 http://zhongjia33.blog.163.com/blog/#m=0 ...
- Android Audio Focus的应用(requestAudioFocus)
网址:http://blog.csdn.net/dadoneo/article/details/8252933 FROM: http://www.linuxidc.com/Linux/2012-04/ ...
- ida dump内存脚本
auto i,fp; fp = fopen("d:\\dump.dex","wb"); for(i=r0;i<r0+0x20;i++) fputc(Byt ...
- Codevs 5590 A+B 问题 超级版
5590 A+B 问题 超级版 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 青铜 Bronze 题目描述 Description 不用+-*/%计算A+B 输入描述 Input De ...