我正在使用 Spring 将我的 cassandra 表映射到 pojo。
@Table(value="student_data")
public class StudentTable
{
@PrimaryKey
private String primaryKey;
@Column(value="column_name1")
private String columnName1;
@Column(value="column_name2")
private String columnName2;
@Column(value="column_name3")
private String columnName3;
/*
getters and setters
*/
}
现在,我从数据库获取数据并创建一个 StudentTable 列表来存储数据。 cassandraOperations.select(select, StudentTable.class)
我想根据列名获取值。 就像获取特定 StudentTable 对象的 column_name1 的值一样。
最佳答案
您可以尝试使用org.apache.commons.lang3.reflect.FieldUtils
获取您想要的字段的值通过 @column
过滤此类的字段注释。
例如:
public void testStudent() throws IllegalArgumentException, IllegalAccessException {
StudentTable student = new StudentTable();
student.setColumnName1("Student 1 Name");
String columnValue = null;
Field[] fields = org.apache.commons.lang3.reflect.FieldUtils.getAllFields(StudentTable.class);
for(Field f : fields) {
Column columnAnnotation = f.getAnnotation(Column.class);
if(columnAnnotation != null && StringUtils.equals(columnAnnotation.name(), "column_name1")) {
f.setAccessible(true);
columnValue = (String) f.get(student);
}
}
System.out.println(columnValue);
}
打印结果:
学生 1 姓名
关于java - 使用@Column获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43677705/