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

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

<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<script type="text/javascript">
    var i = 1;
    function addRow(){
        var br = document.createElement('br');
        var text = document.createElement('input');
        text.setAttribute("name", "educations[" + i++ + "].school");
        text.setAttribute("type", "text");
        
        document.getElementById("school").appendChild(br);
        document.getElementById("school").appendChild(text);
    }
</script>
</head>
<body>

    <form action="/success" method="post">
        <table border="1">
            <tr>
                <td>姓名:</td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td>身份证号:</td>
                <td><input type="text" name="idCard"/></td>
            </tr>
            <tr>
                <td>教育经历:</td>
                <td>
                    <div id="school">
                        <input type="text" name="education[0].school"/>
                    </div>
                        <input type="button" value="添加学校" onclick="addRow()"/>
                </td>
            </tr>
        </table>
        <input type="submit" value="提交"/>
    </form>

</body>
</html>
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;

@Entity
public 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<Educa> educations = new ArrayList<Educa>();// 教育经历

    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<Educa> getEducations() {
        return educations;
    }

    public void setEducations(List<Educa> 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;

@Entity
public 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<Person, Long> {

}
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;

@Service
public 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;

@Controller
public 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实现,看心情补充~~~~