com.zys.mybatis.utils.FieldUtils Maven / Gradle / Ivy
package com.zys.mybatis.utils;
import com.zys.mybatis.annotation.Column;
import com.zys.mybatis.annotation.Id;
import com.zys.mybatis.annotation.Ignore;
import com.zys.mybatis.constant.CharFinal;
import org.apache.ibatis.reflection.property.PropertyNamer;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author zys
* @version 1.0
* @date 2020/10/20 20:14
*/
public class FieldUtils {
/**
*
* @param classes 获取字段的
* @param parentField 是否获取父类字段
* @param type 0,1,2,3 增删改查
* @return
*/
public static String querySqlField(Class> classes, Boolean parentField, int type){
//驼峰转下划线
return queryField(classes,parentField,type,false).collect(Collectors.joining(CharFinal.COMMA));
}
/**
* 获取set集合下划线字段
* @param classes 获取字段的
* @param parentField 是否获取父类字段
* @param type 0,1,2,3 增删改查
* @return
*/
public static Map,Set> getMapColumnField(Class> classes, Boolean parentField, int type){
//驼峰转下划线
return queryMapField(classes,parentField,type);
}
public static Set getMapEntityField(Class> classes, Boolean parentField, int type){
//驼峰转下划线
return queryField(classes, parentField, type, true).map(FieldUtils::toUnderline).collect(Collectors.toSet());
}
/**
* 获取字段流
* @param classes 获取字段的
* @param parentField 是否获取父类字段
* @param type 0,1,2,3 增删改查
* @param status true 驼峰 false 下划线
* @return
*/
private static Stream queryField(Class> classes,Boolean parentField,int type,boolean status){
Set returns = Arrays.stream(classes.getDeclaredFields()).map(field -> isIgnore(field, type, status))
.filter(Objects::nonNull).collect(Collectors.toSet());
if (parentField) {
getSuperclass(classes, returns, type, status);
}
return returns.stream();
}
private static Map, Set> queryMapField(Class> classes, Boolean parentField, int type){
Map,Set> map = new LinkedHashMap<>();
map.put(classes,Arrays.stream(classes.getDeclaredFields()).map(field -> isIgnore(field,type,false))
.filter(Objects::nonNull).collect(Collectors.toSet())) ;
if (parentField) {
getSuperclass(classes, map, type, false);
}
return map;
}
public static String getColumn(List listSl){
List columns = new ArrayList<>(listSl.size());
for (SerializedLambda serializedLambda : listSl) {
String property = PropertyNamer.methodToProperty(serializedLambda.getImplMethodName());
columns.add(FieldUtils.toUnderline(property));
}
return String.join(",", columns);
}
/**
* 是否存在忽略注解
* @return
*/
private static String isIgnore(Field field, int type, boolean status){
if (Modifier.isStatic(field.getModifiers())) {
// 静态直接忽略
return null;
}
if (type == 0) {
if (field.getDeclaredAnnotation(Id.class) != null) {
return null;
}
}
if (field.getDeclaredAnnotation(Ignore.class) != null) {
return null;
}
return getColumn(field, status);
}
private static String getColumn(Field field, boolean status){
Column column = field.getDeclaredAnnotation(Column.class);
if (column != null) {
return StringUtils.isBlank(column.value()) ?
( status ? field.getName() : toUnderline(field.getName()) ) : column.value();
}
return status ? field.getName() : toUnderline(field.getName());
}
/**
* 将驼峰命令转下划线
* @param field
* @return
*/
public static String toUnderline(String field){
int len = field.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = field.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(CharFinal.underline).append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 将下划线转驼峰命令
* @return
* @param field 字段
*/
public static String toHump(String field){
StringBuilder sb = new StringBuilder();
String[] strings = field.split(CharFinal.underline);
for (String s : strings) {
if ("".equals(s)) {
continue;
}
if (sb.length() == 0) {
sb.append(s.toLowerCase());
} else {
sb.append(s.substring(0, 1).toUpperCase());
sb.append(s.substring(1).toLowerCase());
}
}
return sb.toString();
}
/**
* 获取父类所有字段
* @param classes
* @return
*/
private static Set getSuperclass(Class> classes,Set returns,int type,boolean status){
Class> superclass = classes.getSuperclass();
if (superclass == null) {
return returns;
}
returns.addAll(Arrays.stream(superclass.getDeclaredFields()).map(field -> isIgnore(field,type,status)).filter(Objects::nonNull).collect(Collectors.toSet()));
getSuperclass(superclass,returns,type,status);
return returns;
}
private static Map,Set> getSuperclass(Class> classes, Map, Set> returns, int type, boolean status){
Class> superclass = classes.getSuperclass();
if (superclass == null) {
return returns;
}
returns.put(superclass,Arrays.stream(superclass.getDeclaredFields()).map(field -> isIgnore(field,type,status)).filter(Objects::nonNull).collect(Collectors.toSet()));
getSuperclass(superclass, returns, type, status);
return returns;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy