码迷,mamicode.com
首页 > 数据库 > 详细

mybatis动态sql (二)

时间:2017-02-17 23:06:08      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:批量   update   dex   循环   cti   元素   rri   标记   智能   

1、<if> (单个条件)

 

2、<choose> <when> (多个分支条件)

 

3、<where>用于智能的处理where条件,可以智能地加上和去掉条件中的and

<select>

select * from user

<where>

<if test="userName != null">

and userName like  #{userName}

</if>

<if test="id!= null">

and id= #{id}

</if>

</where>

</select>

 

4、<set>处理update操作,可以智能地去掉或加上set里面的逗号

<update>

update user

<set>

<if test="userName != null"> userName=#{userName},</if>

<if test="password!= null"> password=#{password},</if>

</set>

</update>

 

5、<trim>可以自动为sql添加前缀和后缀,消减前缀字符或后缀字符

<update>

update user

<trim prefix="set" suffixOverride="," suffix="where id=#{id}">

<if test="userName!=null and userName!=‘‘ ">

userName=#{userName},

</if>

<if test="password!=null and password!=‘‘ ">

password=#{password},

</if>

</trim>

</update>

 

6、foreach循环标记,主要用于循环查询条件,循环更新

循环查询条件:

<select>

select * from user

<where>

id in 

<foreach item="item" index="index" collection="list" open="(" separator="," close=")">

#{item}

</foreach>

</where>

</select>

批量赋值:

<insert>

insert into user(username,password)

values

<foreach item="item" index="index" collection="list" open="" close="" separator=",">

(#{item.userName},#{item.password})

<foreach>

</insert>

解释:

item循环到的某个元素

index循环的索引

collection循环的集合

open循环的开始符号

close循环的结束符号

mybatis动态sql (二)

标签:批量   update   dex   循环   cti   元素   rri   标记   智能   

原文地址:http://www.cnblogs.com/hy87/p/6411553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!