(1)实例一: <!-- 动态条件分页查询 --> <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> 说明: 0. 使用<sql id="">,<include refid="">作用:方便一些sql内容,在多个地方重复使用;且使主sql语句比较简洁(缺点:但看上去不一目了然) 同时,可以随便将sql语句中作任一部分抽取出来到sql,在主sql中间调用也没问题。如下: SELECT d.Device_ID,d.Device_Name,a.App_ID,a.App_Name,a.App_Memo FROM T_Device_BaseInfo d ,T_App_Spce_R_Info da ,T_App_Info a WHERE d.Spec_Code=da.Spec_Code AND da.App_ID=a.App_ID 可以改为: <sql id="selectDapermit"> d.Device_Name,a.App_ID,a.App_Name,a.App_Memo FROM T_Device_BaseInfo d , </sql> SELECT d.Device_ID,<include refid="selectDapermit" /> T_App_Spce_R_Info da ,T_App_Info a WHERE d.Spec_Code=da.Spec_Code AND da.App_ID=a.App_ID 1. <dynamic prepend="where"> ..</dynamic>标签,即可此标签中间部分任一个条件为true时,会向当前的sql语句中添加一个"where"的字符. 2. 若只有一个判断条件时,可以直接用: <isNotEmpty prepend="where" property="name"> name like '%$name$%' </isNotEmpty> 3. 模糊查询: 在通常情况下ibatis的参数在sqlmap中使用#param#的形式,参数名以’#‘包着,但当使用模糊查询时,须将#改为$.如上. 4. 设置范围查询时,须用双重判断,又如: <isNotEmpty prepend="" property="_starttime"> <isNotEmpty prepend="and" property="_endtime"> <![CDATA[ createtime >= #_starttime# and createtime < #_endtime# ]]> </isNotEmpty> </isNotEmpty> (2)实例二 <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> 说明: <selectKey>用于iBatis自动生成的主键 很多数据库支持自动生成主键的数据类型。不过这通常(并不总是)是个私有的特性。 SQL Map 通过<insert>的子元素<selectKey>来支持自动生成的键值。它同时支持预生成(如 Oracle)和后生成两种类型(如 MS-SQL Server)。下面是两个例子: < !—Oracle SEQUENCE Example --> <insert id="insertProduct-ORACLE" parameterClass="com.domain.Product"> <selectKey resultClass="int" keyProperty="id" > SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL </selectKey> insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values (#id#,#description#) </insert> <!-- Mysql 这个例子是我自己加上去的--> <insert id="insertProduct-Mysql" parameterClass="com.domain.Product"> insert into PRODUCT(PRD_DESCRIPTION) values (#description#) <selectKey resultClass="int" keyProperty="id"> SELECT LAST_INSERT_ID() </selectKey> </insert>
(3)
动态SQL的参数有
属性关键字
|
含义
|
<isEqual>
|
如果参数相等于值则查询条件有效。
|
<isNotEqual>
|
如果参数不等于值则查询条件有效。
|
<isGreaterThan>
|
如果参数大于值则查询条件有效。
|
<isGreaterEqual>
|
如果参数等于值则查询条件有效。
|
<isLessEqual>
|
如果参数小于值则查询条件有效。如下所示:
<isLessEqual prepend = ”AND” property = ”age” compareValue = ”18” >
ADOLESCENT = ‘TRUE’
</isLessEqual>
|
<isPropertyAvailable>
|
如果参数有使用则查询条件有效。
|
<isNotPropertyAvailable>
|
如果参数没有使用则查询条件有效。
|
<isNull>
|
如果参数为NULL则查询条件有效。
|
<isNotNull>
|
如果参数不为NULL则查询条件有效。
|
<isEmpty>
|
如果参数为空则查询条件有效。
|
<isNotEmpty>
|
如果参数不为空则查询条件有效。参数的数据类型为Collection、String 时参数不为NULL或“”。如下所示:
<isNotEmpty prepend=”AND” property=”firstName” >
FIRST_NAME=#firstName#
</isNotEmpty>
|
<isParameterPresent>
|
如果参数类不为NULL则查询条件有效。
|
<isNotParameterPresent>
|
Checks to see if the parameter object is not present (null). Example Usage:
<isNotParameterPresent prepend=”AND”>
EMPLOYEE_TYPE = ‘DEFAULT’
</isNotParameterPresent>
|
(4)
iterator用法:
Person代码大致如下:
public class Person{
public Person(int age){
this.age=age;
}
/**
* 年龄
*/
private int age;
/**
* 性别
*/
private String sex;
//get/set方法略
...
}//end of Person
public Person(int age){
this.age=age;
}
/**
* 年龄
*/
private int age;
/**
* 性别
*/
private String sex;
//get/set方法略
...
}//end of Person
PersonDaoImp如下:
/**
* 删除性别为man,年龄为 11,12 的Person记录
*/
public int deletePerson(Map<String, Object> map) {
List<Person> personList=new ArrayList<Person>();
Person p1=new Person(11);
person p2=new Person(12);
personList.add(p1);
personList.add(p2);
map.put("personList", personList);
map.put("sex",'man');
return getSqlMapClientTemplate().delete(
"person.deletePerson", map);
}
* 删除性别为man,年龄为 11,12 的Person记录
*/
public int deletePerson(Map<String, Object> map) {
List<Person> personList=new ArrayList<Person>();
Person p1=new Person(11);
person p2=new Person(12);
personList.add(p1);
personList.add(p2);
map.put("personList", personList);
map.put("sex",'man');
return getSqlMapClientTemplate().delete(
"person.deletePerson", map);
}
person.xml如下:
<!-- 删除相应的person记录 -->
<delete id="deletePerson" parameterClass="map">
delete from 表名 where sex=#sex#
<iterate prepend="and" property="personList" open="("
close=")" conjunction="or">
age=$personList[].age$
</iterate>
</delete>
<delete id="deletePerson" parameterClass="map">
delete from 表名 where sex=#sex#
<iterate prepend="and" property="personList" open="("
close=")" conjunction="or">
age=$personList[].age$
</iterate>
</delete>
输出sql如下:
delete from 表名 where sex='man' and (age =11 or age=12)
当然你也可以这么写:
person.xml如下:
delete from 表名 where sex='man' and (age =11 or age=12)
当然你也可以这么写:
person.xml如下:
<!-- 删除相应的person记录 -->
<delete id="deletePerson" parameterClass="map">
delete from 表名 where sex=#sex# and age in
<iterate property="personList" open="("
close=")" conjunction=",">
$personList[].age$
</iterate>
</delete>
<delete id="deletePerson" parameterClass="map">
delete from 表名 where sex=#sex# and age in
<iterate property="personList" open="("
close=")" conjunction=",">
$personList[].age$
</iterate>
</delete>
输出sql如下:
delete from 表名 where sex='man' and age in (11 ,12)
delete from 表名 where sex='man' and age in (11 ,12)
(5) ibatis中,须添加: <![CDATA[ ]]>: 可以用来分隔sql语句出来,以防止与xml中一些语法冲突。如sql中的<,>,<>等符号若直接写在xml中,xml会报错。 若放入CDATA中,则正常。 但其中的内容,不包括<include refid> 或 <isNotNull>等标签。 可以包括a.id=#personId#. 如: <sql id="oraderby"> order by a.name desc </sql> <![CDATA[ select a.name ,b.name from a,b where a.id<>b.id and b.age=#age# ]]> <isNotNull prepend="and" property="name"> a.name=#name# </isNotNull> <include refid="orderby">