[英]How to return fields in Spring Boot that is not present in the model?
So I have a use case where I want to return a particular attribute for all the rows of a table.所以我有一个用例,我想为表的所有行返回一个特定的属性。 But the attribute does not exist in the table.
但是该属性在表中不存在。 It should be generated on the fly.
它应该是动态生成的。
For example - A table called Student
- has the following attribute * ID * Name例如 - 一个名为
Student
的表 - 具有以下属性 * ID * Name
Now, when I use the repository for this model class, I want to return all the rows of data including ID
and Name
but also append another property called URL
along with each row.现在,当我使用这个模型类的存储库时,我想返回所有数据行,包括
ID
和Name
但还要附加另一个名为URL
属性以及每一行。 So output will be -所以输出将是 -
The data for the URL
can be generated on the fly - say = "example.com/"+ID
URL
的数据可以即时生成 - 例如 = "example.com/"+ID
How shall I proceed ?我将如何进行?
The same model/Repository/Service class for this entity are as follows -此实体的相同模型/存储库/服务类如下 -
Entity Student实体学生
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class StudentDetail {
@Id
private Long ID;
private String Name;
}
Repository存储库
@Repository
public interface StudentDetail extends JpaRepository<StudentDetail , Integer> {
}
Service服务
@Override
public List<StudentDetail> getStudentDetails() {
List<StudentDetail> studentDetail = studentDetailsRepository.findAll();
return studentDetail;
}
The service will return all the rows with only ID
and Name
attribute.该服务将返回所有只有
ID
和Name
属性的行。 How do I calculate the URL parameter in repository ?如何计算存储库中的 URL 参数?
You can add a transient field (it means, it won't be mapped to any column and persisted):您可以添加一个瞬态字段(这意味着它不会被映射到任何列并持久化):
public class StudentDetail {
@Id
private Long ID;
private String Name;
@Transient
private String url = // Here comes your implementation
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.