03 微架构教务系统——课程创建接口、课程更新接口、课程删除接口

导读:本篇文章讲解 03 微架构教务系统——课程创建接口、课程更新接口、课程删除接口,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1、控制层:CourseController

package cn.org.xcore.edusys.controller.course;

import cn.org.xcore.edusys.common.bean.ApiResponse;
import cn.org.xcore.edusys.db.basic.model.Course;
import cn.org.xcore.edusys.db.ext.model.Performance;
import cn.org.xcore.edusys.service.CourseService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 课程接口
 *
 * @author 李海林 手机:13802780104|微信:lihailin9073|Email:767679879@qq.com
 * @copyright 个人开发者李海林版权所有,产品详情及技术服务请登录官网查询[http://www.x-core.org.cn]
 * @create 2019-08-23 07:12
 */
@Api(tags = "05-课程模块")
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation("创建课程")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name"),
            @ApiImplicitParam(paramType = "query", name = "hours"),
            @ApiImplicitParam(paramType = "query", name = "price"),
            @ApiImplicitParam(paramType = "query", name = "typeId"),
            @ApiImplicitParam(paramType = "query", name = "deptId"),
            @ApiImplicitParam(paramType = "query", name = "teacherId"),
            @ApiImplicitParam(paramType = "query", name = "description"),
    })
    @PostMapping("/add")
    public ApiResponse create(Course course) {
        int res = courseService.save(course);
        if (res>0) {
            return ApiResponse.success("操作成功!");
        }
        return ApiResponse.error("操作失败!");
    }

    @ApiOperation("更新课程")
    @PutMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", required = true),
            @ApiImplicitParam(paramType = "query", name = "name"),
            @ApiImplicitParam(paramType = "query", name = "hours"),
            @ApiImplicitParam(paramType = "query", name = "price"),
            @ApiImplicitParam(paramType = "query", name = "typeId"),
            @ApiImplicitParam(paramType = "query", name = "deptId"),
            @ApiImplicitParam(paramType = "query", name = "teacherId"),
            @ApiImplicitParam(paramType = "query", name = "description"),
    })
    public ApiResponse edit(Course course) {
        if (course.getId()>0) {
            int res = courseService.edit(course);
            if (res>0) {
                return ApiResponse.success("操作成功!");
            }
        }
        return ApiResponse.error("操作失败!");
    }

    @ApiOperation("删除课程")
    @ApiImplicitParam(paramType = "query", name = "id", required = true)
    @DeleteMapping("/del/{course_id}")
    public ApiResponse delete(@PathVariable("course_id") Integer courseId) {
        int res = courseService.remove(courseId);
        if (res > 0) {
            return ApiResponse.success("操作成功!");
        }
        return ApiResponse.error("操作失败!");
    }

    @ApiOperation("搜索课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords", paramType = "query"),
            @ApiImplicitParam(name = "page_now", paramType = "query"),
            @ApiImplicitParam(name = "page_size", paramType = "query")
    })
    @PostMapping("/search")
    public Object search(String keywords, @RequestParam(name = "page_now", defaultValue = "1") Integer pageNow, @RequestParam(name = "page_size", defaultValue = "50") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageInfo pageInfo = courseService.search(keywords, pageNow, pageSize);
        if (null!=pageInfo) {
            map.put("obj", pageInfo);
            return ApiResponse.success("操作成功", map);
        } else {
            return ApiResponse.error("没有匹配的结果");
        }

    }


}

2、服务层:CourseService、CourseServiceImpl

(1)CourseService接口

package cn.org.xcore.edusys.service;

import cn.org.xcore.edusys.db.basic.model.Course;
import com.github.pagehelper.PageInfo;

/**
 * 课程服务接口
 *
 * @author 李海林 手机:13802780104|微信:lihailin9073|Email:767679879@qq.com
 * @copyright 个人开发者李海林版权所有,产品详情及技术服务请登录官网查询[http://www.x-core.org.cn]
 * @create 2019-08-29 08:55
 */
public interface CourseService {

    /**
     * 课程搜索
     * @param keywords 关键词,将模糊匹配课程表的name、description字段来搜索对应的课程记录
     * @param pageNow 页码
     * @param pageSize 每页大小
     * @return 返回搜索到数据返回PageInfo对象,否则返回null
     */
    PageInfo search(String keywords, int pageNow, int pageSize);

    /**
     * 新增课程
     * @param course 课程对象
     * @return
     */
    Integer save(Course course);

    /**
     * 更新课程
     * @param course
     * @return
     */
    Integer edit(Course course);

    /**
     * 删除课程
     * @param id 课程ID
     * @return
     */
    Integer remove(Integer id);

}

(2)CourseServiceImpl服务

package cn.org.xcore.edusys.service.impl;

import cn.org.xcore.edusys.db.basic.mapper.CourseMapper;
import cn.org.xcore.edusys.db.basic.mapper.TeacherMapper;
import cn.org.xcore.edusys.db.basic.model.Course;
import cn.org.xcore.edusys.db.basic.model.CourseExample;
import cn.org.xcore.edusys.db.basic.model.Teacher;
import cn.org.xcore.edusys.db.basic.model.TeacherExample;
import cn.org.xcore.edusys.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程服务
 *
 * @author 李海林 手机:13802780104|微信:lihailin9073|Email:767679879@qq.com
 * @copyright 个人开发者李海林版权所有,产品详情及技术服务请登录官网查询[http://www.x-core.org.cn]
 * @create 2019-08-29 08:56
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private TeacherMapper teacherMapper;

    /**
     * 课程搜索
     * @param keywords 关键词,将模糊匹配课程表的name、description字段来搜索对应的课程记录
     * @param pageNow 页码
     * @param pageSize 每页大小
     * @return 返回搜索到数据返回PageInfo对象,否则返回null
     */
    @Override
    public PageInfo search(String keywords, int pageNow, int pageSize) {

        // 关联讲师表的条件查询字段
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andNameLike("%"+keywords+"%");
        List<Teacher> teacherList = teacherMapper.selectByExampleSelective(teacherExample, Teacher.Column.id, Teacher.Column.name);
        List<Integer> teacherIds = new ArrayList<>();
        if (teacherList.size()>0) { // 根据关键词获取讲师id清单
            for (Teacher teacher : teacherList) {
                teacherIds.add(teacher.getId());
                System.out.println("name="+teacher.getName()+", id="+teacher.getId()+", address="+teacher.getAddress());
            }
        }

        // 关联课程表的条件查询字段
        CourseExample courseExample = new CourseExample();
        courseExample.or().andNameLike("%"+keywords+"%");
        courseExample.or().andDescriptionLike("%"+keywords+"%");
        if(teacherIds.size()>0) {
            courseExample.or().andTeacherIdIn(teacherIds); // 增加查询条件:支持根据老师名称关键词查询对应的课程
        }

        // 启用分页查询
        PageHelper.startPage(pageNow,pageSize);
        List<Course> courseList = courseMapper.selectByExample(courseExample);
        PageInfo pageInfo = new PageInfo(courseList); // 获取分页结果

        if (pageInfo.getList().size()>0) {
            return pageInfo;
        } else {
            return null;
        }
    }

    /**
     * 新增课程
     * @param course 课程对象
     * @return 增加成功返回影响的记录数,否则返回null
     */
    @Override
    public Integer save(Course course) {
        course.setCreateTime(LocalDateTime.now());
        int res = courseMapper.insert(course);
        if (res>0) {
            return res;
        }
        return null;
    }

    /**
     * 更新课程
     * @param course 课程对象
     * @return 更新成功返回影响的记录数,否则返回null
     */
    @Override
    public Integer edit(Course course) {
        Course courseModel = courseMapper.selectByPrimaryKey(course.getId());
        if (null != courseModel) {
            if (null != course.getName()) {
                courseModel.setName(course.getName());
            }
            if (null != course.getHours()) {
                courseModel.setHours(course.getHours());
            }
            if (null != course.getPrice()) {
                courseModel.setPrice(course.getPrice());
            }
            if (null != course.getTypeId()) {
                courseModel.setTypeId(course.getTypeId());
            }
            if (null != course.getDeptId()) {
                courseModel.setDeptId(course.getDeptId());
            }
            if (null != course.getTypeId()) {
                courseModel.setTypeId(course.getTypeId());
            }
            if (null != course.getDescription()) {
                courseModel.setDescription(course.getDescription());
            }
            courseModel.setUpdateTime(LocalDateTime.now());

            int res = courseMapper.updateByPrimaryKey(courseModel);
            if (res>0) {
                return res;
            }
        }

        return null;
    }

    /**
     * 删除课程
     * @param id 课程ID
     * @return 删除成功返回影响的记录数,否则返回null
     */
    @Override
    public Integer remove(Integer id) {
        int res = courseMapper.deleteByPrimaryKey(id);
        if (res > 0) {
            return res;
        }
        return null;
    }
}

3、数据层

数据层借助MyBatis Generator自动生成,这里只列出对应的Mapper和Model,如下图所示:

03 微架构教务系统——课程创建接口、课程更新接口、课程删除接口

basic模块中的代码全部由MyBatis Generator生成,可以随时删除和再生成,以便同步数据库的更改;

ext模块中的代码是根据需要扩展出来的数据层,这一层的代码不能删除。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/10478.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!