更新语法
<update id="updateVideo" parameterType="net.xdclass.online_class.domain.Video">
update video
set
title = #{title,jdbcType=VARCHAR},
point = #{point,jdbcType=DOUBLE}
where
id = #{id}
</update>
存在其他没有重新设置更新的字段,被置为null或者默认值了
动态SQL更新if test标签使用
可以选择性更新非空字段
if标签可以通过判断传入的值来确定查询条件,test指定⼀个OGNL表达式
<update id="updateVideoSelective" parameterType="net.xdclass.online_class.domain.Video">
update video
<trim prefix="set" suffixOverrides=",">
<if test="title != null "> title = #{title,jdbcType=VARCHAR},</if>
<if test="summary != null "> summary = #{summary,jdbcType=VARCHAR},</if>
<if test="coverImg != null "> cover_img = #{coverImg,jdbcType=VARCHAR},</if>
<if test="price != 0 "> price = #{price,jdbcType=INTEGER},</if>
<if test="createTime !=null "> create_time = #{createTime,jdbcType=TIMESTAMP},</if>
<!--一定要看pojo类里面的是基本数据类型,还是包装数据类型-->
<if test="point != null "> point = #{point,jdbcType=DOUBLE},</if>
</trim>
where
id = #{id}
</update>
注意:
1、关于包装数据类型
2、标签中的每段语句都要跟逗号
删除语法
删除某个时间段之后且金额⼤于10元的数据
<delete id="deleteByCreateTimeAndPrice" parameterType="java.util.Map">
delete from video where create_time <![CDATA[ > ]]> #{createTime}
and price <![CDATA[ >= ]]> #{price}
</delete>
转义字符
由于MyBatis的sql写在XML里⾯,所以无法直接使用"<“或者”>"符号,会跟标签冲突
因此使用<![CDATA[ 字符 ]]>
解决冲突
动态SQL
if
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG
WHERE state = 'ACTIVE'
<if test="title != null">
AND title like #{title}
</if>
</select>
where
替换where关键字,会动态的去掉第一个条件前的 and,如果所有的参数没有值则不加where关键字
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = 'ACTIVE'
</if>
<if test="title != null">
AND title like #{title}
</if>
</where>
</select>
choose、when、otherwise
传入了 “title” 就按 “title” 查找,传入了 “author” 就按 “author” 查找的情形。若两者都没有传入,就返回标记为 featured 的 BLOG
<select id="findActiveBlogLike"
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 author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
trim
移除所有 prefixOverrides 属性中指定的内容,并且插入 prefix 属性中指定的内容。
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
set
set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。set元素会动态地在行首插入set关键字,并会删掉额外的逗号。
<update id="updateAuthorIfNecessary">
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
用来迭代任何可迭代的对象(如数组,集合)。
collection 属性: mybatis会将数组参数,封装为一个Map集合。(默认:array == 数组;使用@Param注解改变map集合的默认key的名称)
item 属性: 本次迭代获取到的元素。
separator 属性: 集合项迭代之间的分隔符。foreach 标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。
open 属性: 该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次。
close 属性: 该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次。
<delete id="deleteByIds">
delete from tb_brand where id
in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>