<update id="insertOrUpdate" parameterType="TtToken" > <selectKey keyProperty="count" resultType="int" order="BEFORE"> select count(*) from tt_token where user_id = #{userId} </selectKey> <if test="count > 0"> update tt_token <trim prefix="SET" suffixOverrides=","> <if test="expireTime != null ">expire_time = #{expireTime},</if> <if test="updateTime != null ">update_time = #{updateTime},</if> </trim> where user_id = #{userId} </if> <if test="count==0"> insert into tt_token <trim prefix="(" suffix=")" suffixOverrides=","> <if test="userId != null ">user_id,</if> <if test="token != null and token != '' ">token,</if> <if test="expireTime != null ">expire_time,</if> <if test="updateTime != null ">update_time,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="userId != null ">#{userId},</if> <if test="token != null and token != '' ">#{token},</if> <if test="expireTime != null ">#{expireTime},</if> <if test="updateTime != null ">#{updateTime},</if> </trim> </if> </update>