广告
返回顶部
首页 > 资讯 > 数据库 >mybatis 实现插入或更新数据功能,数据存在时只更新
  • 627
分享到

mybatis 实现插入或更新数据功能,数据存在时只更新

mybatis数据库mysql 2023-09-08 18:09:00 627人浏览 八月长安
摘要

需求 提供一个接口,既能保证新数据的插入操作,又能在数据存在时进行数据更新操作 实现:on duplicate key update 在Mysql中,提供有on duplicate key updat

需求

提供一个接口,既能保证新数据的插入操作,又能在数据存在时进行数据更新操作

实现:on duplicate key update

Mysql中,提供有on duplicate key update 指令,该指令表示如果唯一索引(UNIQUE)或主键(PRIMARY KEY)出现重复值时,则执行更新操作;如果不存在唯一冲突,则执行插入操作。

on duplicate key update 指令只是mysql特性。

实例:单行数据

创建数据表,建立主键约束PRIMARY KEY (ue_id)和唯一约束UNIQUE KEYedge_info_UN (unique_id)

CREATE TABLE `edge_info` (  `ue_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '设备id',  `unique_id` varchar(50) DEFAULT '' COMMENT 'Mac',  `temperature` varchar(500) DEFAULT '' COMMENT '温度',  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',  PRIMARY KEY (`ue_id`),  UNIQUE KEY `edge_info_UN` (`unique_id`)) ENGINE=InnoDB AUTO_INCREMENT=1261 DEFAULT CHARSET=utf8 COMMENT='边端信息心跳表';

insert语句末尾增加on duplicate key update 指令,当主键ue_id或unique_id重复时,会执行更新操作,否则执行插入操作。

<insert id="insertOrUpdateEdgeInfo" parameterType="EdgeInfo" useGeneratedKeys="true" keyProperty="ueId">        insert into edge_info        <trim prefix="(" suffix=")" suffixOverrides=",">            <if test="uniqueId != null">                unique_id,            if>            <if test="temperature != null">                temperature,            if>            <if test="createTime != null">                create_time,            if>            <if test="updateTime != null">                update_time,            if>        trim>        <trim prefix="values (" suffix=")" suffixOverrides=",">            <if test="uniqueId != null">                #{uniqueId},            if>            <if test="temperature != null">                #{temperature},            if>            <if test="createTime != null">                #{createTime},            if>            <if test="updateTime != null">                #{updateTime},            if>        trim>        on duplicate key update        <trim>            <if test="uniqueId != null">                unique_id = #{uniqueId},            if>            <if test="temperature != null">                temperature = #{temperature},            if>            <if test="createTime != null">                create_time = #{createTime},            if>            <if test="updateTime != null">                update_time = #{updateTime}            if>        trim>    insert>

实例:批量数据

同样采用上述表作为示例来讲解,在原来代码基础上增加foreach标签,代码如下。同样当主键ue_id或unique_id重复时,会执行更新操作,否则执行插入操作。

<insert id="batchInsertOrUpdateEdgeInfo" parameterType="EdgeInfo" useGeneratedKeys="true" keyProperty="ueId">        insert into edge_info(        unique_id, temperature, create_time, update_time       )       values       <foreach collection="edgeInfoList" item="edge" separator=",">       (       #{edge.uniqueId},            #{edge.temperature},            #{edge.createTime},            #{edge.updateTime}        foreach>        on duplicate key update        unique_id = values(unique_id),        temperature = values(temperature),        create_time = values(create_time),        update_time = values(update_time)    insert>

当批量插入数据量较大时,为了确保接口响应的性能,可以考虑将数据分批地批量插入,如5000条数据需要插入,我们可以将数据分成100行执行一次批量插入。

来源地址:https://blog.csdn.net/Ber_Bai/article/details/128076093

您可能感兴趣的文档:

--结束END--

本文标题: mybatis 实现插入或更新数据功能,数据存在时只更新

本文链接: https://www.lsjlt.com/news/400443.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作