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批量插入数据,已经存在的更新,不存在新增

相关推荐
serendipity_hky25 分钟前
【SpringCloud | 第5篇】Seata分布式事务
分布式·后端·spring·spring cloud·seata·openfeign
五阿哥永琪41 分钟前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python
Victor3561 小时前
Netty(16)Netty的零拷贝机制是什么?它如何提高性能?
后端
Victor3561 小时前
Netty(15)Netty的线程模型是什么?它有哪些线程池类型?
后端
canonical_entropy2 小时前
Nop入门:增加DSL模型解析器
spring boot·后端·架构
渣娃-小晴晴2 小时前
java集合在并发环境下应用时的注意事项
java·后端
Jaising6663 小时前
PF4J 日志类冲突与 JVM 类加载机制
jvm·后端
Undoom3 小时前
智能开发环境下的 Diagram-as-Code 实践:MCP Mermaid 技术链路拆解
后端
计算机毕设VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue图书借阅管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计