Mybatis的SaveOrUpdate

批量的saveOrupdate:


  
    insert into xxxTable (
       merchant_id, app_id, department_id,
      `name`, mobile, token,
      created_at, updated_at)
    values
    
      (
      #{merchantId,jdbcType=INTEGER},#{appId,jdbcType=INTEGER},#{staff.departmentId},
      #{staff.name},#{staff.mobile},#{staff.token},
      NOW(),NOW()
      )
    
    on duplicate key update department_id = values(department_id),updated_at = values(updated_at)
  

 

1.根据selectkey判断查询的count值是否为1,然后再进行新增或更新

 
        
            select count(*) as count from `${tableName}` where userid = #{user.userid}
        
        
        
            UPDATE  `${tableName}`
            
                
                    `app_id` = #{user.appId},
                
                
                    `yunid` = #{user.yunid},
                
                
                    `qr_scene` = #{user.qr_scene},
                
                
                    `openid` = #{user.openid},
                
                
                    `qr_scene_str` = #{user.qr_scene_str},
                
                
                    `nickname` = #{user.nickname},
                
                
                    `language` = #{user.language},
                
                
                    `city` = #{user.city},
                
                
                    `country` = #{user.country},
                
                
                    `remark` = #{user.remark},
                
                
                    `headimgurl` = #{user.headimgurl},
                
                
                    `province` = #{user.province},
                
                
                    `tagid_list` = #{user.tagIdList},
                
                
                    `subscribe_scene` = #{user.subscribe_scene},
                
                
                    `unionid` = #{user.unionid},
                
                
                    `subscribe` = #{user.subscribe},
                
                
                    `groupid` = #{user.groupid},
                
                
                    `subscribe_time` = #{user.subTime},
                
                
                    `sex` = #{user.sexStr},
                
                
                    `updated_at` = #{user.updatedAt},
                
            
            WHERE userid = #{user.userid}
        
        
        
            INSERT IGNORE INTO `${tableName}`
            (
            
            )
            values
            (
            #{user.appId},#{user.yunid},#{user.userid},#{user.point},#{user.qr_scene},
            #{user.openid},#{user.qr_scene_str},#{user.nickname},#{user.language},#{user.city},
            #{user.country},#{user.remark},#{user.headimgurl},#{user.province},#{user.tagIdList},
            #{user.subscribe_scene},#{user.unionid},#{user.subscribe},#{user.groupid},#{user.subTime},
            #{user.sexStr},#{user.createdAt},#{user.updatedAt}
            )
        
    

2.根据相应的唯一主键来判断是否新增或更新  [对事务支持较好]

参考sql:

INSERT INTO 
user(userid,sex,age) 
VALUES('oCCtTxOz28457LUISKyOq4r94DYE','男',18) 
ON DUPLICATE KEY UPDATE sex=VALUES(sex),age=VALUES(age)

 

 

你可能感兴趣的:(mybatis)