一、一对多、多对一

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

SpringData JPA一对多多对一多对多关联的更多相关文章

  1. 【Jpa hibernate】一对多@OneToMany,多对一@ManyToOne的使用

    项目中使用实体之间存在一对多@OneToMany,多对一@ManyToOne的映射关系,怎么设置呢? GitHub地址:https://github.com/AngelSXD/myagenorderd ...

  2. JPA 一对多、多对一注解

    1. @OneToMany @OneToMany 是属性或方法级别的注解,用于定义源实体与目标实体是一对多的关系. 参数 类型 描述 targetEntity Class 源实体关联的目标实体类型,默 ...

  3. JPA 系列教程7-双向多对多

    双向多对多的ddl语句 同单向多对多表的ddl语句一致 Student package com.jege.jpa.many2many; import java.util.HashSet; import ...

  4. springData Jpa 快速入门

    前言: 数据持久化的操作,一般都要由我们自己一步步的去编程实现,mybatis通过我们编写xml实现,hibernate也要配置对应的xml然后通过创建session执行crud操作.那么有没有这样一 ...

  5. 【持久层框架】- SpringData - JPA

    SpringData - JPA 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! JPA简 ...

  6. django 中models表的多对一,多对多的理解

    django 表的理解 好处:设计的好,会清晰,易于理解和维护,后期开发事半功倍,一目了然. 1. 一对一的表,两表的属性实际上完全可以合并成一个表,共用一个主键即可: 2. 一对多的表,可以设中间关 ...

  7. JPA一对多关联

    关于JPA一对多关联这里使用Order与OrderItem来模拟.一个Order可以关联多个OrderItem,而一个OrderItem只能关联一个Order.Order与OrderItem是一对多的 ...

  8. Spring、SpringMVC、SpringData + JPA 整合详解

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...

  9. 【极简版】SpringBoot+SpringData JPA 管理系统

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在上一篇中已经讲解了如何从零搭建一个SpringBo ...

随机推荐

  1. ReentrantLock和Condition实现生产者和消费者

    一个生产者和一个消费者 public class ConditionTest { private static ReentrantLock lock = new ReentrantLock(); pr ...

  2. 23 Maven工程module的移除和重新导入

    1.移除module 移除后: 点击右侧的maven projects: 2.重新导入刚才移除的module (1)方法1 (2)方法2 Ctrl+Shift+ALT+S的快捷键 选择modules ...

  3. Spring Boot 如何干掉 if else?

    需求 这里虚拟一个业务需求,让大家容易理解.假设有一个订单系统,里面的一个功能是根据订单的不同类型作出不同的处理. 订单实体: service接口: 传统实现 根据订单类型写一堆的if else: 策 ...

  4. 4-5 Scrapy知识补充

    FormRequest FormRequest类是专门用来处理HTML表单的,同时对隐藏的表单处理也很方便.适合用来完成登录操作. 类原型:class scrapy.http.FormRequest( ...

  5. pod的yaml说明

    apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...

  6. win10系统优化

    1.服务:关闭 windows update\ windows search \ windows 备份 2.系统:高级系统设置->性能最优,同事修改虚拟内存如下 3.任务管理器:关闭多余的启动项 ...

  7. vue数据修改不同步更新的问题解决方案

    最近在做一个组件封装的功能,用到父组件将数组(this.DataSource,this.DefaultItem)传到给子组件,子组件接受该数组来进行添加数组和删除数组的操作.因为子组件无法直接修改父组 ...

  8. NetCoreApi框架搭建三、AutoFac 依赖入注)

    这里不多做理论上的解释,因为我感觉自己也不是很完全的理解,所以只是记录我自己做的过程. 首先还是粘贴大神的链接:https://www.cnblogs.com/RayWang/p/11165509.h ...

  9. 活动任务出现bug

    之前做的一个活动任务发现一个bug,是以前和离职同事一起对逻辑的时候没有考虑到的,配置活动的时候应该要先查询下,如果这个产品线上在这段时间已经配置了并且上线了,则不能在做活动处理了,否则就和前面的活动 ...

  10. vue自学笔记

      做前端也做了一段时间了,为了高薪,不能一直做网页不是~~,所以从今天开始整理vue的笔记 内容都是从网上搜集整合并且自己实践过了的,需要注意的点,也在后面标注了“注”   当然了,如果有什么问题的 ...