编写Service
接口
package com.czxy.service;
import com.czxy.domain.Classes;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
public interface ClassesService {
/**
* 查询所有
* @return
*/
public List<Classes> selectAll();
}
实现类
package com.czxy.service.impl;
import com.czxy.domain.Classes;
import com.czxy.mapper.ClassesMapper;
import com.czxy.service.ClassesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@Service
@Transactional
public class ClassesServiceImpl implements ClassesService {
@Resource
private ClassesMapper classesMapper;
@Override
public List<Classes> selectAll() {
List<Classes> classesList = classesMapper.selectAll();
return classesList;
}
}
编写controller
package com.czxy.controller;
import com.czxy.domain.Classes;
import com.czxy.service.ClassesService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@RestController
@RequestMapping("/classes")
public class ClassesController {
@Resource
private ClassesService classesService;
@GetMapping
public ResponseEntity<List<Classes>> selectAll() {
// 查询
List<Classes> classesList = classesService.selectAll();
// 返回
return ResponseEntity.ok(classesList);
}
}
package com.czxy.domain;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@Table(name = "tb_city")
public class City {
@Id
@Column(name = "c_id")
private String cid; //城市ID
private String cityName; //城市名称
private String parentId; //父ID
//一对多:一个城市(省/市)拥有多个子城市(多个市/多个县) ,new对象为了操作【方便】
private List<City> children = new ArrayList<>();
//....
}
/*
CREATE TABLE tb_city(
c_id VARCHAR(32) PRIMARY KEY COMMENT '城市ID',
city_name VARCHAR(20) COMMENT '城市名称' ,
parent_id VARCHAR(32) COMMENT '父ID'
);
*/
package com.czxy.mapper;
import com.czxy.domain.City;
import tk.mybatis.mapper.common.Mapper;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
public interface CityMapper extends Mapper<City> {
}
接口
package com.czxy.service;
import com.czxy.domain.City;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
public interface CityService {
/**
* 查询所有(省、市、县)
* @return
*/
public List<City> selectAll();
}
实现类
package com.czxy.service.impl;
import com.czxy.domain.City;
import com.czxy.mapper.CityMapper;
import com.czxy.service.CityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@Service
@Transactional
public class CityServiceImpl implements CityService {
@Resource
private CityMapper cityMapper;
@Override
public List<City> selectAll() {
//1 条件查询:排序
Example example = new Example(City.class);
example.orderBy("parentId").asc(); //升序
//2 查询
List<City> cityList = cityMapper.selectByExample(example);
return cityList;
}
}
package com.czxy.controller;
import com.czxy.domain.City;
import com.czxy.service.CityService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@RestController
@RequestMapping("/city")
public class CityController {
@Resource
private CityService cityService;
@GetMapping
public ResponseEntity<List<City>> selectAll() {
// 1 查询所有 省/市/县
List<City> cityList = cityService.selectAll();
// 2 处理数据 省(市(县))
// 2.1.1 提供Map,用于缓存所有城市,目的:方便子城市找到父城市
// map.id 城市id,方便子获得, map.value 城市
Map<String, City> cacheMap = new HashMap<>();
// 2.1.2 提供List,存放所有的省
List<City> provinceList = new ArrayList<>();
// 2.2 遍历所有城市
for(City city: cityList) {
// 2.3.1 从map获得父城市
City parentCity = cacheMap.get(city.getParentId());
if(parentCity == null) {
// 2.3.2 1)如果没有获得父城市,表示就是省,直接添加List
provinceList.add(city);
} else {
// 2.3.2 2)如果获得父城市,表示市、县,将器追加到父城市的子列表中
parentCity.getChildren().add(city);
}
// 2.3.3 当前城市添加到map中,方便下一次自己的子城市可以获得自己
cacheMap.put(city.getCid(), city);
}
//3 返回所有的省
return ResponseEntity.ok(provinceList);
}
}
接口
/**
* 通过父id查询所有
* @param parentId
* @return
*/
public List<City> selectByParentId(String parentId);
实现类
@Override
public List<City> selectByParentId(String parentId) {
//1 条件
Example example = new Example(City.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("parentId", parentId);
//2 查询
List<City> cityList = cityMapper.selectByExample(example);
//3 返回
return cityList;
}
/**
* 通过父id查询
* @param pid
* @return
*/
@GetMapping("/parent/{pid}")
public ResponseEntity<List<City>> selectAllByParentId(@PathVariable("pid") String pid) {
//查询
List<City> cityList = cityService.selectByParentId(pid);
//返回
return ResponseEntity.ok(cityList);
}
接口
package com.czxy.service;
import com.czxy.domain.Course;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
public interface CourseService {
/**
* 查询所有课程
* @return
*/
public List<Course> selectAll();
}
实现类
package com.czxy.service.impl;
import com.czxy.domain.Course;
import com.czxy.mapper.CourseMapper;
import com.czxy.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@Service
@Transactional
public class CourseServiceImpl implements CourseService {
@Resource
private CourseMapper courseMapper;
@Override
public List<Course> selectAll() {
List<Course> courseList = courseMapper.selectAll();
return courseList;
}
}
package com.czxy.controller;
import com.czxy.domain.Course;
import com.czxy.service.CourseService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 桐叔
* @email [email protected]
* @description
*/
@RestController
@RequestMapping("/course")
public class CourseController {
@Resource
private CourseService courseService;
/**
* 查询所有课程
* @return
*/
@GetMapping
public ResponseEntity<List<Course>> selectAll() {
//查询
List<Course> courseList = courseService.selectAll();
//返回
return ResponseEntity.ok(courseList);
}
}