add 新增 StringUtils splitTo 与 splitList 方法 优化业务代码

2.X
疯狂的狮子li 2 years ago
parent 6c460e6ca8
commit 2039aa4cb4

@ -1,5 +1,7 @@
package com.ruoyi.common.core.service; package com.ruoyi.common.core.service;
import com.ruoyi.common.core.utils.StringUtils;
/** /**
* *
* *
@ -7,11 +9,6 @@ package com.ruoyi.common.core.service;
*/ */
public interface DictService { public interface DictService {
/**
*
*/
String SEPARATOR = ",";
/** /**
* *
* *
@ -20,7 +17,7 @@ public interface DictService {
* @return * @return
*/ */
default String getDictLabel(String dictType, String dictValue) { default String getDictLabel(String dictType, String dictValue) {
return getDictLabel(dictType, dictValue, SEPARATOR); return getDictLabel(dictType, dictValue, StringUtils.SEPARATOR);
} }
/** /**
@ -31,7 +28,7 @@ public interface DictService {
* @return * @return
*/ */
default String getDictValue(String dictType, String dictLabel) { default String getDictValue(String dictType, String dictLabel) {
return getDictValue(dictType, dictLabel, SEPARATOR); return getDictValue(dictType, dictLabel, StringUtils.SEPARATOR);
} }
/** /**

@ -96,7 +96,7 @@ public class ServletUtils extends ServletUtil {
public static Map<String, String> getParamMap(ServletRequest request) { public static Map<String, String> getParamMap(ServletRequest request) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) { for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) {
params.put(entry.getKey(), StringUtils.join(entry.getValue(), ",")); params.put(entry.getKey(), StringUtils.join(entry.getValue(), StringUtils.SEPARATOR));
} }
return params; return params;
} }

@ -42,7 +42,7 @@ public class StreamUtils {
* @return list * @return list
*/ */
public static <E> String join(Collection<E> collection, Function<E, String> function) { public static <E> String join(Collection<E> collection, Function<E, String> function) {
return join(collection, function, ","); return join(collection, function, StringUtils.SEPARATOR);
} }
/** /**

@ -1,16 +1,16 @@
package com.ruoyi.common.core.utils; package com.ruoyi.common.core.utils;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Validator; import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import java.util.ArrayList; import java.util.*;
import java.util.HashSet; import java.util.function.Function;
import java.util.List; import java.util.stream.Collectors;
import java.util.Set;
/** /**
* *
@ -20,6 +20,8 @@ import java.util.Set;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class StringUtils extends org.apache.commons.lang3.StringUtils { public class StringUtils extends org.apache.commons.lang3.StringUtils {
public static final String SEPARATOR = ",";
/** /**
* *
* *
@ -86,7 +88,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* {} 使 \\ { {} \ 使 \\\\ <br> * {} 使 \\ { {} \ 使 \\\\ <br>
* <br> * <br>
* 使format("this is {} for {}", "a", "b") -> this is a for b<br> * 使format("this is {} for {}", "a", "b") -> this is a for b<br>
* {} format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> * {} format("this is \\{} for {}", "a", "b") -> this is {} for a<br>
* \ format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> * \ format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
* *
* @param template {} * @param template {}
@ -224,7 +226,6 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* *
* @param pattern * @param pattern
* @param url url * @param url url
* @return
*/ */
public static boolean isMatch(String pattern, String url) { public static boolean isMatch(String pattern, String url) {
AntPathMatcher matcher = new AntPathMatcher(); AntPathMatcher matcher = new AntPathMatcher();
@ -238,7 +239,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @param size * @param size
* @return * @return
*/ */
public static final String padl(final Number num, final int size) { public static String padl(final Number num, final int size) {
return padl(num.toString(), size, '0'); return padl(num.toString(), size, '0');
} }
@ -250,7 +251,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @param c * @param c
* @return * @return
*/ */
public static final String padl(final String s, final int size, final char c) { public static String padl(final String s, final int size, final char c) {
final StringBuilder sb = new StringBuilder(size); final StringBuilder sb = new StringBuilder(size);
if (s != null) { if (s != null) {
final int len = s.length(); final int len = s.length();
@ -270,4 +271,55 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return sb.toString(); return sb.toString();
} }
/**
* ()
*
* @param str
* @return
*/
public static List<String> splitList(String str) {
return splitTo(str, Convert::toStr);
}
/**
*
*
* @param str
* @param separator
* @return
*/
public static List<String> splitList(String str, String separator) {
return splitTo(str, separator, Convert::toStr);
}
/**
* ()
*
* @param str
* @param mapper
* @return
*/
public static <T> List<T> splitTo(String str, Function<? super Object, T> mapper) {
return splitTo(str, SEPARATOR, mapper);
}
/**
*
*
* @param str
* @param separator
* @param mapper
* @return
*/
public static <T> List<T> splitTo(String str, String separator, Function<? super Object, T> mapper) {
if (isBlank(str)) {
return new ArrayList<>(0);
}
return StrUtil.split(str, separator)
.stream()
.filter(Objects::nonNull)
.map(mapper)
.collect(Collectors.toList());
}
} }

@ -1,5 +1,7 @@
package com.ruoyi.common.excel.annotation; package com.ruoyi.common.excel.annotation;
import com.ruoyi.common.core.utils.StringUtils;
import java.lang.annotation.*; import java.lang.annotation.*;
/** /**
@ -25,6 +27,6 @@ public @interface ExcelDictFormat {
/** /**
* *
*/ */
String separator() default ","; String separator() default StringUtils.SEPARATOR;
} }

@ -270,7 +270,7 @@ public class ExcelUtil {
*/ */
public static String convertByExp(String propertyValue, String converterExp, String separator) { public static String convertByExp(String propertyValue, String converterExp, String separator) {
StringBuilder propertyString = new StringBuilder(); StringBuilder propertyString = new StringBuilder();
String[] convertSource = converterExp.split(","); String[] convertSource = converterExp.split(StringUtils.SEPARATOR);
for (String item : convertSource) { for (String item : convertSource) {
String[] itemArray = item.split("="); String[] itemArray = item.split("=");
if (StringUtils.containsAny(propertyValue, separator)) { if (StringUtils.containsAny(propertyValue, separator)) {
@ -299,7 +299,7 @@ public class ExcelUtil {
*/ */
public static String reverseByExp(String propertyValue, String converterExp, String separator) { public static String reverseByExp(String propertyValue, String converterExp, String separator) {
StringBuilder propertyString = new StringBuilder(); StringBuilder propertyString = new StringBuilder();
String[] convertSource = converterExp.split(","); String[] convertSource = converterExp.split(StringUtils.SEPARATOR);
for (String item : convertSource) { for (String item : convertSource) {
String[] itemArray = item.split("="); String[] itemArray = item.split("=");
if (StringUtils.containsAny(propertyValue, separator)) { if (StringUtils.containsAny(propertyValue, separator)) {

@ -87,8 +87,8 @@ public class PageQuery implements Serializable {
// 兼容前端排序类型 // 兼容前端排序类型
isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"}); isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});
String[] orderByArr = orderBy.split(","); String[] orderByArr = orderBy.split(StringUtils.SEPARATOR);
String[] isAscArr = isAsc.split(","); String[] isAscArr = isAsc.split(StringUtils.SEPARATOR);
if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) { if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
throw new ServiceException("排序参数有误"); throw new ServiceException("排序参数有误");
} }

@ -52,7 +52,7 @@ public class TencentSmsTemplate implements SmsTemplate {
throw new SmsException("模板ID不能为空"); throw new SmsException("模板ID不能为空");
} }
SendSmsRequest req = new SendSmsRequest(); SendSmsRequest req = new SendSmsRequest();
Set<String> set = Arrays.stream(phones.split(",")).map(p -> "+86" + p).collect(Collectors.toSet()); Set<String> set = Arrays.stream(phones.split(StringUtils.SEPARATOR)).map(p -> "+86" + p).collect(Collectors.toSet());
req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class)); req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class));
if (CollUtil.isNotEmpty(param)) { if (CollUtil.isNotEmpty(param)) {
req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class)); req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class));

@ -212,7 +212,7 @@ public class GenTableColumn extends BaseEntity {
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
Object startStr = value.subSequence(0, 1); Object startStr = value.subSequence(0, 1);
String endStr = value.substring(1); String endStr = value.substring(1);
sb.append("").append(startStr).append("=").append(endStr).append(","); sb.append(StringUtils.EMPTY).append(startStr).append("=").append(endStr).append(StringUtils.SEPARATOR);
} }
} }
return sb.deleteCharAt(sb.length() - 1).toString(); return sb.deleteCharAt(sb.length() - 1).toString();

@ -57,7 +57,7 @@ public class GenUtils {
column.setHtmlType(GenConstants.HTML_INPUT); column.setHtmlType(GenConstants.HTML_INPUT);
// 如果是浮点型 统一用BigDecimal // 如果是浮点型 统一用BigDecimal
String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ","); String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), StringUtils.SEPARATOR);
if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) { if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
column.setJavaType(GenConstants.TYPE_BIGDECIMAL); column.setJavaType(GenConstants.TYPE_BIGDECIMAL);
} }
@ -166,7 +166,7 @@ public class GenUtils {
boolean autoRemovePre = GenConfig.getAutoRemovePre(); boolean autoRemovePre = GenConfig.getAutoRemovePre();
String tablePrefix = GenConfig.getTablePrefix(); String tablePrefix = GenConfig.getTablePrefix();
if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) { if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) {
String[] searchList = StringUtils.split(tablePrefix, ","); String[] searchList = StringUtils.split(tablePrefix, StringUtils.SEPARATOR);
tableName = replaceFirst(tableName, searchList); tableName = replaceFirst(tableName, searchList);
} }
return StringUtils.convertToCamelCase(tableName); return StringUtils.convertToCamelCase(tableName);
@ -183,7 +183,7 @@ public class GenUtils {
String text = replacementm; String text = replacementm;
for (String searchString : searchList) { for (String searchString : searchList) {
if (replacementm.startsWith(searchString)) { if (replacementm.startsWith(searchString)) {
text = replacementm.replaceFirst(searchString, ""); text = replacementm.replaceFirst(searchString, StringUtils.EMPTY);
break; break;
} }
} }

@ -230,7 +230,7 @@ public class VelocityUtils {
public static HashSet<String> getImportList(GenTable genTable) { public static HashSet<String> getImportList(GenTable genTable) {
List<GenTableColumn> columns = genTable.getColumns(); List<GenTableColumn> columns = genTable.getColumns();
GenTable subGenTable = genTable.getSubTable(); GenTable subGenTable = genTable.getSubTable();
HashSet<String> importList = new HashSet<String>(); HashSet<String> importList = new HashSet<>();
if (ObjectUtil.isNotNull(subGenTable)) { if (ObjectUtil.isNotNull(subGenTable)) {
importList.add("java.util.List"); importList.add("java.util.List");
} }
@ -252,7 +252,7 @@ public class VelocityUtils {
*/ */
public static String getDicts(GenTable genTable) { public static String getDicts(GenTable genTable) {
List<GenTableColumn> columns = genTable.getColumns(); List<GenTableColumn> columns = genTable.getColumns();
Set<String> dicts = new HashSet<String>(); Set<String> dicts = new HashSet<>();
addDicts(dicts, columns); addDicts(dicts, columns);
if (ObjectUtil.isNotNull(genTable.getSubTable())) { if (ObjectUtil.isNotNull(genTable.getSubTable())) {
List<GenTableColumn> subColumns = genTable.getSubTable().getColumns(); List<GenTableColumn> subColumns = genTable.getSubTable().getColumns();

@ -1,6 +1,7 @@
package com.ruoyi.resource.service.impl; package com.ruoyi.resource.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -70,13 +71,13 @@ public class SysOssServiceImpl implements ISysOssService {
@Override @Override
public String selectUrlByIds(String ossIds) { public String selectUrlByIds(String ossIds) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (Long id : Arrays.stream(ossIds.split(",")).map(Long::parseLong).collect(Collectors.toList())) { for (Long id : StringUtils.splitTo(ossIds, Convert::toLong)) {
SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); SysOssVo vo = SpringUtils.getAopProxy(this).getById(id);
if (ObjectUtil.isNotNull(vo)) { if (ObjectUtil.isNotNull(vo)) {
list.add(this.matchingUrl(vo).getUrl()); list.add(this.matchingUrl(vo).getUrl());
} }
} }
return String.join(",", list); return String.join(StringUtils.SEPARATOR, list);
} }
private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) { private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) {

@ -1,7 +1,7 @@
package com.ruoyi.system.controller; package com.ruoyi.system.controller;
import cn.dev33.satoken.annotation.SaCheckPermission; import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.convert.Convert;
import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
@ -48,7 +48,8 @@ public class SysDeptController extends BaseController {
@GetMapping("/list/exclude/{deptId}") @GetMapping("/list/exclude/{deptId}")
public R<List<SysDept>> excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) { public R<List<SysDept>> excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
List<SysDept> depts = deptService.selectDeptList(new SysDept()); List<SysDept> depts = deptService.selectDeptList(new SysDept());
depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtil.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")); depts.removeIf(d -> d.getDeptId().intValue() == deptId
|| StringUtils.splitList(d.getAncestors()).contains(Convert.toStr(deptId)));
return R.ok(depts); return R.ok(depts);
} }

@ -30,7 +30,6 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* *
@ -131,13 +130,13 @@ public class SysDeptServiceImpl implements ISysDeptService {
@Override @Override
public String selectDeptNameByIds(String deptIds) { public String selectDeptNameByIds(String deptIds) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (Long id : Arrays.stream(deptIds.split(",")).map(Long::parseLong).collect(Collectors.toList())) { for (Long id : StringUtils.splitTo(deptIds, Convert::toLong)) {
SysDept dept = SpringUtils.getAopProxy(this).selectDeptById(id); SysDept dept = SpringUtils.getAopProxy(this).selectDeptById(id);
if (ObjectUtil.isNotNull(dept)) { if (ObjectUtil.isNotNull(dept)) {
list.add(dept.getDeptName()); list.add(dept.getDeptName());
} }
} }
return String.join(",", list); return String.join(StringUtils.SEPARATOR, list);
} }
/** /**
@ -225,7 +224,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) { if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
throw new ServiceException("部门停用,不允许新增"); throw new ServiceException("部门停用,不允许新增");
} }
dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); dept.setAncestors(info.getAncestors() + StringUtils.SEPARATOR + dept.getParentId());
return baseMapper.insert(dept); return baseMapper.insert(dept);
} }
@ -241,7 +240,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
SysDept newParentDept = baseMapper.selectById(dept.getParentId()); SysDept newParentDept = baseMapper.selectById(dept.getParentId());
SysDept oldDept = baseMapper.selectById(dept.getDeptId()); SysDept oldDept = baseMapper.selectById(dept.getDeptId());
if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) { if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) {
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); String newAncestors = newParentDept.getAncestors() + StringUtils.SEPARATOR + newParentDept.getDeptId();
String oldAncestors = oldDept.getAncestors(); String oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors); dept.setAncestors(newAncestors);
updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);

@ -92,7 +92,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
Set<String> permsSet = new HashSet<>(); Set<String> permsSet = new HashSet<>();
for (String perm : perms) { for (String perm : perms) {
if (StringUtils.isNotEmpty(perm)) { if (StringUtils.isNotEmpty(perm)) {
permsSet.addAll(Arrays.asList(perm.trim().split(","))); permsSet.addAll(StringUtils.splitList(perm.trim()));
} }
} }
return permsSet; return permsSet;
@ -110,7 +110,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
Set<String> permsSet = new HashSet<>(); Set<String> permsSet = new HashSet<>();
for (String perm : perms) { for (String perm : perms) {
if (StringUtils.isNotEmpty(perm)) { if (StringUtils.isNotEmpty(perm)) {
permsSet.addAll(Arrays.asList(perm.trim().split(","))); permsSet.addAll(StringUtils.splitList(perm.trim()));
} }
} }
return permsSet; return permsSet;

@ -5,11 +5,11 @@ import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery; import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo; import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.satoken.utils.LoginHelper; import com.ruoyi.common.satoken.utils.LoginHelper;
@ -106,7 +106,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
Set<String> permsSet = new HashSet<>(); Set<String> permsSet = new HashSet<>();
for (SysRole perm : perms) { for (SysRole perm : perms) {
if (ObjectUtil.isNotNull(perm)) { if (ObjectUtil.isNotNull(perm)) {
permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(","))); permsSet.addAll(StringUtils.splitList(perm.getRoleKey().trim()));
} }
} }
return permsSet; return permsSet;

Loading…
Cancel
Save