Java项目学生管理系统六后端补充

班级管理

1 班级列表:后端

  • 编写JavaBean【已有】
  • 编写Mapper【已有】
  • 编写Service
  • 编写controller

在这里插入图片描述

  • 编写Service

    • 接口

      package com.czxy.service;
      
      import com.czxy.domain.Classes;
      
      import java.util.List;
      
      /**
       * @author 桐叔
       * @email liangtong@itcast.cn
       * @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 liangtong@itcast.cn
       * @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 liangtong@itcast.cn
     * @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);
        }
    }
    
    

城市管理

1 查询所有城市:后端

1 JavaBean

在这里插入图片描述

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 liangtong@itcast.cn
 * @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'
);
 */

2 Mapper

在这里插入图片描述

package com.czxy.mapper;

import com.czxy.domain.City;
import tk.mybatis.mapper.common.Mapper;

/**
 * @author 桐叔
 * @email liangtong@itcast.cn
 * @description
 */
public interface CityMapper extends Mapper<City> {
}

3 Service

在这里插入图片描述

  • 接口

    package com.czxy.service;
    
    import com.czxy.domain.City;
    
    import java.util.List;
    
    /**
     * @author 桐叔
     * @email liangtong@itcast.cn
     * @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 liangtong@itcast.cn
     * @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;
        }
    }
    
    

4 Controller

在这里插入图片描述

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 liangtong@itcast.cn
 * @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的所有城市:后端

1 Service

  • 接口

    在这里插入图片描述

        /**
         * 通过父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;
        }
    

2 Controller

在这里插入图片描述

    /**
     * 通过父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);
    }

课程管理

1查询所有:后端

在这里插入图片描述

1 Service

  • 接口

    package com.czxy.service;
    
    import com.czxy.domain.Course;
    
    import java.util.List;
    
    /**
     * @author 桐叔
     * @email liangtong@itcast.cn
     * @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 liangtong@itcast.cn
     * @description
     */
    @Service
    @Transactional
    public class CourseServiceImpl implements CourseService {
        @Resource
        private CourseMapper courseMapper;
        @Override
        public List<Course> selectAll() {
            List<Course> courseList = courseMapper.selectAll();
            return courseList;
        }
    }
    
    

2 Controller

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 liangtong@itcast.cn
 * @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);
    }
}

2 查询指定学生的所有课程:后端

在这里插入图片描述

1 Mapper【已有】

2 Service

  • 接口

    在这里插入图片描述

        /**
         * 查询指定学生的所有课程
         * @param sid
         * @return
         */
        public List<Course> selectAllBySid(Integer sid);
    
  • 实现类

    在这里插入图片描述

        @Override
        public List<Course> selectAllBySid(Integer sid) {
            List<Course> courseList = courseMapper.selectAllBySid(sid);
            return courseList;
        }
    
  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛慕昭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值