引言(需求)
需求来了,听好了。
车主故事录入(车主分享自己的购车经历),按照长城的需求来说,一个车主只能分享一次故事,如果在进入
个人中心里,需判断是否有车主故事了,如果有直接修改,没有则添加。之前蠢蠢的,做个 if 判断嘛,有记录就修
改,没有记录就添加,但是一整理的时候发现重复代码太多。后来想到咱们用Hibernate的时候不是有个方法交
UpdateorSave吗,网上一查,mybatis果然可以实现!
直接上代码!
- <!-- 车主故事录入 -->
- <insert id="saveCarStory" useGeneratedKeys="true" keyProperty="memberOwnerStoryId" parameterType="com.ec.model.carStory.MemberOwnerStory">
- <!-- 查看是否存在memberid,如果存在及更新,否则插入 -->
- <selectKey keyProperty="count" resultType="int" order="BEFORE">
- select count(*) as count from member_owner_story where MEMBER_ID = #{memberId,jdbcType=INTEGER}
- </selectKey>
- <!-- 如果大于0则更新 -->
- <if test="count>0">
- update member_owner_story set
- STORY_TOPIC=#{storyTopic,jdbcType=VARCHAR},
- STORY_CONTENT=#{storyContent,jdbcType=VARCHAR},
- ADD_TIME=#{addTime,jdbcType=VARCHAR},
- SHOW_CARTYPE=#{showCartype,jdbcType=VARCHAR},
- SAVE_MODE=#{saveMode,jdbcType=VARCHAR}
- where MEMBER_ID = #{memberId,jdbcType=INTEGER}
- </if>
- <!-- 如果等于0则保存 -->
- <if test="count==0">
- insert into member_owner_story
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="memberId != null ">MEMBER_ID,</if>
- <if test="productId != null ">PRODUCT_ID,</if>
- <if test="catalogId != null ">CATALOG_ID,</if>
- <if test="storyTopic != null ">STORY_TOPIC,</if>
- <if test="storyContent != null ">STORY_CONTENT,</if>
- <if test="showTime != null ">SHOW_TIME,</if>
- <if test="isAudit != null ">IS_AUDIT,</if>
- <if test="auditUserId != null ">AUDIT_USER_ID,</if>
- <if test="auditTime != null ">AUDIT_TIME,</if>
- <if test="isDelete != null ">IS_DELETE,</if>
- <if test="addUserId != null ">ADD_USER_ID,</if>
- <if test="addTime != null ">ADD_TIME,</if>
- <if test="editUserId != null ">EDIT_USER_ID,</if>
- <if test="editTime != null ">EDIT_TIME,</if>
- <if test="noAuditReason != null ">NO_AUDIT_REASON,</if>
- <if test="praiseNum != null ">PRAISE_NUM,</if>
- <if test="showCartype != null ">SHOW_CARTYPE,</if>
- <if test="saveMode != null ">SAVE_MODE,</if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="memberId != null ">#{memberId,jdbcType=INTEGER},</if>
- <if test="productId != null ">#{productId,jdbcType=INTEGER},</if>
- <if test="catalogId != null ">#{catalogId,jdbcType=INTEGER},</if>
- <if test="storyTopic != null ">#{storyTopic,jdbcType=VARCHAR},</if>
- <if test="storyContent != null ">#{storyContent,jdbcType=VARCHAR},</if>
- <if test="showTime != null ">#{showTime,jdbcType=VARCHAR},</if>
- <if test="isAudit != null ">#{isAudit,jdbcType=VARCHAR},</if>
- <if test="auditUserId != null ">#{auditUserId,jdbcType=VARCHAR},</if>
- <if test="auditTime != null ">#{auditTime,jdbcType=VARCHAR},</if>
- <if test="isDelete != null ">#{isDelete,jdbcType=VARCHAR},</if>
- <if test="addUserId != null ">#{addUserId,jdbcType=VARCHAR},</if>
- <if test="addTime != null ">#{addTime,jdbcType=VARCHAR},</if>
- <if test="editUserId != null ">#{editUserId,jdbcType=VARCHAR},</if>
- <if test="editTime != null ">#{editTime,jdbcType=VARCHAR},</if>
- <if test="noAuditReason != null ">#{noAuditReason,jdbcType=VARCHAR},</if>
- <if test="praiseNum != null ">#{praiseNum,jdbcType=VARCHAR},</if>
- <if test="showCartype != null ">#{showCartype,jdbcType=VARCHAR},</if>
- <if test="saveMode != null ">#{saveMode,jdbcType=VARCHAR},</if>
- </trim>
- </if>
- </insert>
先根据memberId判断是否有车主故事的相关记录,返回count,如果有数据(count>0),则说明车主之前录入过
车主故事,所以直接进行更新就Ok;否则进行插入操作。
这样就实现了更新和插入的操作了哦~~~很简单吧~~