1。<if test=“属性”>用于判断
<select>select * from students where <if test="name!=null and name !=''"> name like concat(concat('%',#{name}),'%')</if><if test="sex!=null and sex!=''">
and sex=#{sex,jdbc=INTGER}</if>
</select>
说明:contact(str1,str2)是连接里面的参数构成 一个字符串.若里面有个参数为null 则返回的是null
2.<where><if>搭配
<select>select * from students <where> <if test="name!=null and name !=''"> name like concat(concat('%',#{name}),'%')</if><if test="sex!=null and sex!=''">
and sex=#{sex,jdbc=INTGER}</if></where>
</select>
这个搭配的好处:在<if test> 如果name==null sex不为null,则变为where and sex。而<where><if>则可以避免这个.如果不想用这个的话,还有一种方法。就是 where 1=1 <if>..
这样就会变成where 1=1 and sex
3.<set><if>用于更新
set标签用于防止多余逗号出现。即update student set age=13,where
<update> update student <set> <if test="name!=null and name!=''">name=#{Name},</if><if test="age!=null">age=#{age}</set>
where id=#{ID} </update>
4.<trim>代替where/set
http://www.cnblogs.com/qiankun-site/p/5758924.html这个博客说的很清楚
5. <choose> <when> <otherwise >类似switch case default
<select>select * from student <where><choose> <when test="name!=null"> name=${Name}</when><when test="age!=null"> age=#{Age}</when>
<otherwise></otherwise></choose></where></select>
choose-----switch
when: name----case
when: age----case
otherwise:----default
6.<sql>标签 用来定义一个可以重复用的sql语句段。其他语句段可以通过这个标签的id来引用
<sql id="selectAll"> select * from student ST</sql>
<select id="getStudent" parameterType="string" result="studentResultMapping"> <include refid="selectAll"/> where ST.id=#{ID}</select>
7.<foreach>批量操作。 增删改查 的方法是一样的
以 修改 update为例
<update id="update" parameterType="Java.util.Map"> update student set name=#{Name} where id in<foreach collection="list" index="index" Item="item"
open="(" separator="," close=")"> #{item} </foreach></update>
index 表示每次迭代的位置。
open表示该语句以什么 开始
separator 表示每次迭代之间以什么符号作为分隔符
close 表示 该语句以什么结束