Based on the string, get the annotation on the entity attribute, e.g.: createTime" Find the TableField in the corresponding entity attribute(value = "create_time", fill = )
- Field[] fields = (); // only get class (and its parent) public attribute members
- Field[] declaredFields = (); //Only get the attribute members of the class itself (including private, shared, protected)
- Field[] superDeclaredFields = ().getDeclaredFields(); //So when getting the private attributes of the parent class, get them through getSuperclass's followed by getDeclaredFiled.
public class BaseEntity implements Serializable {
/**
* Creation time
*/
@Schema(description = "Creation time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(value = "create_time", fill = )
private Date createTime;
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
= createTime;
}
}
/**
* character
*/
@Schema(description = "character信息")
@TableName("sys_role")
public class SysRole extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* fitness (loanword)ID
*/
@Schema(description = "fitness (loanword)ID")
@TableId(value = "id", type = IdType.ASSIGN_UUID)
private String id;
/**
* character标识
*/
@Schema(description = "character标识")
@TableField(value = "code")
private String code;
public String getId() {
return id;
}
public void setId(String id) {
= id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
= code;
}
}
MybatisPlusTest
import ;
import ;
import .;
import ;
import ;
import ;
public class MybatisPlusTest {
@Test
void queryWrapperTest() {
String propertyName = "createTime";
String columnName = getAnnotationValue(, propertyName, );
("Database column name for '" + propertyName + "' is: " + columnName);
String condition = getAnnotationValue(, propertyName, , "description");
("@Schema description is: " + condition);
}
/**
* based on a string,Find the corresponding property,gainAnnotationvalue of
*
* @param clazz addressee
* @param propertyName Looked For Attributes
* @param annotationClass comment categories
* @param methodNames comment categories的方法
* @param <T>
* @return
*/
public static <T extends Annotation> String getAnnotationValue(Class<?> clazz, String propertyName, Class<T> annotationClass, String... methodNames) {
try {
// First check the fields in the current class -- 仅能gain类本身的属性成员(private、have altogether、safeguard)
Field[] declaredFields = ();
for (Field field : declaredFields) {
if (().equals(propertyName)) {
T annotation = (annotationClass);
if (annotation != null) {
// It is assumed here that the annotation has a name "value" method of the String typology
// You'll need to adjust this to the actual annotation definition
valueMethod = ( > 0 ? methodNames[0] : "value");
return (String) (annotation);
}
}
}
// If not found in the current class,Just check the parent class -- 在gain父类的私有属性时,by means ofgetSuperclassand then through thegetDeclaredFiledgain。
Class<?> superclass = ();
while (superclass != null) {
Field[] superDeclaredFields = ();
for (Field field : superDeclaredFields) {
if (().equals(propertyName)) {
T annotation = (annotationClass);
if (annotation != null) {
valueMethod = ( > 0 ? methodNames[0] : "value");
return (String) (annotation);
}
}
}
superclass = (); // Recursive search for parent class
}
} catch (Exception e) {
();
}
return null; // If no corresponding annotation or property is found,come (or go) backnull
}
}