SSM项目从零开始到入门014-mybatis的update介绍

update

update元素

 1. id :   与 Mapper接口对应的方法名称
 2. parameterType  :  传入的参数格式
 3. flushCache :   是否清空缓存,默认为false
 4. statementType :  PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。

 5. timeout :  等待数据库返回请求结果的秒数


1. 一般的update操作——返回值为更新的记录数目

mapper接口代码

/** 
 * 更新用户信息 
 * @param user 
 * @return 成功操作的记录数目 
 */  
int update(User user); 

mapper.xml:

<update id = "update" parameterType = "User">
      UPDATE USER SET user_name = #{userName}, 
         pass_word = #{passWord}, 
      WHERE ID = #{id};
</update>

2.update动态SQL语句

涉及到更新操作时,可能不需要对所有字段更新,这时不需要更新的字段需要保持原字段信息,因为不更新的字段,会被传递null到SQL中,引起异常。这时就需要进行动态SQL拼接,如下,使用trim就是为了删掉最后字段的“,”。
主要不用单独写SET了,因为set被包含在trim中了:

<update id="update"  parameterType="user">
 UPDATE user
 <trim prefix="set" suffixOverrides=",">
  <if test="userName!=null">user_name=#{userName},</if>
  <if test="passWord!=null">pass_word=#{passWord},</if>
 </trim>
 WHERE id=#{id}
</update>

3. 更新过后得到更新的记录
平常我门都是更新数据,用更新的条件再查询一次,得到更新的记录。这样我门就进行了两次数据库操作,链接了两次数据库。增加了接口的处理事件,因为链接数据库是很耗时的操作。其实可以通过 mybatis 的 selectKey 标签来解决这个问题。 
<update id="updateByUserName" parameterType="User">
      <selectKey keyProperty='id' resultType='int' order='BEFORE'>
          SELECT
            (select id FROM user WHERE
             user_name = #{userName}
            )id
            from DUAL
      </selectKey>
        UPDATE user SET
            pass_word=#{passWord}
        WHERE
            user_name = #{userName}
  </update>
4.批量修改

mapper接口代码
/** 
 * 批量更新用户信息 
 * @param user 
 * @return 成功操作的记录数目 
 */  
int update(List<User> list); 

mapper.xml:

<update id="batchUpdate" parameterType="java.util.List">
        update user
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="user_name=case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.userName!=null">
                        when id=#{i.id}  then  #{i.userName}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" pass_word=case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.passWord!=null">
                        when id=#{i.id} then  #{i.passWord}
                    </if>
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
           #{item.id}
        </foreach>
 </update>

注意:如果update的值是相同的话,很简单,组织  update table set column='...' where id in (1,2,3) 类似就可以
<update id="batchUpdate" parameterType="java.util.Map" >
    UPDATE USER SET status = #{status} WHERE id IN
    <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</update>
当值不同时,为了提高效率一般采用用case when来拼成一长串sql处理。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心歌技术

打赏不能超过你的早餐钱!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值