MyBatis批量插入(MySql进行无数据插入,有数据更新)

ok,先来原理:

当在MySql进行插入时,触发了唯一键冲突,一般会报错,但是

vbnet 复制代码
ON DUPLICATE KEY UPDATE

使用这个可以实现更新的目的。

那么条件来了,要有触发了唯一键冲突,那么你就要在表中给除id外的字段,唯一建UNIQUE的唯一索引,当然也可以多个字段一起,但是注意,当所有字段设定的长度太长时,不会创建,即相关字段不能很长很长,不过一般都是id,问题不大。

在xml中,粗略格式(当然你要在注解中也可以)

ini 复制代码
<insert id="saveOrUpdateBatch">
    INSERT INTO bs_jackyun_check_info
    ( platform,checkNo。。。 )
    VALUES
    <foreach item="item" index="index" collection="list" separator=",">
        ( #{item.platform},#{item.checkNo}。。。)
    </foreach>
    ON DUPLICATE KEY UPDATE
    // 如果有数据要进行更新的字段 platform = VALUES( platform )这种模式
    platform = VALUES( platform ),check_no = VALUES( check_no )。。。
</insert>

然后附上一个生成这个的sql的方法,不过潦草,哈哈哈哈哈

不过需要注意一点点小地方

第一

go 复制代码
这里是,你没有@TableField(exist = false)注解,但是这个字段又不想更新的地方,当然自定义注解什么的也可以,但是我不想写

// 不存在于名单中进行更新
String[] whiteList = {"isAudit", "reportNumber"};
if (!StringUtils.ifMatches(field.getName(),whiteList)) {
    // 构建更新字段
    updateSql.append(name).append(" = VALUES( ").append(name).append(" ),");
}

第二

go 复制代码
这里,你可能需要补充基类的字段,
// 补充基类字段
builder.append(" id, create_by, create_time, update_by, update_time, del_flag, group_id ");

还有这里

go 复制代码
updateSql.append(" update_by = VALUES(update_by), update_time = VALUES(update_time)");
builder.append(" ) ").append("\n").append("VALUES ").append("\n");

以及这里

ruby 复制代码
builder.append(",#{item.id},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime},#{item.delFlag},#{item.groupId}");

OK,那完整的代码在下面,可能有些潦草,如果需要使用请按自己的需求更改

ini 复制代码
public static void sdssas(Class<?> entityClass){
    StringBuilder builder = new StringBuilder();
    StringBuilder updateSql = new StringBuilder();
    builder.append("INSERT INTO ");
    // 获取表名
    TableName annotation = entityClass.getAnnotation(TableName.class);
    String tableName = annotation.value();
    builder.append(tableName).append("\n").append(" ( ");
    // 获取属性
    Field[] declaredFields = entityClass.getDeclaredFields();
    CopyOnWriteArrayList<String> fieldNameList = new CopyOnWriteArrayList<>();
    // 按属性设置sql字段
    Arrays.stream(declaredFields).forEach(field -> {
        TableField tableField = field.getAnnotation(TableField.class);
        if (tableField == null || tableField.exist()){
            // 转驼峰命名
            String name = StringUtils.toUnderScoreCase(field.getName());
            fieldNameList.add(field.getName());
            builder.append(name).append(",");
            // 不存在于名单中进行更新
            String[] whiteList = {"isAudit", "reportNumber"};
            if (!StringUtils.ifMatches(field.getName(),whiteList)) {
                // 构建更新字段
                updateSql.append(name).append(" = VALUES( ").append(name).append(" ),");
            }
        }
    });
    // 补充基类字段
    builder.append(" id, create_by, create_time, update_by, update_time, del_flag, group_id ");

    updateSql.append(" update_by = VALUES(update_by), update_time = VALUES(update_time)");
    builder.append(" ) ").append("\n").append("VALUES ").append("\n");
    builder.append("<foreach item="item" index="index" collection="list" separator=",">").append("\n");
    builder.append(" ( ");
    fieldNameList.stream().forEachOrdered(fieldName -> {
        builder.append("#{item.").append(fieldName).append("},");
    });
    builder.delete(builder.length()-1,builder.length());
    builder.append(",#{item.id},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime},#{item.delFlag},#{item.groupId}");
    builder.append(")").append("\n");
    builder.append("</foreach>").append("\n");
    builder.append(" ON DUPLICATE KEY UPDATE ").append("\n");
    builder.append(updateSql);
    System.out.println(builder.toString());
}

感谢:

# MySql批量插入数据,已经存在的更新,不存在新增

相关推荐
何陋轩32 分钟前
AI时代,程序员何去何从?别慌,看完这篇你就明白了
后端·面试
weixin_4080996735 分钟前
OCR 识别率提升实战:模糊 / 倾斜 / 反光图片全套优化方案(附 Python / Java / PHP 代码)
图像处理·人工智能·后端·python·ocr·api·抠图
weixin_4080996737 分钟前
【实战教程】懒人精灵如何实现 OCR 文字识别?接口调用完整指南(附可运行示例)
java·前端·人工智能·后端·ocr·api·懒人精灵
珍朱(珠)奶茶39 分钟前
Spring Boot3整合Jxls工具包实现模版excel导出文件
spring boot·后端·excel
Daemon1 小时前
AI Agent系列记录(第二篇)
前端·人工智能·后端
冰心少年1 小时前
ROS2节点:机器人的工作细胞
后端
冰心少年1 小时前
ROS2话题:节点间传递数据的桥梁
后端
星辰徐哥1 小时前
异步定时任务系统的设计与Rust实战集成
开发语言·后端·rust
海兰2 小时前
【springboot】gradle快速镜像配置
spring boot·笔记·后端
武超杰2 小时前
SpringBoot 整合 Spring Security 实现权限控制
spring boot·后端·spring