🎉 add new utils
This commit is contained in:
parent
992f416578
commit
0185f12088
@ -46,6 +46,12 @@
|
||||
### redis工具
|
||||
utils.redis.RedisCacheUtils
|
||||
|
||||
### 字符串处理工具
|
||||
utils.CamelCAsseUtil 将下划线分割字符串转为驼峰式 (aaa_bbb_ccc => aaaBbbCcc)
|
||||
|
||||
### 类型转换工具
|
||||
utils.EntityUtil 将实体类转为map <String, Object>
|
||||
|
||||
### 自动生成数据库所有实体类
|
||||
autoentity.pom.xml
|
||||
基于jOOQ的自动生成实体类功能,添加pom文件或者修改至自己的pom文件中,maven打包即可在对应处生成实体类
|
||||
|
51
src/main/java/utils/CamelCaseUtil.java
Normal file
51
src/main/java/utils/CamelCaseUtil.java
Normal file
@ -0,0 +1,51 @@
|
||||
package utils;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CamelCaseUtil {
|
||||
|
||||
/**
|
||||
* 数据表字段名转换为驼峰式名字的实体类属性名
|
||||
* @param tabAttr 数据表字段名
|
||||
* @return 转换后的驼峰式命名
|
||||
*/
|
||||
public static String camelize(String tabAttr){
|
||||
if(isBlank(tabAttr))
|
||||
return tabAttr;
|
||||
Pattern pattern = Pattern.compile("(.*)_(\\w)(.*)");
|
||||
Matcher matcher = pattern.matcher(tabAttr);
|
||||
if(matcher.find()){
|
||||
return camelize(matcher.group(1) + matcher.group(2).toUpperCase() + matcher.group(3));
|
||||
}else{
|
||||
return tabAttr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰式的实体类属性名转换为数据表字段名
|
||||
* @param camelCaseStr 驼峰式的实体类属性名
|
||||
* @return 转换后的以"_"分隔的数据表字段名
|
||||
*/
|
||||
public static String decamelize(String camelCaseStr){
|
||||
return isBlank(camelCaseStr) ? camelCaseStr : camelCaseStr.replaceAll("[A-Z]", "_$0").toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串是否为空
|
||||
* @param cs 待检查的字符串
|
||||
* @return 空:true; 非空:false
|
||||
*/
|
||||
public static boolean isBlank(final CharSequence cs) {
|
||||
int strLen;
|
||||
if (cs == null || (strLen = cs.length()) == 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (!Character.isWhitespace(cs.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
22
src/main/java/utils/EntityUtil.java
Normal file
22
src/main/java/utils/EntityUtil.java
Normal file
@ -0,0 +1,22 @@
|
||||
package utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class EntityUtil {
|
||||
|
||||
public static Map<String, Object> entityToMap(Object entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
Field[] fields = entity.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
result.put(field.getName(), field.get(entity));
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user