一、一对多、多对一

  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. AtomicInteger例子

    AtomicInteger可以保证原子性,可见性,有序性 public class AtomicIntegerTest { private static AtomicInteger value = n ...

  2. 使用jedis操作redis常用方法

    在redis入门及在商城案例中的使用中简单介绍了下使用jedis如何操作redis,但是其实方法是跟redis的操作大部分是相对应的.我这里做下记录 1.String类型操作 public class ...

  3. Neo4j图数据库从入门到精通(转)

    add by zhj: 转载时,目录没整理好,还会跳转到原文 其实RDB也可以存储多对多的关系,使用的是中间表,GDB使用的是边,RDB中的实体存储在数据表,而GDB存储在节点.两者使用的底层技术不同 ...

  4. 【2】hexo+github搭建个人博客的简单使用

    使用hexo+github搭建一个可以外网访问的个人博客,此文用于记录博客初级的使用方法. 新建-编写-生成-部署文章的全过程 1.使用cmd完成 打开命令提示符[win+r输入cmd] 切换到自己本 ...

  5. JavaIO学习:字符流

    JavaIO流之字符流 字符流 Reader InputStreamReader FileReader:专门用于处理文件的字符读取流对象. Writer OutputStreamWriter File ...

  6. EF Core 简单使用介绍

    EF Core 是一个ORM(对象关系映射),它使 .NET 开发人员可以使用 .NET对象操作数据库,避免了像ADO.NET访问数据库的代码,开发者只需要编写对象即可. EF Core 支持多种数据 ...

  7. Java NIO学习系列一:Buffer

    前面三篇文章中分别总结了标准Java IO系统中的File.RandomAccessFile.I/O流系统,对于I/O系统从其继承体系入手,力求对类数量繁多的的I/O系统有一个清晰的认识,然后结合一些 ...

  8. RabbitMQ如何实现高可用

    RabbitMQ一共具有三种模式:单机.普通集群.镜像集群 单机模式 单机模式,就是我们平常玩的demo,生产上肯定不能用.具体安装部署过程可以参考我的这篇文章:CentsOS原生RabbitMQ安装 ...

  9. 用node发布一个包

    手把手教你用npm发布一个包 注:本文引用于简书 http://www.jianshu.com/p/36d3e0e00157   但是内容的话,还是一样的,也就是继续之前的工作,将那个autoRout ...

  10. sql server 大数据, 统计分组查询,数据量比较大计算每秒钟执行数据执行次数

    -- 数据量比较大的情况,统计十分钟内每秒钟执行次数 ); -- 开始时间 ); -- 结束时间 declare @num int; -- 结束时间 set @begintime = '2019-08 ...