引言(需求)
需求來了,聽好了。
車主故事錄入(車主分享自己的購車經歷),按照長城的需求來說,一個車主只能分享一次故事,如果在進入
個人中心裏,需判斷是否有車主故事了,如果有直接修改,沒有則添加。之前蠢蠢的,做個 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;否則進行插入操作。
這樣就實現了更新和插入的操作了哦~~~很簡單吧~~