mybatis常用标签简单介绍(trim,foreach,include,set)
来源:互联网 发布:天下3魍魉捏脸数据 编辑:程序博客网 时间:2024/06/11 20:24
在使用Mybatis时写sql语句是必须的,在写sql语句时如果涉及到根据条件拼接的sql语句的话就要处理wher、and、 in 这样的字符串,而Mybatis中有满足这样的标签可以让我们不用为处理这样的字符串而苦恼。下面收集和总结了一下比较常用的标签:
trim标签
prefix:前缀覆盖并增加其内容 不写的话默认替换为空
suffix:后缀覆盖并增加其内容 不写的话默认替换为空
prefixOverrides:前缀判断的条件
suffixOverrides:后缀判断的条件
一个trim只能去掉一个需要去掉的sql的前缀或者后缀。
<span style="font-size:18px;"><select id="pageListBookByNameAndTypeAndPages" resultMap="pojoBooks"> select * from books where 1=1 and <trim suffixOverrides="and" prefixOverrides="and"> <if test="books!=null"> <if test="books.book_name!=null and books.book_name==''"> and book_name like"%" #{books.book_name} "%" and </if> <if test="books.book_type!=null and books.book_type==''"> and book_type like "%"#{books.book_type} "%" </if> </if> <if test="pages!=null"> and limit #{pages.pageStart},#{pages.pageEnd} and </if> </trim></select></span>
打印出的sql:
SQL: select* from books where 1=1 and book_name like "%" ? "%" and and book_type like "%"? "%" and limit ?,?
可见我们的第一个and和最后一个and被去除了,但是中间的没有被去掉。所以说trim是针对trim标签中的整个sql而言。
我们在看另一个sql语句:
<span style="font-size:18px;"><select id="pageListBookByNameAndTypeAndPages" resultMap="pojoBooks"> select * from books where 1=1 and <trim suffixOverrides="and" prefixOverrides="and"> <if test="books!=null"> <if test="books.book_name!=null and books.book_name==''"> and book_name like"%" #{books.book_name} "%" and </if> <if test="books.book_type!=null and books.book_type==''"> book_type like"%" #{books.book_type} "%" </if> </if> <trim prefixOverrides="and"> <if test="pages!=null"> and limit#{pages.pageStart},#{pages.pageEnd} and </if> </trim> </trim></select></span>
这样得到的结果是:
select *from books where 1=1 and book_name like "%" ? "%" and book_type like "%" ? "%" limit ?,?
可见,一个trim只能去掉trim标签内的一个前缀或者后缀。
Include标签
<includerefid=""></include>是包含一个<sql>的语句片段。
Foreach标签:
用来遍历我们的list或者数组;如:
<span style="font-size:18px;"><delete id="delBooks" > delete from books <where> book_id in <foreach collection="list" item="bookid" open="(" separator="," close=")" > #{bookid} </foreach> </where></delete></span>
对于要指定范围的bookid我们就可以这样达到效果,这里值得注意的是如果没有设置@param那个list或者数组的参数名的话,MyBatis会自动将它包装在一个 Map 中,用名称在作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。index是当前迭代的次数,item的值是本次迭代获取的元素。当使用字典(或者Map.Entry对象的集合)时,index是键,item是值。
Where标签:
Where标签中如果都是if标签那至少需要一个if标签满足才插入where。如果里面有没有被<if>标签包裹的字符串,那这个where就会被拼接到最后的sql语句中;有同学说 如果若最后的内容是“AND”或“OR”开头的,where 标签可以自己将他们去除。我做了一下验证:
<span style="font-size:18px;"> <delete id="delBooks" > delete from books <where> and book_id in <foreach collection="mydelbookids" item="bookid" open="(" separator="," close=")"> #{bookid} </foreach> and </where> </delete></span>打印的sql语句是:
Preparing: delete from books WHERE book_id in ( ? , ? ) and
<delete id="delBooks" > delete from books <where> and book_id=70 and </where> </delete>打印的sql语句是:
delete from books WHERE book_id=70 and
where 能去掉开头的 and 但是不能去除结尾的and (也可能我书写的方式不对)。
<set>标签:
<span style="font-size:18px;"><update id="updateBook"> <if test="books.book_state==0"> update books <set> <if test="books.book_name!=null"> book_name=#{books.book_name}, </if> <if test="books.book_price!=null"> book_price=#{books.book_price}, </if> <if test="books.book_content!=null"> book_content=#{books.book_content}, </if> <if test="books.book_type!=null"> book_type=#{books.book_type}, </if> <if test="books.cover!=null"> book_cover=#{books.cover} </if> </set> </if> where book_id = #{books.book_id}</update></span>
用于update中,满足条件的才更新值。
- mybatis常用标签简单介绍(trim,foreach,include,set)
- Mybatis的<where><foreach><set><trim>等标签详解
- mybatis中<where>标签、<set>标签、<trim>标签、<sql>标签、<foreach>标签的使用
- Mybatis的<where><foreach><set><trim>详解
- Mybatis中的trim标签 介绍
- Mybatis中的三个标签<where> <set> <trim>
- Mybatis动态标签--trim,where,set
- Mybatis <where> <if> <set> <trim> <choose>标签
- Mybatis的动态sql----where,trim,set,foreach
- Mybatis的动态sql----where,trim,set,foreach
- Mybatis的<where><foreach><set>标签详解
- Mybatis的<where><trim><set>标签使用和排重
- Mybatis trim 标签使用
- mybatis trim标签用法
- mybatis <trim>标签
- mybatis trim标签
- Mybatis的<where><foreach><set>等标签详解
- Mybatis的<where><foreach><set>等标签详解
- HBase数据导入方式
- 最近做需求的一点感悟心得
- 子串
- java生成word之poi-tl代码学习
- 《谁说菜鸟不会数据分析》第一章学习记录
- mybatis常用标签简单介绍(trim,foreach,include,set)
- 406. Queue Reconstruction by Height
- 3Sum java leetcode
- Shareplex安装文档
- 快速开发 生成实体类(二)
- ORA Error Solutions——ORA-00020 ORA-00257 ORA-00600 ORA-01012 ORA-01031 ORA-01102
- ActiveMQ 在shareplex8中的使用
- resmgr:cpu quantum Top Event Issues Solved
- Java JDBC连接Oracle11.2.0.2(Eclipse)