51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Instead of id name needs to be display spring boot

英文:

Instead of id name needs to be display spring boot

问题 {#heading}

我有一个Spring Boot应用程序,用于保存和检索任务。在检索时,我想显示学生姓名而不是学生ID。
我已经附上了我的当前代码。这是Postman中响应的格式。

{
    "taskid": 4,
    "taskname": "taskkk",
    "description": "ssstaskkk",
    "complete": true,
    "studentId": 1
}

TaskController

@GetMapping(path="/getAllTasks")
public List<TaskDTO> getAllTasks()
{
    List<TaskDTO> allTasks = taskService.getAllTasks();
    System.out.println(allTasks.size());
    System.out.println(allTasks);
    return allTasks;
}

TaskService

List<TaskDTO> getAllTasks();

TaskServiceIMPL

@Override
public List<TaskDTO> getAllTasks() {

    List<Task> getTasks = taskRepo.findAll();

    System.out.println(getTasks);

    List<TaskDTO> taskDTOList = new ArrayList<>();

    for(Task t:getTasks)
    {
        TaskDTO taskDTO = new TaskDTO(
                t.getTaskid(),
                t.getTaskname(),
                t.getDescription(),
                t.getComplete(),
                t.getStudent().getStudentid()
        );
        taskDTOList.add(taskDTO);
    }
    return taskDTOList;
}

Task

public class Task {

    @Id
    @Column(name = "task_id", length = 50)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long taskid;

    @Column(name = "task_name")
    private String taskname;

    @Column(name = "description")
    private String description;

    @Column(name = "complete")
    private Boolean complete;

    @OneToOne
    @JoinColumn(name = "student_id")
    private Student student;
}

TaskRepo

public interface TaskRepo extends JpaRepository<Task,Long >
{
    
}

Student Entity

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="student_id", length = 50)
private Long  studentid;

@Column(name="student_name", length = 50)
private String studentname;

@Column(name="address", length = 70)
private String address;

@Column(name="mobile", length = 12)
private int mobile;

@Column(name="activestate", columnDefinition = "TINYINT default 1")
private boolean activestate;

Student DTO

public class StudentDTO {
    private Long studentid;
    private String studentname;
    private String address;
    private int mobile;
    private boolean activestate;
}

英文:

I have a Spring Boot application which saves and retrieves tasks. During the retrieval, I want to display student name instead of student id.
I have attached my current code below. This is what the response in Postman looks like this format.

  {
        &quot;taskid&quot;: 4,
        &quot;taskname&quot;: &quot;taskkk&quot;,
        &quot;description&quot;: &quot;ssstaskkk&quot;,
        &quot;complete&quot;: true,
        &quot;studentId&quot;: 1
    }

TaskController

@GetMapping(path=&quot;/getAllTasks&quot;)
    public List&lt;TaskDTO&gt; getAllTasks()
    {
        List&lt;TaskDTO&gt; allTasks = taskService.getAllTasks();
        System.out.println(allTasks.size());
        System.out.println(allTasks);
        return allTasks;
    }

TaskService

  List&lt;TaskDTO&gt; getAllTasks();

TaskServiceIMPL

@Override
public List&lt;TaskDTO&gt; getAllTasks() {

    List&amp;lt;Task&amp;gt; getTasks = taskRepo.findAll();

    System.out.println(getTasks);

    List&amp;lt;TaskDTO&amp;gt; taskDTOList = new ArrayList&amp;lt;&amp;gt;();

    for(Task t:getTasks)
    {
        TaskDTO taskDTO = new TaskDTO(
                t.getTaskid(),
                t.getTaskname(),
                t.getDescription(),
                 t.getComplete(),
                t.getStudent().getStudentid()
        );
        taskDTOList.add(taskDTO);
    }
    return taskDTOList;



`}
`

Task

public class Task {

    @Id
    @Column(name = &amp;quot;task_id&amp;quot;, length = 50)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long taskid;

    @Column(name = &amp;quot;task_name&amp;quot;)
    private String taskname;

    @Column(name = &amp;quot;description&amp;quot;)
    private String description;

    @Column(name = &amp;quot;complete&amp;quot;)
    private Boolean complete;

    @OneToOne
    @JoinColumn(name = &amp;quot;student_id&amp;quot;)
    private Student student;



`}
`

TaskRepo

public interface TaskRepo extends JpaRepository&lt;Task,Long &gt;
{
`}
`

Student Entity

@Id
@GeneratedValue(strategy =GenerationType.IDENTITY)
@Column(name=&quot;student_id&quot;, length = 50)
private Long  studentid;

@Column(name=\&quot;student_name\&quot;, length = 50)
private String studentname;


@Column(name=\&quot;address\&quot;, length = 70)
private String address;


@Column(name=\&quot;mobile\&quot;, length = 12)
private int mobile;

`@Column(name=&quot;activestate&quot;, columnDefinition = &quot;TINYINT default 1&quot;)
private boolean activestate;
`

Student DTO

public class StudentDTO {
    private Long studentid;
    private String studentname;
    private String address;
    private int mobile;
    private boolean activestate;
}

答案1 {#1}

得分: 1

从您的TaskController中,您返回了一个TaskDTO对象的列表。
为了显示学生的名称,您需要在您的TaskDTO类内部添加一个StudentDTO类。您的TaskDTO类应该像下面这样:

修改后的TaskDTO:

public class TaskDTO {
    private Long taskid;
    private String taskname;
    private String description;
    private Boolean complete;
    private StudentDTO student; // 在这里使用StudentDTO,同时添加构造函数、getter和setter方法。
}

现在,您需要更新您的TaskServiceIMPL以创建带有StudentDTO的TaskDTO对象。

修改后的TaskServiceIMPL:

@Override
public List<TaskDTO> getAllTasks() {
    List<Task> getTasks = taskRepo.findAll();

    List<TaskDTO> taskDTOList = new ArrayList<>();

    for (Task t : getTasks) {
        Student student = t.getStudent(); // 获取关联的学生
        StudentDTO studentDTO = new StudentDTO(
            student.getStudentid(),
            student.getStudentname(),
            student.getAddress(),
            student.getMobile(),
            student.isActivestate()
        );

        TaskDTO taskDTO = new TaskDTO(
            t.getTaskid(),
            t.getTaskname(),
            t.getDescription(),
            t.getComplete(),
            studentDTO // 在这里设置StudentDTO对象
        );

        taskDTOList.add(taskDTO);
    }
    return taskDTOList;
}

现在,您的TaskController应该返回修改后的TaskDTO。 英文:

From you TaskController, you are returning a list of TaskDTO objects.
In order to display student names, you need to add StudentDTO class inside your TaskDTO class. Your TaskDTO class should be like below:

Modified TaskDTO:

public class TaskDTO {
    private Long taskid;
    private String taskname;
    private String description;
    private Boolean complete;
    private StudentDTO student; // Use StudentDTO here along 
                                  with it&#39;s constructor,getters,setters.
}

Now, you need to update your TaskServiceIMPL to create TaskDTO objects with StudentDTO.

Modified TaskServiceIMPL:

@Override
public List&lt;TaskDTO&gt; getAllTasks() {
    List&lt;Task&gt; getTasks = taskRepo.findAll();

    List&amp;lt;TaskDTO&amp;gt; taskDTOList = new ArrayList&amp;lt;&amp;gt;();

    for(Task t : getTasks) {
        Student student = t.getStudent(); // Get the associated student
        StudentDTO studentDTO = new StudentDTO(
            student.getStudentid(),
            student.getStudentname(),
            student.getAddress(),
            student.getMobile(),
            student.isActivestate()
        );
        
        TaskDTO taskDTO = new TaskDTO(
            t.getTaskid(),
            t.getTaskname(),
            t.getDescription(),
            t.getComplete(),
            studentDTO // Set the StudentDTO object here
        );
        
        taskDTOList.add(taskDTO);
    }
    return taskDTOList;




}

Now, your TaskController should return the modified TaskDTO


赞(1)
未经允许不得转载:工具盒子 » Instead of id name needs to be display spring boot