/** * Populate the INSERT clause with the properties of the given bean using * the given Mapper. * * @param obj object to use for population * @param mapper mapper to use * @return the current object */ @SuppressWarnings("rawtypes") @WithBridgeMethods(value = SQLInsertClause.class, castRequired = true) public <T> C populate(T obj, Mapper<T> mapper) { Map<Path<?>, Object> values = mapper.createMap(entity, obj); for (Map.Entry<Path<?>, Object> entry : values.entrySet()) { set((Path) entry.getKey(), entry.getValue()); } return (C) this; }
/** * 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(); }