cn.hutool.core.util.StrUtil.repeatAndJoin()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中cn.hutool.core.util.StrUtil.repeatAndJoin()方法的一些代码示例,展示了StrUtil.repeatAndJoin()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StrUtil.repeatAndJoin()方法的具体详情如下:
包路径:cn.hutool.core.util.StrUtil
类名称:StrUtil
方法名:repeatAndJoin

StrUtil.repeatAndJoin介绍

[英]重复某个字符串并通过分界符连接

StrUtil.repeatAndJoin("?", 5, ",")   = "?,?,?,?,?" 
StrUtil.repeatAndJoin("?", 0, ",")   = "" 
StrUtil.repeatAndJoin("?", 5, null) = "?????"

[中]重复某个字符串并通过分界符连接

StrUtil.repeatAndJoin("?", 5, ",")   = "?,?,?,?,?" 
StrUtil.repeatAndJoin("?", 0, ",")   = "" 
StrUtil.repeatAndJoin("?", 5, null) = "?????"

代码示例

代码示例来源:origin: looly/hutool

/**
 * 构建IN语句中的值部分<br>
 * 开头必须加空格,类似:" (?,?,?)" 或者 " (1,2,3,4)"
 * 
 * @param conditionStrBuilder 条件语句构建器
 * @param condition 条件
 */
private void buildValuePartForIN(StringBuilder conditionStrBuilder, Condition condition) {
  conditionStrBuilder.append(" (");
  final Object value = condition.getValue();
  if (condition.isPlaceHolder()) {
    List<?> valuesForIn;
    // 占位符对应值列表
    if (value instanceof CharSequence) {
      valuesForIn = StrUtil.split((CharSequence) value, ',');
    } else {
      valuesForIn = Arrays.asList(Convert.convert(String[].class, value));
      if (null == valuesForIn) {
        valuesForIn = CollUtil.newArrayList(Convert.toStr(value));
      }
    }
    conditionStrBuilder.append(StrUtil.repeatAndJoin("?", valuesForIn.size(), ","));
    paramValues.addAll(valuesForIn);
  } else {
    conditionStrBuilder.append(StrUtil.join(",", value));
  }
  conditionStrBuilder.append(')');
}

代码示例来源:origin: looly/hutool

/**
 * 构建IN语句中的值部分<br>
 * 开头必须加空格,类似:" (?,?,?)" 或者 " (1,2,3,4)"
 * 
 * @param conditionStrBuilder 条件语句构建器
 * @param condition 条件
 */
private void buildValuePartForIN(StringBuilder conditionStrBuilder, Condition condition) {
  conditionStrBuilder.append(" (");
  final Object value = condition.getValue();
  if (condition.isPlaceHolder()) {
    List<?> valuesForIn;
    // 占位符对应值列表
    if (value instanceof CharSequence) {
      valuesForIn = StrUtil.split((CharSequence) value, ',');
    } else {
      valuesForIn = Arrays.asList(Convert.convert(String[].class, value));
      if (null == valuesForIn) {
        valuesForIn = CollUtil.newArrayList(Convert.toStr(value));
      }
    }
    conditionStrBuilder.append(StrUtil.repeatAndJoin("?", valuesForIn.size(), ","));
    paramValues.addAll(valuesForIn);
  } else {
    conditionStrBuilder.append(StrUtil.join(",", value));
  }
  conditionStrBuilder.append(')');
}

代码示例来源:origin: cn.hutool/hutool-db

/**
 * 构建IN语句中的值部分<br>
 * 开头必须加空格,类似:" (?,?,?)" 或者 " (1,2,3,4)"
 * 
 * @param conditionStrBuilder 条件语句构建器
 * @param paramValues 参数集合,用于参数占位符对应参数回填
 */
private void buildValuePartForIN(StringBuilder conditionStrBuilder, List<Object> paramValues) {
  conditionStrBuilder.append(" (");
  final Object value = this.value;
  if (isPlaceHolder()) {
    List<?> valuesForIn;
    // 占位符对应值列表
    if (value instanceof CharSequence) {
      valuesForIn = StrUtil.split((CharSequence) value, ',');
    } else {
      valuesForIn = Arrays.asList(Convert.convert(String[].class, value));
      if (null == valuesForIn) {
        valuesForIn = CollUtil.newArrayList(Convert.toStr(value));
      }
    }
    conditionStrBuilder.append(StrUtil.repeatAndJoin("?", valuesForIn.size(), ","));
    if(null != paramValues) {
      paramValues.addAll(valuesForIn);
    }
  } else {
    conditionStrBuilder.append(StrUtil.join(",", value));
  }
  conditionStrBuilder.append(')');
}

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 构建IN语句中的值部分<br>
 * 开头必须加空格,类似:" (?,?,?)" 或者 " (1,2,3,4)"
 * 
 * @param conditionStrBuilder 条件语句构建器
 * @param paramValues 参数集合,用于参数占位符对应参数回填
 */
private void buildValuePartForIN(StringBuilder conditionStrBuilder, List<Object> paramValues) {
  conditionStrBuilder.append(" (");
  final Object value = this.value;
  if (isPlaceHolder()) {
    List<?> valuesForIn;
    // 占位符对应值列表
    if (value instanceof CharSequence) {
      valuesForIn = StrUtil.split((CharSequence) value, ',');
    } else {
      valuesForIn = Arrays.asList(Convert.convert(String[].class, value));
      if (null == valuesForIn) {
        valuesForIn = CollUtil.newArrayList(Convert.toStr(value));
      }
    }
    conditionStrBuilder.append(StrUtil.repeatAndJoin("?", valuesForIn.size(), ","));
    if(null != paramValues) {
      paramValues.addAll(valuesForIn);
    }
  } else {
    conditionStrBuilder.append(StrUtil.join(",", value));
  }
  conditionStrBuilder.append(')');
}

代码示例来源:origin: xkcoding/spring-boot-demo

/**
 * 通用插入,自增列需要添加 {@link Pk} 注解
 *
 * @param t          对象
 * @param ignoreNull 是否忽略 null 值
 * @return 操作的行数
 */
protected Integer insert(T t, Boolean ignoreNull) {
  String table = getTableName(t);
  List<Field> filterField = getField(t, ignoreNull);
  List<String> columnList = getColumns(filterField);
  String columns = StrUtil.join(Const.SEPARATOR_COMMA, columnList);
  // 构造占位符
  String params = StrUtil.repeatAndJoin("?", columnList.size(), Const.SEPARATOR_COMMA);
  // 构造值
  Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray();
  String sql = StrUtil.format("INSERT INTO {table} ({columns}) VALUES ({params})", Dict.create().set("table", table).set("columns", columns).set("params", params));
  log.debug("【执行SQL】SQL:{}", sql);
  log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values));
  return jdbcTemplate.update(sql, values);
}

相关文章

微信公众号

最新文章

更多

StrUtil类方法