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

相关推荐
Amagi.1 小时前
Spring中Bean的作用域
java·后端·spring
2402_857589361 小时前
Spring Boot新闻推荐系统设计与实现
java·spring boot·后端
J老熊2 小时前
Spring Cloud Netflix Eureka 注册中心讲解和案例示范
java·后端·spring·spring cloud·面试·eureka·系统架构
Benaso2 小时前
Rust 快速入门(一)
开发语言·后端·rust
sco52822 小时前
SpringBoot 集成 Ehcache 实现本地缓存
java·spring boot·后端
原机小子2 小时前
在线教育的未来:SpringBoot技术实现
java·spring boot·后端
吾日三省吾码2 小时前
详解JVM类加载机制
后端
努力的布布2 小时前
SpringMVC源码-AbstractHandlerMethodMapping处理器映射器将@Controller修饰类方法存储到处理器映射器
java·后端·spring
PacosonSWJTU3 小时前
spring揭秘25-springmvc03-其他组件(文件上传+拦截器+处理器适配器+异常统一处理)
java·后端·springmvc
记得开心一点嘛3 小时前
在Java项目中如何使用Scala实现尾递归优化来解决爆栈问题
开发语言·后端·scala