首页  

mybatis动态更新自动去掉多余逗号     所属分类 mybatis 浏览量 316
mybatis动态更新 set标签 ,自动去掉set语句 尾部 多余的逗号 
推荐使用 trim  
suffixOverrides
prefixOverrides


	<update id="update2" parameterType="demo.data.UserDO">
		UPDATE t_user
		<set>
			updatetime=now(),
			<if test="birth != null">birth = #{birth},</if>
			<if test="height != null">height = #{height},</if>
			<if test="weight != null">weight = #{weight},</if>
		</set>
		WHERE
		id = #{id}
	</update>

	<update id="update3" parameterType="dyyx.data.UserDO">
		UPDATE t_user
		<trim prefix="set" suffixOverrides=",">
			updatetime=now(),
			<if test="birth != null">birth = #{birth},</if>
			<if test="height != null">height = #{height},</if>
			<if test="weight != null">weight = #{weight},</if>
		</trim>
		WHERE
		id = #{id}
	</update>


	
	<select id="query" resultType="dyyx.data.UserDO">
		SELECT * FROM t_user 
		<trim prefix="where" prefixOverrides = "and | or">
		  <if test="weight != null">and weight >= #{weight}  </if>
		  <if test="height != null">and height >= #{height}  </if>
		</trim>
	</select>




height weight 不为 null 的情况  ,动态生成 sql 

update t_user set updatetime=now(), height=?,weight=? where id=?

weight=? 后面的逗号会被自动去掉



例子代码
https://gitee.com/dyyx/springboothello/blob/master/src/main/resources/mapper/UserMapper.xml
https://gitee.com/dyyx/demos/blob/master/mybatis/src/main/resources/mapper/UserMapper.xml

上一篇     下一篇
zookeeper watch 机制

zookeeper Java 客户端

服务注册与发现组件比较

dubbo知识点

lambda使用说明

Redis cluster 原理