标签:repo nta class body teacher 国家 getc asc 中国
@Entity @Table(name = "Country") public class Country { @Id //sequence identity @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer countryid; private String country; //mappedBy:指定由谁维护关联关系(设置的是关联对象的属性名) @OneToMany(cascade = CascadeType.ALL,mappedBy = "country") //@JoinColumn(name="countryid") private List<City> citys=new ArrayList<>(); //关联属性
@Entity @Table(name = "City") public class City { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer cityid; private String cityname; @ManyToOne(cascade = CascadeType.ALL) private Country country;
public interface CountryDao extends JpaRepository<Country,Integer>{ }
public interface CityDao extends JpaRepository<City,Integer>{ }
@Controller public class CountryController { @Autowired CountryDao dao; @Autowired CityDao dao1; @Autowired StudentDao dao2; @Autowired TeacherDao dao3; //级联增加 @RequestMapping("/onetomany") public String onetomany(){ Country c1=new Country(); c1.setCountry("中国万岁 ChinaNo.1"); City ct1=new City(); ct1.setCityname("中国香港"); City ct2=new City(); ct2.setCityname("中国台湾"); //维护国家与城市的一对多关系 c1.getCitys().add(ct1); c1.getCitys().add(ct2); dao.save(c1); return "success"; } //关联查询 @RequestMapping("/getcountry") @ResponseBody public Object get(){ return dao.findAll(); } //级联删除 @RequestMapping("/deleteCountry") public String delete(){ //检索国家实体 Country one = dao.getOne(1); dao.delete(one); return "success"; } //由城市到国家的关联查询 @RequestMapping("/getCity") @ResponseBody public Object getCity(){ return dao1.findAll(); } //添加学生和老师 @RequestMapping("/saveStudent") public String save(){ Student student1=new Student("利亚东哥"); Student student2=new Student("玉姐"); Student student3=new Student("雄哥"); Teacher t1=new Teacher("山间的风"); student1.getTeachers().add(t1); student2.getTeachers().add(t1); student3.getTeachers().add(t1); dao2.saveAll(Arrays.asList(student1,student2,student3)); return "success"; } @RequestMapping("/saveTeacher") public String saveTeacher(){ Teacher t1=new Teacher("帅的离谱"); List<Student> all = dao2.findAll(); t1.getStudents().addAll(all); dao3.save(t1); return "success"; } @RequestMapping("/getTeacher") @ResponseBody public Object getTeacher(){ return dao3.getOne(1); } }
@Entity @Table(name="teacherinfo") public class Teacher { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer teacherid; private String teachername; @ManyToMany(cascade = CascadeType.ALL) //mappedby属性绝对不能再存在@JoinTable和@JoinColumn注解时使用 @JoinTable(name="student_teacher",joinColumns =@JoinColumn(name="teacherid") , inverseJoinColumns =@JoinColumn(name="studentid") ) private List<Student> students=new ArrayList<>();
@Entity @Table(name = "studentinfo") public class Student { @Id @GeneratedValue private Integer id; private String name; private Integer age; private Integer sex; @Column(name = "stu_name")
public interface TeacherDao extends JpaRepository<Teacher,Integer> { }
public interface StudentDao extends JpaRepository<Student,Integer>{ }
@Controller public class StudentController{ @Autowired StudentDao studentDao; @Autowired TeacherDao teacherDao; //添加学生和老师 @RequestMapping("/addstu") @ResponseBody public String addstu(){ Student student1=new Student("丽丽"); Student student2=new Student("明明"); Student student3=new Student("安安"); Teacher teacher1=new Teacher("筱丽"); student1.getTeachers().add(teacher1); student2.getTeachers().add(teacher1); student3.getTeachers().add(teacher1); studentDao.saveAll(Arrays.asList(student1,student2,student3)); return "SUCCESS"; } //多对多添加老师 @RequestMapping("/addDom") @ResponseBody public String addDom(){ Teacher teacher=new Teacher("李老师"); List<Student> all = studentDao.findAll(); teacher.getStudents().addAll(all); teacherDao.save(teacher); return "SUCCESS"; } //多对多关联查询(慎用!!死循环!!) @RequestMapping("/getDom") @ResponseBody public Object getDom(){ return teacherDao.getOne(1); } }
标签:repo nta class body teacher 国家 getc asc 中国
原文地址:https://www.cnblogs.com/dabrk/p/12002926.html