/** * 获取某个字段的值 * * @param entity * @param fieldName * @return */ public static Field getFieldByColumnName(Class<?> clazz, String columnName) { Field field = null; if (columnName != null) { Field[] fields = clazz.getDeclaredFields(); if (fields != null && fields.length > 0) { if (columnName.equals(ClassUtils.getPrimaryKeyColumn(clazz))) field = ClassUtils.getPrimaryKeyField(clazz); if (field == null) { for (Field f : fields) { Property property = f.getAnnotation(Property.class); if (property != null && columnName.equals(property.column())) { field = f; break; } ManyToOne manyToOne = f.getAnnotation(ManyToOne.class); if (manyToOne != null && manyToOne.column().trim().length() != 0) { field = f; break; } } } if (field == null) { field = getFieldByName(clazz, columnName); } } } return field; }
/** * 获取某个熟悉对应的 表的列 * * @param entity * @param fieldName * @return */ public static String getColumnByField(Field field) { Property property = field.getAnnotation(Property.class); if (property != null && property.column().trim().length() != 0) { return property.column(); } ManyToOne manyToOne = field.getAnnotation(ManyToOne.class); if (manyToOne != null && manyToOne.column().trim().length() != 0) { return manyToOne.column(); } OneToMany oneToMany = field.getAnnotation(OneToMany.class); if (oneToMany != null && oneToMany.manyColumn() != null && oneToMany.manyColumn().trim().length() != 0) { return oneToMany.manyColumn(); } Id id = field.getAnnotation(Id.class); if (id != null && id.column().trim().length() != 0) return id.column(); return field.getName(); }