博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring实现一对多表单的保存
阅读量:5096 次
发布时间:2019-06-13

本文共 4405 字,大约阅读时间需要 14 分钟。

现在我已经从ios转行为java了呦吼吼吼~~~

1.先来一张界面,项目的字段太多了,为了研究知识点我做了个潘长江版的

Insert title here    
姓名:
身份证号:
教育经历:
form

2.Person、Educa两个model

Person中@OneToMany括号中的cascade是级联,用来管理如何存储Person与Educa表

package com.example.model;import java.util.ArrayList;import java.util.List;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;@Entitypublic class Person {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    @Column(name = "id")    protected Long id;    @Column    private String idCard;// 身份证号    @Column    private String name;// 姓名    @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)    @JoinColumn(name = "person_id")    private List
educations = new ArrayList
();// 教育经历 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List
getEducations() { return educations; } public void setEducations(List
educations) { this.educations = educations; }}
Person
package com.example.model;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;@Entitypublic class Educa {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    @Column(name = "id")    protected Long id;    @ManyToOne    @JoinColumn(name = "person_id")    private Person person;// 所属人物    @Column    private String school;// 学校    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public Person getPerson() {        return person;    }    public void setPerson(Person person) {        this.person = person;    }    public String getSchool() {        return school;    }    public void setSchool(String school) {        this.school = school;    }}
Educa

3.利用jpa的save方法

声明PersonRepository的时候一定要加注释,注意命名规范,要同名小写

package com.example.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.example.model.Person;public interface PersonRepository extends JpaRepository
{}
PersonRepository
package com.example.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.example.model.Person;import com.example.repository.PersonRepository;@Servicepublic class PersonService {    @Autowired    private PersonRepository personRepository;    public void save(Person person) {        personRepository.save(person);    }}
PersonService

4.controller

同样声明的时候要注意注释和命名规范

package com.example;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import com.example.model.Person;import com.example.service.PersonService;@Controllerpublic class PersonController {    @Autowired    private PersonService personService;    @RequestMapping    public String form() {        return "form";    }    @RequestMapping("/success")    public String submit(@ModelAttribute Person person) {        personService.save(person);        return "success";    }}
PersonController

5.完成!

点击提交:

(这是一张截图啦~)

ps:如果修改页面中的角标再提交会在数据库插入若干条空的信息。所以表单需要在controller里处理一下。另外页面还可以用thymeleaf实现,看心情补充~~~~

转载于:https://www.cnblogs.com/brook1223/p/5026394.html

你可能感兴趣的文章
位操作实现加减乘除
查看>>
ie6 兼容
查看>>
教你记住ASP.NET WebForm页面的生命周期
查看>>
数据结构强化1
查看>>
在NT中直接访问物理内存
查看>>
Intel HEX 文件格式
查看>>
SIP服务器性能测试工具SIPp使用指导(转)
查看>>
Centos Development Tools 安装
查看>>
vue form 验证
查看>>
php_扑克类
查看>>
回调没用,加上iframe提交表单
查看>>
socket总结
查看>>
(安卓)一般安卓开始界面 Loding 跳转 实例 ---亲测!
查看>>
元素和为目标值的子矩阵数量
查看>>
POJ-1287.Network(Kruskal + Prim + Prim堆优化)
查看>>
Mysql 索引优化 - 1
查看>>
LeetCode(3) || Median of Two Sorted Arrays
查看>>
JSDoc规范
查看>>
ssh命令
查看>>
数据库中事务的浅析
查看>>