ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringData JPA一对多多对一多对多关联

2019-12-08 18:51:08  阅读:856  来源: 互联网

标签:zn SpringData JPA ResponseBody 一多 import com public RequestMapping


一、一对多、多对一

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


}
View Code

二、多对多

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

 

 

 

标签:zn,SpringData,JPA,ResponseBody,一多,import,com,public,RequestMapping
来源: https://www.cnblogs.com/mayuan01/p/12006673.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有