新增工具类与方法

This commit is contained in:
wanggeng 2021-11-01 11:20:15 +08:00
parent 27424b84ad
commit 19f4649e2d
2 changed files with 113 additions and 27 deletions

View File

@ -0,0 +1,54 @@
package ink.wgink.util.jdbc;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;
/**
* @ClassName: JdbcUtil
* @Description: Jdbc
* @Author: WangGeng
* @Date: 2019-07-14 18:53
* @Version: 1.0
**/
public class JdbcUtil {
/**
* 获取单条信息
*
* @param resultSet
* @return
*/
public static Map<String, Object> getResult(ResultSet resultSet) {
List<Map<String, Object>> resultList = listResult(resultSet);
if (resultList.isEmpty()) {
return null;
}
return resultList.get(0);
}
/**
* 查询列表
*
* @param resultSet
* @return
*/
public static List<Map<String, Object>> listResult(ResultSet resultSet) {
List<Map<String, Object>> resultList = new ArrayList<>();
try {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
while (resultSet.next()) {
Map<String, Object> row = new LinkedHashMap<>(16);
for (int i = 0; i < columnCount; i++) {
row.put(resultSetMetaData.getColumnLabel(i + 1), resultSet.getObject(i + 1));
}
resultList.add(row);
}
} catch (Exception e) {
return new ArrayList<>();
}
return resultList;
}
}

View File

@ -19,28 +19,6 @@ import java.util.regex.Pattern;
public class WStringUtil {
static final Pattern LOWER_UPPER_PATTERN = Pattern.compile("[A-Z][a-z]*");
static final Pattern PHONE_PATTERN = Pattern.compile("1\\d{10}");
static final Pattern EMAIL_PATTERN = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
/**
* 是否是电话
*
* @param value
* @return
*/
public static boolean isPhone(String value) {
return PHONE_PATTERN.matcher(value).matches();
}
/**
* 是否是邮箱
*
* @param value
* @return
*/
public static boolean isEmail(String value) {
return EMAIL_PATTERN.matcher(value).matches();
}
/**
* 将以逗号分隔的字符串转换成字符串数组
@ -139,15 +117,31 @@ public class WStringUtil {
* @date 2018年2月28日 下午4:28:45
*/
public static String lowerUpper2UnderLine(String str) {
return lowerUpper2Separator(str, "-");
}
/**
* <p>
* title 驼峰名称转分隔符名称
* </p>
* <p>
* description 驼峰名称转小写名称用分隔符名称
* </p>
*
* @param str
* @param separator
* @return
*/
public static String lowerUpper2Separator(String str, String separator) {
Matcher matcher = LOWER_UPPER_PATTERN.matcher(str);
while (matcher.find()) {
String group = matcher.group();
String lower = "_" + group.toLowerCase();
String lower = separator + group.toLowerCase();
str = str.replaceFirst(group, lower);
matcher = LOWER_UPPER_PATTERN.matcher(str);
}
if (str.startsWith("_")) {
str = str.substring(1, str.length());
if (str.startsWith(separator)) {
str = str.substring(1);
}
return str;
}
@ -168,7 +162,23 @@ public class WStringUtil {
* @date 2018年2月28日 下午4:49:13
*/
public static String underLine2LowerUpper(String str) {
String[] strs = str.split("_");
return separator2LowerUpper(str, "_");
}
/**
* <p>
* title 分割符转驼峰式
* </p>
* <p>
* description 分割符转驼峰式
* </p>
*
* @param str 字符串
* @param separator 分隔符
* @return
*/
public static String separator2LowerUpper(String str, String separator) {
String[] strs = str.split(separator);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
String letter = strs[i].toLowerCase();
@ -209,7 +219,29 @@ public class WStringUtil {
String result = underLine2LowerUpper(str);
if (firstLower) {
int first = result.charAt(0) + 32;
result = ((char) first) + result.substring(1, result.length());
result = ((char) first) + result.substring(1);
}
return result;
}
/**
* <p>
* title 分隔符转驼峰式
* </p>
* <p>
* description 分隔符转驼峰式
* </p>
*
* @param str 字符串
* @param separator 分隔符
* @param firstLower 首字母小写
* @return
*/
public static String separator2LowerUpper(String str, String separator, boolean firstLower) {
String result = separator2LowerUpper(str, separator);
if (firstLower) {
int first = result.charAt(0) + 32;
result = ((char) first) + result.substring(1);
}
return result;
}