mysql 新增或修改


一、DUPLICATE KEY UPDATE +關鍵字

1、准備一個表

2、給特定字段創建一個唯一索引(可以是組合索引)

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `status` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `create_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `create_date` datetime NULL DEFAULT NULL,
  `update_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `update_date` datetime NULL DEFAULT NULL,
  `remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `Indexes_1`(`username`, `phone`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

3、上手操作,示例SQL

insert into user(username,phone,status,create_by,create_date,update_by,update_date,remarks) 
values('test','123456789','0','system','2022-01-01','system','2022-01-01','')
ON DUPLICATE KEY UPDATE
status = values(status),
create_by = values(create_by),
create_date = values(create_date),
update_by = values(update_by),
update_date = values(update_date),
remarks = values(remarks)

可以看到已經插入一條數據了,然后我們修改一下 status = ‘1’

insert into user(username,phone,status,create_by,create_date,update_by,update_date,remarks) 
values('test','123456789','1','system','2022-01-01','system','2022-01-01','')
ON DUPLICATE KEY UPDATE
status = values(status),
create_by = values(create_by),
create_date = values(create_date),
update_by = values(update_by),
update_date = values(update_date),
remarks = values(remarks)

這個時候我們就已經達到效果了,數據庫並沒有新增新的數據,而是直接修改

二、MyBatis中配置

1、單條數據修改SQL配置

<insert id="" parameterType="User">
        INSERT INTO user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="username != null">username,</if>
            <if test="phone != null">phone,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createDate != null">create_date,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateDate != null">update_date,</if>
            <if test="remarks != null">remarks,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="username != null">#{username},</if>
            <if test="phone != null">#{phone},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateDate != null">#{updateDate},</if>
            <if test="remarks != null">#{remarks},</if>
        </trim>
        <trim prefix=" ON DUPLICATE KEY UPDATE " suffixOverrides=",">
            <if test="status != null">status = values(status),</if>
            <if test="createBy != null">create_by = values(create_by),</if>
            <if test="createDate != null">create_date = values(create_date),</if>
            <if test="updateBy != null">update_by = values(update_by),</if>
            <if test="updateDate != null">update_date = values(update_date),</if>
            <if test="remarks != null">remarks = values(remarks),</if>
        </trim>
    </insert>

2、多條數據批量修改

<insert id="" parameterType="java.util.List">
        insert into user(username,phone,status,create_by,create_date,update_by,update_date,remarks)
        values
        <foreach collection="list" item="entity" separator=",">
            (
            #{entity.username},
            #{entity.phone},
            #{entity.status},
            #{entity.createBy},
            #{entity.createDate},
            #{entity.updateBy},
            #{entity.updateDate},
            #{entity.remarks}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        status = values(status),
        create_by = values(create_by),
        create_date = values(create_date),
        update_by = values(update_by),
        update_date = values(update_date),
        remarks = values(remarks)
</insert>
/* 單條數據修改 */
public
long insertOrUpdate(User user);
/* 多數據批量修改或新增 */
public void insertOrUpdateList(List<User> list);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM