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

This commit is contained in:
疯狂的狮子li 2023-02-06 14:26:21 +08:00
parent b9b76539ac
commit e803388cad
16 changed files with 87 additions and 33 deletions

View File

@ -1,7 +1,7 @@
package com.ruoyi.web.controller.system; package com.ruoyi.web.controller.system;
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.annotation.Log; import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
@ -49,7 +49,7 @@ public class SysDeptController extends BaseController {
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().equals(deptId) depts.removeIf(d -> d.getDeptId().equals(deptId)
|| ArrayUtil.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")); || StringUtils.splitList(d.getAncestors()).contains(Convert.toStr(deptId)));
return R.ok(depts); return R.ok(depts);
} }

View File

@ -1,5 +1,7 @@
package com.ruoyi.common.annotation; package com.ruoyi.common.annotation;
import com.ruoyi.common.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;
} }

View File

@ -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("排序参数有误");
} }

View File

@ -25,7 +25,7 @@ public class XssFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
String tempExcludes = filterConfig.getInitParameter("excludes"); String tempExcludes = filterConfig.getInitParameter("excludes");
if (StringUtils.isNotEmpty(tempExcludes)) { if (StringUtils.isNotEmpty(tempExcludes)) {
String[] url = tempExcludes.split(","); String[] url = tempExcludes.split(StringUtils.SEPARATOR);
for (int i = 0; url != null && i < url.length; i++) { for (int i = 0; url != null && i < url.length; i++) {
excludes.add(url[i]); excludes.add(url[i]);
} }

View File

@ -94,7 +94,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;
} }

View File

@ -41,7 +41,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);
} }
/** /**

View File

@ -1,16 +1,16 @@
package com.ruoyi.common.utils; package com.ruoyi.common.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 = ",";
/** /**
* 获取参数不为空值 * 获取参数不为空值
* *
@ -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();
@ -234,23 +235,23 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
/** /**
* 数字左边补齐0使之达到指定长度注意如果数字转换为字符串后长度大于size则只保留 最后size个字符 * 数字左边补齐0使之达到指定长度注意如果数字转换为字符串后长度大于size则只保留 最后size个字符
* *
* @param num 数字对象 * @param num 数字对象
* @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');
} }
/** /**
* 字符串左补齐如果原始字符串s长度大于size则只保留最后size个字符 * 字符串左补齐如果原始字符串s长度大于size则只保留最后size个字符
* *
* @param s 原始字符串 * @param s 原始字符串
* @param size 字符串指定长度 * @param size 字符串指定长度
* @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());
}
} }

View File

@ -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)) {

View File

@ -32,7 +32,7 @@ public class FilterConfig {
FilterRegistrationBean registration = new FilterRegistrationBean(); FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setDispatcherTypes(DispatcherType.REQUEST); registration.setDispatcherTypes(DispatcherType.REQUEST);
registration.setFilter(new XssFilter()); registration.setFilter(new XssFilter());
registration.addUrlPatterns(StringUtils.split(xssProperties.getUrlPatterns(), ",")); registration.addUrlPatterns(StringUtils.split(xssProperties.getUrlPatterns(), StringUtils.SEPARATOR));
registration.setName("xssFilter"); registration.setName("xssFilter");
registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE); registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
Map<String, String> initParameters = new HashMap<String, String>(); Map<String, String> initParameters = new HashMap<String, String>();

View File

@ -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();

View File

@ -58,7 +58,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);
} }
@ -167,7 +167,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);
@ -184,7 +184,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;
} }
} }

View File

@ -232,7 +232,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");
} }

View File

@ -53,7 +53,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));

View File

@ -226,7 +226,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
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);
} }
@ -242,7 +242,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
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);

View File

@ -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;

View File

@ -5,7 +5,6 @@ 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.constant.UserConstants; import com.ruoyi.common.constant.UserConstants;
@ -15,6 +14,7 @@ import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper; import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StreamUtils; import com.ruoyi.common.utils.StreamUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysRoleDept; import com.ruoyi.system.domain.SysRoleDept;
import com.ruoyi.system.domain.SysRoleMenu; import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.domain.SysUserRole; import com.ruoyi.system.domain.SysUserRole;
@ -107,7 +107,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;