一、一对多、多对一

  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. scala中的Option

    Scala中Option是用来表示一个可选类型 什么是可选? --> 主要是指 有值(Some) 和 无值(None)-->Some和None是Option的子类 val myMap:Ma ...

  2. Java学习:反射机制简介

    反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性: 这种动态获取的信息以及动态调用对象的方法的功能称为ja ...

  3. 我是如何一步步编码完成万仓网ERP系统的(七)产品库设计 3.品牌图片跨域上传

    https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...

  4. C++中Matrix(矩阵)的基本运算( +、-、=、<<)

    利用二维指针开辟空间形成二维数组: 原题为设计一个Matrix类,实现基本的矩阵运算: 初次设计为HL[10][10]数组,存放矩阵元素,后改为二维指针: 主要问题存在于二维指针理解的不透彻,无法理解 ...

  5. 获取Object对象属性的方法,Reflect.ownKeys, Object.getOwnPropertyNames,Object.getOwnPropertySymbols,Object.keys,for in

    let triangle={ a:1, b:2, c:3 } function coloTriangle(){ this.color='red'; } coloTriangle.prototype=t ...

  6. qt 子窗口内嵌到父窗口

    类声明 动态申请子窗口类对象 ClassA *a = new ClassA(this); 隐藏边框 a->setWindowFlags(Qt::CustomizeWindowHint|Qt::F ...

  7. Go 语言基础语法-Go

    Go 标记 Go 程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号.如以下 GO 语句由 6 个标记组成: fmt.Println("Hello, World!") ...

  8. WorkFlow三:配BO对象,事件触发工作流

    1.新建个BO对象的字段. 2.新建取数函数: 3.运行事物代码SWO1新建BO对象. 4.新建关键字段: 5.新建BO对象的事件: 6.添加处理方法: 6.调整对象状态,这里是本地对象,不需要释放, ...

  9. python基础-面向对象编程之多态

    面向对象编程之多态以及继承.抽象类和鸭子类型三种表现形式 多态 定义:同一种类型的事物,不同的形态 作用: 多态也称之为"多态性".用于在不知道对象具体类型的情况下,统一对象调用方 ...

  10. PostgreSQL SQL HINT的使用说明

    本文来自:http://www.023dns.com/Database_mssql/5974.html PostgreSQL优化器是基于成本的 (CBO) , (当然, 如果开启了GEQO的话, 在关 ...