一、一对多、多对一

  1、Country实体类

    

  2、City实体类

    

  3、CountryDao层

    

  4、CityDao层

    

  5、Controller

package com.zn.controller;

import com.zn.dao.CityDao;
import com.zn.dao.CountryDao;
import com.zn.entity.City;
import com.zn.entity.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class CountryController {
@Autowired
CountryDao countryDao;
CityDao cityDao; //级联添加
@RequestMapping("/OneToMany")
@ResponseBody
public String AddCountry(){
Country country1=new Country();
country1.setCountry_name("中国");
City city1=new City();
city1.setCity_name("中国香港");
City city2=new City();
city2.setCity_name("中国台湾"); //维护国家与城市的一对多关系
country1.getCitys().add(city1);
country1.getCitys().add(city2);
countryDao.save(country1);
return "SUCCESS";
} //关联查询
@RequestMapping("/getCountry")
@ResponseBody
public Object getCountry(){
return countryDao.findAll();
} //级联删除
@RequestMapping("/deleteCountry")
@ResponseBody
public String deleteCountry(){
//检索国家实体
Country one=countryDao.getOne(5);
countryDao.delete(one);
return "SUCCESS";
} //由城市到国家的关联查询
@RequestMapping("/getCity")
@ResponseBody
public Object getCity(){
return countryDao.findAll();
} }

二、多对多

  1、TStudent实体类

    

  2、Teacher实体类   

    

  3、Stu_TeaDao

    

   4、Tea_StuDao

    

  5、Controller

package com.zn.controller;

import com.zn.dao.Stu_TeaDao;
import com.zn.dao.Tea_StuDao;
import com.zn.entity.TStudent;
import com.zn.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.Arrays;
import java.util.List; @Controller
public class Stu_TeaController { @Autowired
Stu_TeaDao stu_teaDao; @Autowired
Tea_StuDao tea_stuDao; //添加学生和老师
@RequestMapping("/addstu")
@ResponseBody
public String addstu(){
TStudent student1=new TStudent("张三");
TStudent student2=new TStudent("李四");
TStudent student3=new TStudent("王五"); Teacher teacher1=new Teacher("小王子");
student1.getTeachers().add(teacher1);
student2.getTeachers().add(teacher1);
student3.getTeachers().add(teacher1); stu_teaDao.saveAll(Arrays.asList(student1,student2,student3));
return "SUCCESS";
} //多对多添加老师
@RequestMapping("/addTea")
@ResponseBody
public String addTea(){
Teacher teacher=new Teacher("王老师");
List<TStudent> all = stu_teaDao.findAll();
teacher.getStudents().addAll(all);
tea_stuDao.save(teacher);
return "SUCCESS";
} //多对多关联查询(慎用!!死循环!!)
@RequestMapping("/getTea")
@ResponseBody
public Object getTea(){
return tea_stuDao.getOne(1);
}
}

最新文章

  1. IP 地址分类(A、B、C、D、E类)
  2. Arduino 极速入门系列–1 点亮 LED
  3. Camera中对焦模式总结
  4. facedetect
  5. [Redux] Fetching Data on Route Change
  6. Windows 中默认安装的.Net 版本
  7. windows上putty访问ubuntu
  8. JAVA单元测试Junit
  9. 设置ListView的item多选
  10. [原]iOS中 Web 页面与 Native Code 的一种通信方式
  11. 2.动手实操Apache ZooKeeper
  12. Ruby 2.x 命名参数特性简介
  13. Git之生成SSH公钥
  14. 【转】通过js获取系统版本以及浏览器版本
  15. tomcat配置说明,配置不用访问工程名
  16. 从源代码解释Android事件分发机制
  17. Hexo初体验
  18. centos crontab 计划任务 设置与查看
  19. 20169205实验四 Android程序设计
  20. 【LeetCode】76. Minimum Window Substring

热门文章

  1. [考试反思]0729NOIP模拟测试10
  2. NOIP模拟27
  3. P3052 [USACO12MAR]摩天大楼里的奶牛(迭代加深搜索)
  4. Python 定义动态变量
  5. 更新linux时候提示“由于没有公钥,无法验证下列签名&quot;.
  6. javascript腾讯地图放到网页中的方法
  7. Linux\CentOS Tomcat 配置
  8. MD5 加盐加密
  9. 【python测试开发栈】python基础语法大盘点
  10. 记录一次在Github写博客时的报错和解决方法