mybatis基础之标签

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 表示 该语句以什么结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值