mybatis实战之动态sql标签学习
2017-08-29 23:29
483 查看
Mybatis实现动态SQL,主要通过以下标签:if,where,choose(when,otherwise),trim,set,foreach。接下来,我将逐一来说明各标签的用法。有错误的地方麻烦指正~
这个一般用于动态选项的查询,即多值结合查询,选项值有可能为空,因此需要用到动态if标签来判断多个值存在与否。
例1:通过年龄和居住地查询用户表。
注意:上面“1=1”始终为true,所以后边可以直接接上and 语句,不然就会出现“where and age=age”这样的错误。
例2:例1的升级版。
这里就不需要再补充“1=1”的语句了,这是因为where标签会自动帮你处理,如果where后边是and,or开头的会被自动忽略掉。如果使用了and,or的话,mybatis也会自动为你在前后补充空格,妈妈就再也不用担心出现“ageandpassword”的错误出现~
例3:根据年龄和居住地查找表
这里与例1和例2不同的是,这里不会每一条都执行,类似于switch case语句。从上往下执行,当when中有条件满足的时候,就会跳出choose。when中条件都不执行则会执行otherwise语句。即最终choose里面仅有一条语句执行。
补充:set标签会自动忽略不必要的后缀逗号。
时常用于sql的批量操作,如批量删除,批量添加数据等。
例子:批量添加用户
与之相应的sql语句为:insert into admin (…) values(…);。foreach就将对象一个个存入数据库中。
例子:批量删除用户
相应的语句为:delete from admin where aid into(…);。foreach会将数组转为(aid,aid,aid)的形式,主要看foreach的后三个属性就知道啦
例:
充当where标签:
充当set标签:
例子:动态添加用户属性
上面相应的语句为:insert into admin (…) values(…);。通过trim标签用()包裹,以及自动忽略尾部的逗号。
trim标签的可用性十分广阔,还可以在实战中慢慢挖掘~
if标签
就是简单的条件判断,利用if语句我们可以实现某些简单的条件选择。这个一般用于动态选项的查询,即多值结合查询,选项值有可能为空,因此需要用到动态if标签来判断多个值存在与否。
例1:通过年龄和居住地查询用户表。
<select id="find" parameterType="Admin" resultType="Admin"> select * from admin where 1=1 <if test="age!= null and age!='' "> and age=age </if> <if test="city!= null and city!='' "> and city=city </if> </select>
注意:上面“1=1”始终为true,所以后边可以直接接上and 语句,不然就会出现“where and age=age”这样的错误。
where标签
补充相应的where的sql语句,解决了if标签在上面所容易造成错误的问题。更好地简化了where的条件判断。例2:例1的升级版。
<select id="find" parameterType="Admin" resultType="Admin"> select * from admin <where> <if test="age!= null and age!='' "> and age=age </if> <if test="city!= null and city!='' "> and city=city </if> </where> </select>
这里就不需要再补充“1=1”的语句了,这是因为where标签会自动帮你处理,如果where后边是and,or开头的会被自动忽略掉。如果使用了and,or的话,mybatis也会自动为你在前后补充空格,妈妈就再也不用担心出现“ageandpassword”的错误出现~
choose标签
相当于switch语句,通常与when和otherwise搭配使用。例3:根据年龄和居住地查找表
<select id="find" parameterType="Admin" resultType="Admin"> select * from admin where 1=1 <choose> <when test="age!= null and age!=''"> and age=#{age} </when> <when test="city!= nulland city!=''"> and city=#{city} </when> <otherwise> and aname=#{aname} </otherwise> <choose> <select>
这里与例1和例2不同的是,这里不会每一条都执行,类似于switch case语句。从上往下执行,当when中有条件满足的时候,就会跳出choose。when中条件都不执行则会执行otherwise语句。即最终choose里面仅有一条语句执行。
set标签
类似于where功能,主要用于sql赋值更新操作,聪明的你一个可以领悟得到啦~<update id ="update" parameterType="Admin"> update admin <set> <if test="aname !=null and aname !='' "> aname=#{aname}, </if> <if test="age!= null and age!='' "> age=#{age}, </if> <if test="city!= null and city!='' "> password=#{password}, </if> </set> where aid=#{aid} </update>
补充:set标签会自动忽略不必要的后缀逗号。
foreach标签
类似于for循环,循环的对象可以是list和数组。时常用于sql的批量操作,如批量删除,批量添加数据等。
例子:批量添加用户
<insert id="addAdmin" > insert into amdin (aid,aname,pwd,city,address) values <foreach collection="list" item="item" index="index" separator=","> (#{item.aid},#{item.aname},#{item.pwd},#{item.city},#{item.address) </foreach> </insert>
与之相应的sql语句为:insert into admin (…) values(…);。foreach就将对象一个个存入数据库中。
例子:批量删除用户
<delete id="delAdmin"> delete from admin where aid in <foreach collection="array" item="aid" open="(" separator="," close=")"> #{aid} </foreach> </delete>
相应的语句为:delete from admin where aid into(…);。foreach会将数组转为(aid,aid,aid)的形式,主要看foreach的后三个属性就知道啦
trim标签
十分强大!可以自定义添加前后缀,与之对应的属性是prefix和suffix。同时通过prefixOverrides和suffixOverrides分别来覆盖首尾部的内容,即忽略不必要的前后缀。就是说它可以充当where标签,也可以充当set标签啦~例:
充当where标签:
<trim prefix = "where" prefixOverrides="and|or" > ... </trim>
充当set标签:
<trim prefix="set" suffixOverrides=","> ... </trim>
例子:动态添加用户属性
<insert id="find" resultType="Admin"> insert into admin <trim prefix="(" suffix=")" suffixOverrides=","> <if test = "aname != null and aname !='' "> aname, </if> <if test = "city != null and city !='' "> city, </if> <if test = "age != null and age !='' "> age, </if> </trim> <trim prefix="values(" suffix=")" suffixOverrides=","> <if test = "aname != null and aname !='' "> #{aname}, </if> <if test = "city != null and city !='' "> #{city}, </if> <if test = "age != null and age !='' "> #{age}, </if> </trim> </insert>
上面相应的语句为:insert into admin (…) values(…);。通过trim标签用()包裹,以及自动忽略尾部的逗号。
trim标签的可用性十分广阔,还可以在实战中慢慢挖掘~
相关文章推荐
- MyBatis学习之动态SQL标签
- mybatis学习--动态sql之foreach标签
- 框架学习系列 mybatis 第十五篇 mybatis动态sql及常用标签二
- mybatis动态sql中的trim标签的使用
- Mybatis学习总结之动态SQL与模糊查询
- Mybatis学习总结(五).动态SQL与Mybatis缓存
- mybatis动态SQL标签的用法
- mybatis 学习 三、动态SQL语句
- mybatis动态SQL标签的用法
- MyBatis框架学习(五)-动态sql
- MyBatis学习笔记(六)动态sql
- myBatis学习笔记(6)——动态SQL & 模糊查询
- mybatis动态sql中的trim标签的使用
- MyBatis学习 之 三、动态SQL语句
- Mybatis学习笔记--(二)动态SQL
- mybatis动态sql中的trim标签的使用
- MyBatis学习 之四、动态SQL语句
- mybatis中动态sql标签之if trim
- mybatis学习之路----动态sql之foreach set
- MyBatis动态SQL中trim标签的使用