标签:解释 and 补充 eve 等等 span update 个人 int
简单介绍:翻看以前在学校写的代码,发现那时候有一个sql写的很有意思,用到了 <set>标签,和我现在写的虽然有点差别,但是效果一样
代码:
<update id="updateEvent" parameterType="map">
update event
<set>
<if test="title!=null and title!=‘‘">
title=#{title},
</if>
<if test="event!=null and event!=‘‘">
event=#{event}
</if>
</set>
where id=#{id}
</update>
//mapper里的sql
<update id="updateEvent" parameterType="map">
update event
<trim prefix="set" suffixOverrides=",">
<if test="title!=null and title!=‘‘">
title=#{title},
</if>
<if test="event!=null and event!=‘‘">
event=#{event},
</if>
</trim>
where id=#{id}
</update>
解释属性,顺便再补充几个常用的属性:
//insert sql
<insert id="insert" parameterType="Contract">
insert into contract
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="userId != null">user_id,</if>
<if test="createDate != null">create_date,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id,jdbcType=BIGINT},</if>
<if test="userId != null">#{userId,jdbcType=BIGINT},</if>
<if test="createTime != null">#{createDate,jdbcType=TIMESTAMP},</if>
</trim>
</insert>
//update sql
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="gender != null and gender.length()>0"> gender=#{gender} , </if>
</trim>
总结:我个人感觉来说,这两种写法花费的时间都差不多,但是第二种的能够处理不小心添加的逗号或者其他,规范性上感觉更加好一点吧。
标签:解释 and 补充 eve 等等 span update 个人 int
原文地址:https://www.cnblogs.com/xuchao0506/p/9970950.html