可以选择性更新非空字段
if test标签介绍
if标签可以通过判断传入的值来确定查询的条件,test指定一个OGNL表达式
常见写法
// A code block
// An highlighted block
<if test='title != null and id == 87 '> title = #{title}, </if>
<if test="title!=null"> title = #{title}, </if>
代码:
下面展示一些 代码片
。
// A code block
一定要看pojo类里面的是基本数据类型(int short byte long等),还是包装数据类型(String,DOUBLE等)
// 注意驼峰字段 coverImg:pojo对象字段 cover_img: 数据库字段
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 != 0"> point = #{point,jdbcType=DOUBLE},</if>
</trim>
where
id = #{id}
为什么要转义字符
由于Mybatis的sql 写在XML里面,有些sql的语法符号和xml里面的冲突
大于等于 <![CDATA[ >= ]]>
小于等于 <![CDATA[ <= ]]>