com.tmsps.ne4springboot.orm.ClassUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ne4SpringBoot Show documentation
Show all versions of ne4SpringBoot Show documentation
NE For SpringBoot is good :)
The newest version!
package com.tmsps.ne4springboot.orm;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.tmsps.ne4springboot.annotation.Column;
import com.tmsps.ne4springboot.annotation.NotMap;
import com.tmsps.ne4springboot.annotation.PK;
import com.tmsps.ne4springboot.annotation.Table;
import com.tmsps.ne4springboot.orm.model.DataModel;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
/**
*
* @author zhangwei [email protected]
*
*/
public class ClassUtil {
/**
* @Description: 获取表名称,如果没有表名称注解,则返回Class的名称
* @author: zhangwei(Mr.z)[email protected]
* @date: 2023/05/19
*/
public static String getTableName(Class extends DataModel> clazz) {
// 没有Table注解,返回类的SimplName
if (!clazz.isAnnotationPresent(Table.class)) {
return clazz.getSimpleName().toString();
}
// Table注解的value值不为空,返回value的值为表名称
if (StrUtil.isNotBlank(clazz.getAnnotation(Table.class).value())) {
return clazz.getAnnotation(Table.class).value();
}
//Table注解的TableName值不为空,返回TableName的值为表名称
if (StrUtil.isNotBlank(clazz.getAnnotation(Table.class).TableName())) {
return clazz.getAnnotation(Table.class).TableName();
}
// 未找到显式的表明,返回约定的类名称作为表名称
return clazz.getSimpleName().toString();
}
/**
* @Description: 返回Bean中所有的字段名称
* @author: zhangwei(Mr.z)[email protected]
* @date: 2023/05/20
*/
public static List getPropertyName(Class> clazz) {
List list = new ArrayList();
List fields = getClassFields(clazz);
for (Field field : fields) {
// 剔除不参与映射字段@NotMap、合成字段synthetic
if (field.isAnnotationPresent((Class extends Annotation>) NotMap.class) || field.isSynthetic()) {
continue;
}
if (field.isAnnotationPresent((Class extends Annotation>) Column.class)) {
list.add(field.getAnnotation(Column.class).name());
continue;
}
list.add(field.getName());
}
return list;
}
/**
* @Description: 获取类型中指定注解的属性名称
* @author: zhangwei(Mr.z)[email protected] @date: 2023/07/18
*/
public static List getTargetAnnotationPropertyName(Class> clazz, Class extends Annotation> targetAnnotation) {
List list = new ArrayList();
List fields = getClassFields(clazz);
for (Field field : fields) {
if (field.isAnnotationPresent(targetAnnotation)) {
list.add(field.getName());
}
}
return list;
}
/**
* @Description: 返回不带主键字段的class属性名称
* @author: zhangwei(Mr.z)[email protected]
* @date: 2023/05/20
*/
public static List getPropertyNameWithoutPk(Class> clazz) {
List list = new ArrayList();
List fields = getClassFields(clazz);
for (Field field : fields) {
// 剔除不参与映射字段@NotMap、合成字段synthetic
if (field.isAnnotationPresent((Class extends Annotation>) NotMap.class) || field.isAnnotationPresent((Class extends Annotation>) PK.class) || field.isSynthetic()) {
continue;
}
if (field.isAnnotationPresent((Class extends Annotation>) Column.class)) {
list.add(field.getAnnotation(Column.class).name());
continue;
}
list.add(field.getName());
}
return list;
}
public static List getClassFields(Class> clazz) {
List clazzField = new ArrayList();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
// 剔除不参与映射字段@NotMap、合成字段synthetic
if (field.isAnnotationPresent(NotMap.class) || field.isSynthetic()) {
continue;
}
clazzField.add(field);
} // #for
return clazzField;
}
/**
* @Description: 返回属性值列表
* @author: zhangwei(Mr.z)[email protected]
* @date: 2023/05/20
*/
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy