现在我已经从ios转行为java了呦吼吼吼~~~
1.先来一张界面,项目的字段太多了,为了研究知识点我做了个潘长江版的
Insert title here
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 Listeducations = 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; }}
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; }}
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{}
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); }}
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"; }}
5.完成!
点击提交:
(这是一张截图啦~)
ps:如果修改页面中的角标再提交会在数据库插入若干条空的信息。所以表单需要在controller里处理一下。另外页面还可以用thymeleaf实现,看心情补充~~~~