一、一对多、多对一
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(); } }