繁体   English   中英

如何在spring boot中将数据从表复制到另一个?

[英]How do I copy data from on table to another in spring boot?

我目前正在制作一个应用程序,让您创建学生,然后标记他们缺席。 我希望能够通过将其添加到名为AbsentStudents的单独表中来实现此目的。 但是jparepository或crudreposotroy不给我这些选择。

我试图创建一个新实体,它是学生实体的复制品,然后使dao等于学生的findbyid。 它看起来像这样:

dao.equals(repo.findById(id));

的index.jsp:

<body>
<p> Add a student into the database:<p>
<form action ="addStudent">
    <input type = "text" name = "ID"><br>
    <input type = "text" name = "Name"><br>
    <input type = "text" name = "Teacher"><br>
    <input type = "submit">
</form>

<p> Mark a Student Absent<p>
<form action ="markAbsent">
    <input type = "text" name = "ID"><br>
    <input type = "submit">
</form>

</body>
</html>

然后是absentStudent,这与学生一样

@Entity
@Getter
@Setter
public class AbsentStudent
{
    @Id
    private int id;
    public int getId() {
        return id;
    }
}

然后我创建了学生和缺席的人。

最后,这是控制器。 我离开了自动装配。

@RequestMapping("/addStudent")
public String addStudent(Student student) {

    repo.save(student);
    return "index.jsp";

}

@RequestMapping("/markAbsent")
public ModelAndView markAbsent(@RequestParam int id) {
    ModelAndView mv = new ModelAndView();

    dao.equals(repo.findById(id));

    mv.setViewName("absent.jsp");
    mv.addObject(dao);



    return mv;

}

}

我最终期待一个页面,它将从数据库中获取所有缺席的学生,并将它们发布在一个页面上。 但是,我得到一个错误页面。

数据没有从学生复制到缺席的学生。

我希望能够通过将其添加到名为AbsentStudents的单独表中来实现此目的。 但是jparepository或crudreposotroy不给我这些选择。

也许dao(存储库)界面中的@Query注释将有助于解决您的问题( https://www.baeldung.com/spring-data-jpa-query

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM