1 MyBatis3.X实战更新语法之视频更新操作
简介:讲解Mybatis视频更新操作
update 语法更新视频对象
<update id="updateVideo" parameterType="net.xdclass.online_class.domain.Video"> UPDATE video set title = #{title,jdbcType=VARCHAR}, summary = #{summary,jdbcType=VARCHAR}, cover_img = #{coverImg,jdbcType=VARCHAR}, price = #{price,jdbcType=INTEGER}, c_id = #{cId,jdbcType=INTEGER}, point = #{point,jdbcType=INTEGER}, learn_base = #{learnBase,jdbcType=VARCHAR}, learn_result = #{learnResult,jdbcType=VARCHAR}, total_episode = #{totalEpisode,jdbcType=INTEGER}, update_time = now() WHERE id = #{id} </update>
存在其他不想被更新的字段却置为null或者默认值了
2 MyBatis3.X实战 更新语法之选择性更新标签使用
简介:讲解Mybatis 动态字段更新 if test 标签使用
可以选择性更新非空字段
if test标签介绍
if 标签可以通过判断传入的值来确定查询条件,test 指定一个OGNL表达式
常见写法
//当前字段符合条件才更新这个字段的值 <if test='title != null and id == 87 '> title = #{title}, </if> <if test="title!=null"> title = #{title}, </if>
代码(里面包含一个惨痛教训,一定要看pojo类里面的是基本数据类型,还是包装数据类型)
<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>
pojo中point并非包装类型Double,而是double,可使用两种方法,一种是pojo中改变double为Double,另外一种是在 POJO 类中添加一个布尔类型的标志位,用于指示是否要新
point
字段。
问题:
原因是where语句写在了trim里面,where id = #{id}
这部分写在了 trim
标签内部,这会导致语法错误。WHERE
子句不属于 SET
子句的一部分,应该放在 trim
标签外部。
3 MyBatis3.X实战之删除语法和转义字符使用
简介:讲解Mybatis删除语法操作
delete删除语法
需求:删除某个时间段之后 且金额大于 10元的数据
<delete id="deleteByCreateTimeAndPrice" parameterType="java.util.Map"> delete from video where create_time <![CDATA[ > ]]> #{createTime} and price <![CDATA[ >= ]]> #{price} </delete>
为什么要转义字符:
由于MyBatis的sql写在XML里面, 有些sql的语法符号和xml里面的冲突
大于等于 <![CDATA[ >= ]]>
小于等于 <![CDATA[ <= ]]>