批量插入或更新数据(MyBatis-plus框架)

2023-05-16

目录

1.场景说明

2.DUPLICATE 和REPLACE比较

3.批量插入或者更新(两种方式)

方式一:mybatis-plus的saveOrUpdateBatch方法

问题:如果操作类集成了基础类,比如封装了BaseEntity去集成,那么这样使用会出问题

方式二:on duplicate key (推荐)

4.注意

5.常见问题 


1.场景说明

插入数据时,我们经常会遇到这样的情况:

1、首先判断数据是否存在;

2、如果不存在,则插入;

3、如果存在,则更新

需求:根据表中的部分字段去判断插入或者更新

有一张表 hh_adx_monitor_summary

 ddl:

CREATE TABLE `hh_adx_monitor_summary` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` varchar(6) DEFAULT NULL COMMENT '链路编码',
  `plan_id` varchar(32) DEFAULT NULL COMMENT '计划id',
  `cons` int(11) DEFAULT NULL COMMENT '消耗',
  `exp` int(11) DEFAULT NULL COMMENT '曝光数',
  `conv` int(11) DEFAULT NULL COMMENT '转化数',
  `click` int(11) DEFAULT NULL COMMENT '点击数',
  `dimension_time` varchar(32) DEFAULT NULL COMMENT '维度时间',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_hh_adx_monitor_summary_cpd` (`code`,`plan_id`,`dimension_time`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8 COMMENT='监测请求数据汇总';

需要通过code,plan_id,dimension_time判断插入或者更新

INSERT INTO hh_adx_monitor_summary ( CODE, plan_id, cons, exp, conv, click, dimension_time)
VALUES
	( '001001', '1', 6, 3, 0, 0, '20220823' ) 
	ON DUPLICATE KEY UPDATE 
	CODE =VALUES	( CODE ),
	plan_id =VALUES	( plan_id ),
	cons =VALUES	( cons ),
	exp =VALUES	( exp ),
	conv =VALUES	( conv ),
	click =VALUES	( click ),
	dimension_time =VALUES	(	dimension_time)

此时会发现依然会重复插入数据,需要创建一个组合索引

 添加完索引再次尝试,code,plan_id,dimension_time相同的情况下只会更新不会新增

2.DUPLICATE 和REPLACE比较

replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中, 1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 2. 否则,直接插入新数据。

3.批量插入或者更新(两种方式)

方式一:mybatis-plus的saveOrUpdateBatch方法

使用saveOrUpdateBatch方法直接调用就可以了,分别在持久层实现Mapper接口,服务层接口继承 IService接口,实现类继承 ServiceImpl接口

1.持久层代码示例

说明:继承BaseMapper即可,泛型使用当前要操作类

package com.hhmt.delivery.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hhmt.delivery.pojo.entity.HhChainCustomerInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

/**
 * 链路客户信息Mapper接口
 *
 * @author huachun
 * @date 2023-01-31
 */
@Mapper
public interface HhChainCustomerInfoMapper extends BaseMapper<HhChainCustomerInfo> {
    

    /**
    * 使用mybatis-plus方式调用saveOrUpdateBatch不需要写这个接口
    boolean saveOrUpdateBatch(@Param("entities") Collection<HhChainCustomerInfo> hhChainCustomerInfos);
    */

}

2.服务层接口示例

说明:继承 IService即可,泛型使用当前要操作类

package com.hhmt.delivery.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.hhmt.delivery.pojo.entity.HhChainCustomerInfo;

import java.util.List;

/**
 * 链路客户信息Service接口
 *
 * @author huachun
 * @date 2023-01-31
 */
public interface IHhChainCustomerInfoService extends IService<HhChainCustomerInfo> {
    
}

3.服务实现类示例

说明:继承ServiceImpl即可,泛型使用持久层操作对象接口类和操作类

package com.hhmt.delivery.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhmt.delivery.mapper.HhChainCustomerInfoMapper;
import com.hhmt.delivery.pojo.entity.HhChainCustomerInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 链路客户信息Service业务层处理
 *
 * @author huachun
 * @date 2023-01-31
 */
@Service
public class HhChainCustomerInfoServiceImpl extends ServiceImpl<HhChainCustomerInfoMapper, HhChainCustomerInfo> implements IHhChainCustomerInfoService {


    /*@Override
    public boolean saveOrUpdateBatch(Collection<HhChainCustomerInfo> entityList) {
        return hhChainCustomerInfoMapper.saveOrUpdateBatch(entityList);
    }*/

}

4.服务层示例

package com.hhmt.delivery.controller;

import com.hhmt.delivery.core.controller.BaseController;
import com.hhmt.delivery.core.domain.model.ResultVo;
import com.hhmt.delivery.core.page.TableDataInfo;
import com.hhmt.delivery.pojo.entity.HhChainCustomerInfo;
import com.hhmt.delivery.pojo.model.query.HhChainCustomerInfoQuery;
import com.hhmt.delivery.pojo.model.vo.HhChainCustomerInfoVo;
import com.hhmt.delivery.service.IHhChainCustomerInfoService;
import com.hhmt.delivery.valiadtion.Add;
import com.hhmt.delivery.valiadtion.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 链路客户信息Controller
 *
 * @author huachun
 * @date 2023-01-31
 */
@Api(tags = "链路客户信息")
@RestController
@RequestMapping("/chain/HhChainCustomerInfo")
public class HhChainCustomerInfoController extends BaseController {
    @Autowired
    private IHhChainCustomerInfoService hhChainCustomerInfoService;

   
    @ApiOperation("批量插入或更新客户信息")
    @PostMapping("/batch")
    public ResultVo<Integer> addBatch(@Validated(value = Add.class) @RequestBody List<HhChainCustomerInfo> hhChainCustomerInfos) {
        return toAjax(hhChainCustomerInfoService.saveOrUpdateBatch(hhChainCustomerInfos));
    }
    
}

此时调用发现结果是成功的,数据库数据也被更新了(省略过多的测试截图)

 这种方式在执行时候会通过id判断是否有内容,然后在做更新操作。从打印的sql日志可以看出

总结:

        1.没有唯一键(id)回自动生成id后新增

        2.有id会查询后判断

        3.查询后数据有差异会调用update语句更新

问题:如果操作类集成了基础类,比如封装了BaseEntity去集成,那么这样使用会出问题

示例如下:

1.BaseEntity类(一般是实体类的公共参数)

package com.hhmt.delivery.core.domain.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Entity基类
 *
 * @author huachun
 */
@Data
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 搜索值
     */
    private String searchValue;

    /**
     * 创建者
     */
    private String createBy;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间", example = "2022-09-01 13:24:09")
    private Date createTime;

    /**
     * 更新者
     */
    private String updateBy;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间", example = "2022-09-01 13:24:09")
    private Date updateTime;

    /**
     * 备注
     */
    private String remark;

    /**
     * 请求参数
     */
    @JsonIgnore
    private Map<String, Object> params;


}

2.操作类HhChainCustomerInfo继承了BaseEntity

package com.hhmt.delivery.pojo.entity;

import com.hhmt.delivery.annotation.Excel;
import com.hhmt.delivery.constant.VerificationTips;
import com.hhmt.delivery.core.domain.model.BaseEntity;
import com.hhmt.delivery.valiadtion.Update;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotNull;

/**
 * 链路客户信息对象 hh_chain_customer_info
 *
 * @author huachun
 * @date 2023-01-31
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class HhChainCustomerInfo extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    @NotNull(groups = Update.class, message = VerificationTips.EMPTY_TIPS)
    @ApiModelProperty(value = "${comment}")
    private Long id;

    /**
     * 描述
     */
    @Excel(name = "描述")
    @ApiModelProperty(value = "描述")
    private String description;

    /**
     * 服务
     */
    @Excel(name = "服务")
    @ApiModelProperty(value = "服务")
    private Long serviceId;

    /**
     * 名称
     */
    @Excel(name = "名称")
    @ApiModelProperty(value = "名称")
    private String name;

    /**
     * 编码
     */
    @Excel(name = "编码")
    @ApiModelProperty(value = "编码")
    private String code;

    /**
     * 回传请求方式(1.GET 2.POST)
     */
    @Excel(name = "回传请求方式(1.GET 2.POST)")
    @ApiModelProperty(value = "回传请求方式(1.GET 2.POST)")
    private Integer reqMode;

    /**
     * 上报接口
     */
    @Excel(name = "上报接口")
    @ApiModelProperty(value = "上报接口")
    private String reqApi;

    /**
     * 签名策略
     */
    @Excel(name = "签名策略")
    @ApiModelProperty(value = "签名策略")
    private Integer signPolicy;

}

此时想要进行批量插入或者更新会出现以下问题:

有请求id时候被认为是更新,更新会通过id查询判断,问题就出在这里。plus在进行查询时候通过操作类属性去查询,导致了集成的父类属性也进去了,然而在表里面是没有这些字段的,所以出现了上述问题。

方式二:on duplicate key (推荐)

说明:通过sql的方式实现批量的插入或更新,这种方式需要有唯一索引,通过唯一索引去判断是否冲突,有冲突就会更新,没有冲突就会插入数据。

<!-- 批量插入或者更新 -->
    <insert id="saveOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into hh_chain_customer_info(id,description, create_time, update_time, service_id, name, code, req_mode,
        req_api, sign_policy)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id},#{entity.description}, #{entity.createTime}, #{entity.updateTime}, #{entity.serviceId},
            #{entity.name},
            #{entity.code}, #{entity.reqMode}, #{entity.reqApi}, #{entity.signPolicy})
        </foreach>
        on duplicate key update
        description = values(description),
        create_time = values(create_time),
        update_time = values(update_time),
        service_id = values(service_id),
        name = values(name),
        code = values(code),
        req_mode = values(req_mode),
        req_api = values(req_api),
        sign_policy = values(sign_policy)
    </insert>

1.持久层代码示例

package com.hhmt.delivery.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hhmt.delivery.pojo.entity.HhChainCustomerParams;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

/**
 * 链路客户参数Mapper接口
 *
 * @author huachun
 * @date 2023-01-31
 */
@Mapper
public interface HhChainCustomerParamsMapper extends BaseMapper<HhChainCustomerParams> {
    

    boolean insertOrUpdateBatch(@Param("entities") Collection<HhChainCustomerParams> hhChainCustomerParams);
}

2.服务实现类

package com.hhmt.delivery.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhmt.delivery.core.utils.DateUtils;
import com.hhmt.delivery.mapper.HhChainCustomerParamsMapper;
import com.hhmt.delivery.pojo.entity.HhChainCustomerParams;
import com.hhmt.delivery.service.IHhChainCustomerParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 链路客户参数Service业务层处理
 *
 * @author huachun
 * @date 2023-01-31
 */
@Service
public class HhChainCustomerParamsServiceImpl extends ServiceImpl<HhChainCustomerParamsMapper, HhChainCustomerParams> implements IHhChainCustomerParamsService {
    @Autowired
    private HhChainCustomerParamsMapper hhChainCustomerParamsMapper;    

    @Override
    public boolean saveOrUpdateBatch(Collection<HhChainCustomerParams> entityList) {
        return hhChainCustomerParamsMapper.insertOrUpdateBatch(entityList);
    }
}

此时sql日志:

拦截的sql ==>: com.hhmt.delivery.mapper.HhChainCustomerInfoMapper.saveOrUpdateBatch:insert into hh_chain_customer_info(id,description, create_time, update_time, service_id, name, code, req_mode, req_api, sign_policy) values (1621028577047281666,, , , , '111111111111', , , , ) on duplicate key update description = values(description), create_time = values(create_time), update_time = values(update_time), service_id = values(service_id), name = values(name), code = values(code), req_mode = values(req_mode), req_api = values(req_api), sign_policy = values(sign_policy)
sql耗时 ==>: 14 毫秒
类型 ==> INSERT
拦截的sql ==>: com.hhmt.delivery.mapper.HhChainCustomerInfoMapper.saveOrUpdateBatch:insert into hh_chain_customer_info(id,description, create_time, update_time, service_id, name, code, req_mode, req_api, sign_policy) values (1621028577047281666,, , , , 'dsfasdfadf', , , , ) on duplicate key update description = values(description), create_time = values(create_time), update_time = values(update_time), service_id = values(service_id), name = values(name), code = values(code), req_mode = values(req_mode), req_api = values(req_api), sign_policy = values(sign_policy)
sql耗时 ==>: 0 毫秒
类型 ==> INSERT
拦截的sql ==>: com.hhmt.delivery.mapper.HhChainCustomerInfoMapper.saveOrUpdateBatch:insert into hh_chain_customer_info(id,description, create_time, update_time, service_id, name, code, req_mode, req_api, sign_policy) values (,, , , , 'dsfasdfadf', , , , ) on duplicate key update description = values(description), create_time = values(create_time), update_time = values(update_time), service_id = values(service_id), name = values(name), code = values(code), req_mode = values(req_mode), req_api = values(req_api), sign_policy = values(sign_policy)
sql耗时 ==>: 0 毫秒

个人感觉这样效率更改更方便,值得推荐

4.注意

on udplicate key update后的内容表示,主键存在时则执行更新操作,需要注意的是insert字段中需要含有唯一性字段(主键索引或唯一索引)

原文参考 mysql插入或更新_w_t_y_y的博客-CSDN博客_mysql 插入更新

5.常见问题 

详细后续补充~

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

批量插入或更新数据(MyBatis-plus框架) 的相关文章

  • DIY:用开源软件搭建自己的物联网

    原文链接 xff1a DIY Open Source Software for your very own IoT 作者 xff1a Vishal Shah 翻译 xff1a 赵屹华 审校 xff1a 刘翔宇 图片来源 xff1a open
  • ubuntu开启rdp服务

    概要 ssh登录用于终端 xff0c 如果需要GUI的远程登陆ubuntu 我了解到大概3中方案 vncxrdp第三方软件 向日葵 TeamViewer之类的 因为vnc我一直配置不好 xff0c 所以试了一下xrdp xff0c 这样wi

随机推荐

  • csp模拟2-T1 HRZ的序列

    题目 时间限制 1s 空间限制 64MB 题目描述 相较于咕咕东 xff0c 瑞神是个起早贪黑的好孩子 xff0c 今天早上瑞神起得很早 xff0c 刷B站时看到了一个序列aaa xff0c 他对这个序列产生了浓厚的兴趣 他好奇是否存在一个
  • Ubuntu 中文件和目录的操作命令

    在 Ubuntu 中 xff0c 文件和目录的操作命令是非常重要的 这些命令帮助您在文件系统中创建 复制 移动 删除和查看文件和目录 以下是一些常用的文件和目录操作命令 xff1a cd cd 命令用于切换当前工作目录 例如 xff0c 要
  • Docker无法在WSL2的Ubuntu启动的问题

    今天在更新了WSL2上的Ubuntu22 04版本 xff0c 在安装Docker后无法启动 xff0c 查看Docker的日志显示如下的错误 INFO 2022 04 22T16 14 55 718999500 43 08 00 stop
  • C语言中的移位操作

    C语言中的移位操作 xff0c 内容不多 不过有些地方你不注意 xff0c 就疏忽了 先做两个小题先 1 unsigned char x 61 3 x lt lt 1是多少 xff1f x gt gt 1是多少 xff1f 2 char x
  • Android获取设备唯一标识的方法

    String uniqueId String mac 61 getMacAddressByInetAddress if mac 61 null amp amp mac equals 34 34 amp amp mac equals 34 0
  • Linux 搭建私有CA证书服务器之超详细版本

    一 CA简介 CA是什么 xff1f CA是Certificate Authority的简写 xff0c 从字面意思翻译过来是凭证管理中心 xff0c 认证授权 它有点类似我们生活中的身份证颁发机构 xff0c 这里的CA就相当于生活中颁发
  • 基于Nginx搭建RTMP-HLS视频直播服务器(推流+拉流)

    1 环境准备 Linux centos7 6 nginx 1 18 0 源码包 span class token function wget span http nginx org download nginx 1 8 1 tar gz n
  • k8s-部署本地仓库harbor

    1 基础配置 xff1a 主机名IP系统版本k8s master192 168 32 128centos 7 6k8s node1192 168 32 129centos 7 6k8s node2192 168 32 130centos 7
  • k8s部署nginx容器

    1 创建挂载nginx namespace yaml配置文件 xff08 k8s master xff1a 192 168 32 128 xff09 apiVersion v1 kind Namespace metadata name ng
  • k8s部署tomcat并且映射本地目录

    1 编写Dockerfile span class token punctuation span root 64 VM 12 7 centos opt span class token punctuation span span class
  • 自动化运维记录之GitLab CI/CD 自动化部署入门教程

    1 前端项目自动化部署需要的环境依赖 Node 安装项目依赖 打包都需要 Nginx web 项目部署必须 正向代理 方向代理 负载均衡等等 GitLab 也会用到 Nginx span class token punctuation sp
  • k8s-kubeadm证书过期续订解决方法

    1 实验目的 通过kubeadm安装的kubernetes集群各个组件所使用证书的期限为1年 xff0c 本实验练习的是到期之后如何续期 2 实验环境 ubernetes环境及版本 整个实验三台机器192 168 32 128作为maste
  • 宝塔部署Django项目-避坑必看

    1 在linux windoes机器上已经安装宝塔 浏览器登录宝塔管理页面 1 1打包模块 span class token number 1 span 将本地计算机的项目下的模块打包 python m pip freeze span cl
  • week11作业—A - 必做题11-1—

    题目 蒜头君从现在开始工作 xff0c 年薪 NNN 万 他希望在蒜厂附近买一套 606060 平米的房子 xff0c 现在价格是 200200200 万 假设房子价格以每年百分之 KKK 增长 xff0c 并且蒜头君未来年薪不变 xff0
  • Linux 系统 nginx 源码编译安装

    nginx版本 xff1a nginx 1 18 0 操作实施环境 Ubuntu 16 04 6 LTS SUSE Linux Enterprise Server 12 SP4 x86 64 注 xff1a 因为是源码安装 xff0c 操作
  • ubuntu18.04和20.04(ubuntu focal)安装MySQL8并使用navicat连接(详细)

    文章的第一节转载自原文连接 本文实现了服务器ubuntu18 04和虚拟机ubuntu20 04两个版本的MySQL8 的安装 xff0c 和navicat的连接 其中ubuntu18 04对应第二节 xff0c ubuntu20 04是u
  • 对接淘宝公共平台API

    1 说明 由于项目临时提出需求 xff0c 需要对接淘宝公共平台查询用户的一些信息 xff0c 所以需要和淘宝平台做对接 xff0c 我查看了一下淘宝公共平台开发文档 xff0c 虽然写的挺丰富挺整洁 xff0c 但我还是一头雾水 xff0
  • 使用IDEA插件从数据库表生成实体类

    目录 1 介绍 2 添加插件 3 创建数据库连接 4 添加数据库连接信息和驱动 5 表生成实体类 1 介绍 EasyCode是基于IntelliJ IDEA Ultimate版开发的一个代码生成插件 xff0c 主要通过自定义模板 基于ve
  • SpringBoot多环境动态环境切换(nacos)

    目录 1 环境变量切换 1 1 建立各环境配置文件 1 2 设置环境变量 2 nacos配置中心动态切换 2 1 配置文件 2 2 nacos配置 2 3 启动服务 3 同一nacos环境下服务不同环境控制 3 1 cloud方式 3 1
  • 批量插入或更新数据(MyBatis-plus框架)

    目录 1 场景说明 2 DUPLICATE 和REPLACE比较 3 批量插入或者更新 两种方式 方式一 xff1a mybatis plus的saveOrUpdateBatch方法 问题 xff1a 如果操作类集成了基础类 xff0c 比