/** * MySql专用批量保存实体,如果唯一键已存在则更新。 */ public long insertDuplicateUpdates(Collection<?> beans, Path<?>... duplicatePaths) { // 把相同的SQL合并成批量模式 Map<Collection<Path<?>>, AbstractSQLInsertClause<?>> updatePathsInsertMap = new HashMap<>(); for (Object bean : beans) { Map<Path<?>, Object> valuesMap = DefaultMapper.DEFAULT.createMap(entity, bean); Collection<Path<?>> duplicates = filterUpdatePaths(valuesMap.keySet(), duplicatePaths); AbstractSQLInsertClause<?> insert = updatePathsInsertMap.computeIfAbsent( duplicates, (paths) -> onDuplicateUpdates( new MysqlInsert(connection(), configuration, entity), paths)); valuesMap.forEach((key, value) -> insert.set((Path<Object>) key, value)); insert.addBatch(); } return updatePathsInsertMap.values().stream() .mapToLong(AbstractSQLInsertClause::execute).sum(); }