首页  

mybatis动态更新自动去掉多余逗号     所属分类 mybatis 浏览量 1331
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=? 后面的逗号会被自动去掉


select * from t_customer <where> <if test="username !=null and username !=''"> and username like concat('%',#{username}, '%') </if> <if test="jobs !=null and jobs !=''"> and jobs= #{jobs} </if> </where> 上述代码中,使用<where>元素对“where 1=1”条件进行了替换,<where>元素会 自动判断组合条件下拼装的SQL语句,动态添加where where之后多余的 AND 或 OR 自动去除
例子代码 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 原理