if:条件判断语句,单条件
choose(when,otherwise) 相当于case when
trim(set,where):辅助语句,主要用来处理一些sql拼装
foreach:循环语句
if条件语句
<select id=”findActiveBlogWithTitleLike”parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test=”title != null”>
AND title like #{title}
</if>
</select>
如果满足if条件,则会拼接对应的sql
<select id=”findActiveBlogLike”
parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test=”title != null”>
AND title like #{title}
</when>
<when test=”author != null and author.name != null”>
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
where条件
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
<where>
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</where>
</select>
这两者效果一样。
在使用if条件的时候,如果使用where条件,如果前面没有条件,我们可能需要使用where 1=1
然后在if中使用and xxx
这种格式,但是使用<where>
条件的时候不用,mybatis默认会给<where>
条件增加where的sql,同时条件的第一个and语句会自动去掉。这样就不用被动的写 where 1=1
这种了
trim主要是用来去除某些SQL语法的时候使用,比如上面的<where>
条件
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
< trim prefix= "WHERE" prefixOverrides= "AND |OR " >
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</ trim >
</select>
如果当state为空的时候,正常情况下sql为:
SELECT * FROM BLOG where AND title like #{title} AND title like #{author.name}
但是trim会把where前面的AND或者OR关键字去除,变为:
SELECT * FROM BLOG where title like #{title} AND title like #{author.name}
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="gender != null and gender.length()>0"> gender=#{gender} , </if>
</trim>
如说name和gender的值都不为null的话打印的SQL为:update user set name=‘xx’ , gender=‘xx’ where id=‘x’
在where前面不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
suffix:后缀
set元素主要是用来解决当满足一个条件时,需要用where语句,而不满足时,不需要
在update中类似的,用set元素:
<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
foreach元素:
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT * FROM POST P WHERE ID in
<foreach " item="item" " index="index" collection="list" open="(" " separator="," close=")">
#{item}
</foreach>
</select>
open和close表示用什么符号把元素包装起来,separator是各个元素的间隔符
item是循环中当前的元素
bind元素
bind元素听过OGNL表达式定义一个上下文的变量:
<select id=”findActiveBlogWithTitleLike”parameterType=”Blog” resultType=”Blog”>
<bind name="pattern_title" value="'%'+title+'%'" />
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
and title like #{pattern_title}
</select>