06 微架构教务系统——学员搜索接口、学员档案添加接口、学员档案更新接口、学员档案删除接口

1、先看接口测试:基于PostMan

使用PostMan作为API接口调试工具,对学员模块的Restful接口进行测试,如下截图所示:

2、控制层代码:StudentController

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

import cn.org.xcore.edusys.common.bean.ApiResponse;
import cn.org.xcore.edusys.db.basic.model.Student;
import cn.org.xcore.edusys.service.StudentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
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-28 11:42
 */
@Api(tags = "04-学员模块")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ApiOperation("新增学员档案")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name"),
            @ApiImplicitParam(paramType = "query", name = "age"),
            @ApiImplicitParam(paramType = "query", name = "phone"),
            @ApiImplicitParam(paramType = "query", name = "parent"),
            @ApiImplicitParam(paramType = "query", name = "province"),
            @ApiImplicitParam(paramType = "query", name = "city"),
            @ApiImplicitParam(paramType = "query", name = "county"),
            @ApiImplicitParam(paramType = "query", name = "street"),
            @ApiImplicitParam(paramType = "query", name = "addressDetail"),
            @ApiImplicitParam(paramType = "query", name = "type"),
            @ApiImplicitParam(paramType = "query", name = "remark"),
            @ApiImplicitParam(paramType = "query", name = "entranceTime")
    })
    @PostMapping("/add")
    public Object add(Student student) {
        int res = studentService.create(student);
        if (res > 0) {
            return ApiResponse.success("操作成功!");
        } else {
            return ApiResponse.error("操作失败!");
        }
    }

    @ApiOperation("更新学员档案")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", required = true),
            @ApiImplicitParam(paramType = "query", name = "name"),
            @ApiImplicitParam(paramType = "query", name = "age"),
            @ApiImplicitParam(paramType = "query", name = "phone"),
            @ApiImplicitParam(paramType = "query", name = "parent"),
            @ApiImplicitParam(paramType = "query", name = "province"),
            @ApiImplicitParam(paramType = "query", name = "city"),
            @ApiImplicitParam(paramType = "query", name = "county"),
            @ApiImplicitParam(paramType = "query", name = "street"),
            @ApiImplicitParam(paramType = "query", name = "addressDetail"),
            @ApiImplicitParam(paramType = "query", name = "type"),
            @ApiImplicitParam(paramType = "query", name = "remark"),
            @ApiImplicitParam(paramType = "query", name = "entranceTime")
    })
    @PutMapping("/update")
    public Object update(Student student) {
        if (student.getId()>0) {
            int res = studentService.save(student);
            if (res>0) {
                return ApiResponse.success("操作成功!");
            }
        }
        return ApiResponse.error("操作失败!");
    }

    @ApiOperation("注销学员档案")
    @DeleteMapping("/del/{student_id}")
    public Object del( @PathVariable(name="student_id") Integer studentId) {
        int res = studentService.remove(studentId);
        if (res > 0) {
            return ApiResponse.success("操作成功!");
        }
        return ApiResponse.error("操作失败!");
    }

    @ApiOperation("查询学员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords", paramType = "query"),
            @ApiImplicitParam(name = "page_num", paramType = "query"),
            @ApiImplicitParam(name = "page_size", paramType = "query")
    })
    @PostMapping("/search")
    public Object search(String keywords, @RequestParam(name = "page_num", defaultValue = "1") int pageNum, @RequestParam(name = "page_size", defaultValue = "20") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageInfo pageInfo = studentService.search(keywords, pageNum, pageSize);
        if (null == pageInfo || 0==pageInfo.getList().size()) {
            map.put("studentList", null);
            map.put("totalRecord", 0);
        } else {
            map.put("studentList", pageInfo.getList());
            map.put("totalRecord", pageInfo.getTotal());
        }

        return map;
    }


}

3、服务层代码:StudentService

(1)学员服务接口 StudentService

package cn.org.xcore.edusys.service;

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

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

    /**
     * 新增学生档案
     * @param student 学生Student对象
     * @return
     */
    Integer create(Student student);

    /**
     * 更新学生档案
     * @param student 学生Student对象
     * @return
     */
    Integer save(Student student);

    /**
     * 删除学生档案
     * @param studentId 学生ID
     * @return
     */
    Integer remove(int studentId);

    /**
     * 搜索学生档案
     * @param keywords 关键词
     * @param pageNum 当前页码
     * @param pageSize 每页大小
     * @return 返回分页对象PageInfo
     */
    PageInfo search(String keywords, int pageNum, int pageSize);
}

(2)学员服务实现类 StudentServiceImpl

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

import cn.org.xcore.edusys.db.basic.mapper.StudentMapper;
import cn.org.xcore.edusys.db.basic.model.Student;
import cn.org.xcore.edusys.db.basic.model.StudentExample;
import cn.org.xcore.edusys.service.StudentService;
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.List;

/**
 * 学生服务
 *
 * @author 李海林 手机:13802780104|微信:lihailin9073|Email:767679879@qq.com
 * @copyright 个人开发者李海林版权所有,产品详情及技术服务请登录官网查询[http://www.x-core.org.cn]
 * @create 2019-08-31 18:36
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    /**
     * 新增学生档案
     * @param student 学生Student对象
     * @return 增加成功返回影响的记录数,否则返回null
     */
    @Override
    public Integer create(Student student) {
        student.setCreateTime(LocalDateTime.now());
        int res = studentMapper.insert(student);
        if (res > 0) {
            return res;
        }
        return null;
    }

    /**
     * 更新学生档案
     * @param student 学生Student对象
     * @return 更新成功返回影响的记录数,否则返回null
     */
    @Override
    public Integer save(Student student) {
        Student studentModel = studentMapper.selectByPrimaryKey(student.getId());
        if (null != studentModel) {
            if (null != student.getName()) {
                studentModel.setName(student.getName());
            }
            if (null != student.getAge()) {
                studentModel.setAge(student.getAge());
            }
            if (null != student.getPhone()) {
                studentModel.setPhone(student.getPhone());
            }
            if (null != student.getParent()) {
                studentModel.setParent(student.getParent());
            }
            if (null != student.getProvince()) {
                studentModel.setProvince(student.getProvince());
            }
            if (null != student.getCity()) {
                studentModel.setCity(student.getCity());
            }
            if (null != student.getCounty()) {
                studentModel.setCounty(student.getCounty());
            }
            if (null != student.getStreet()) {
                studentModel.setStreet(student.getStreet());
            }
            if (null != student.getAddressDetail()) {
                studentModel.setAddressDetail(student.getAddressDetail());
            }
            if (null != student.getType()) {
                studentModel.setType(student.getType());
            }
            if (null != student.getRemark()) {
                studentModel.setRemark(student.getRemark());
            }
            if (null != student.getEntranceTime()) {
                studentModel.setEntranceTime(student.getEntranceTime());
            }
            studentModel.setUpdateTime(LocalDateTime.now());

            int res = studentMapper.updateByPrimaryKey(studentModel);
            if (res > 0) {
                return res;
            }
        }

        return null;
    }

    /**
     * 删除学生档案
     * @param studentId 学生ID
     * @return 删除成功返回影响的记录数,否则返回null
     */
    @Override
    public Integer remove(int studentId) {
        int res = studentMapper.deleteByPrimaryKey(studentId);
        if (res>0) {
            return res;
        }
        return null;
    }

    /**
     * 学生档案搜索
     * @param keywords 关键词,将模糊匹配课程表的name、description字段来搜索对应的课程记录
     * @param pageNum 当前页码
     * @param pageSize 每页大小
     * @return 返回搜索到数据返回PageInfo对象,否则返回null
     */
    @Override
    public PageInfo search(String keywords, int pageNum, int pageSize) {
        StudentExample studentExample = new StudentExample();
        if (null==keywords || "".equals(keywords) || "All".equals(keywords)) {
            // 查询id>0的全部数据
            studentExample.createCriteria().andIdGreaterThan(0);
        } else {
            studentExample.or().andNameLike("%"+keywords+"%"); // 关联姓名查询
            studentExample.or().andPhoneLike("%"+keywords+"%"); // 关联电话号码查询
            studentExample.or().andParentLike("%"+keywords+"%"); // 关联家长姓名查询
            // 其它字段暂不关联
        }

        // 启用分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Student> studentList = studentMapper.selectByExample(studentExample);
        PageInfo pageInfo = new PageInfo(studentList); // 获取分页结果

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

4、数据层代码:基于MyBatis Generator

5、数据库表结构:基于MySQL

-- phpMyAdmin SQL Dump
-- version 4.9.0.1
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2019-08-31 21:39:21
-- 服务器版本: 5.5.62
-- PHP 版本: 7.2.20

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

--
-- 数据库: `edu_sys`
--

-- --------------------------------------------------------

--
-- 表的结构 `student`
--

CREATE TABLE `student` (
  `id` int(11) NOT NULL COMMENT '自增主键',
  `name` varchar(30) NOT NULL COMMENT '姓名',
  `age` int(3) DEFAULT '0' COMMENT '年龄',
  `phone` varchar(16) DEFAULT NULL COMMENT '联系电话',
  `parent` varchar(16) DEFAULT NULL COMMENT '家长',
  `province` varchar(60) DEFAULT NULL COMMENT '省份',
  `city` varchar(60) DEFAULT NULL COMMENT '城市',
  `county` varchar(60) DEFAULT NULL COMMENT '县/区',
  `street` varchar(100) DEFAULT NULL COMMENT '街道',
  `address_detail` varchar(200) DEFAULT NULL COMMENT '详细地址(到门牌号)',
  `type` enum('幼儿教育','小学教育','初中教育','高中教育','会计培训','软件培训','职业考证') NOT NULL COMMENT '学习意向',
  `remark` text,
  `entrance_time` date DEFAULT NULL COMMENT '入学时间',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `student`
--

INSERT INTO `student` (`id`, `name`, `age`, `phone`, `parent`, `province`, `city`, `county`, `street`, `address_detail`, `type`, `remark`, `entrance_time`, `create_time`, `update_time`) VALUES
(1, '诗小涵', 5, '13802780108', '诗天洛', '广东省', '广州市', '番禺区', '番禺大道东一路', '锦绣香江小区16栋1003号', '幼儿教育', '该生由外省转入,需提前向当地教育局报备办理档案迁入手续', '2019-09-01', '2019-08-23 02:16:41', NULL),
(2, '洛飞羽', 5, '13802780108', '诗天洛', '广东省', '广州市', '番禺区', '番禺大道东一路', '锦绣香江小区16栋1003号', '幼儿教育', '该生由外省转入,需提前向当地教育局报备办理档案迁入手续', '2019-09-01', '2019-08-22 18:16:41', NULL),
(3, '李洛璃', 5, '13802780108', '诗天洛', '广东省', '广州市', '番禺区', '番禺大道东一路', '锦绣香江小区16栋1003号', '幼儿教育', '本省它市学生', '2019-09-01', '2019-08-22 18:16:41', NULL),
(4, '陈敖', 5, '13802780108', '诗天洛', '广东省', '广州市', '番禺区', '番禺大道东一路', '锦绣香江小区16栋1003号', '幼儿教育', '该生由外省转入,需提前向当地教育局报备办理档案迁入手续', '2019-09-01', '2019-08-22 18:16:41', NULL),
(5, '张云皓', 5, '13802780108', '诗天洛', '广东省', '广州市', '番禺区', '番禺大道东一路', '锦绣香江小区16栋1003号', '幼儿教育', '本省本市学生', '2019-09-01', '2019-08-22 18:16:41', NULL);

--
-- 转储表的索引
--

--
-- 表的索引 `student`
--
ALTER TABLE `student`
  ADD PRIMARY KEY (`id`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `student`
--
ALTER TABLE `student`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键', AUTO_INCREMENT=657;
COMMIT;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
phpMyAdmin 是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库。借由此Web接口可以成为一个简易方式输入繁杂SQL语法的较佳途径,尤其要处理大量资料的汇入及汇出更为方便。其中一个更大的优势在于由于phpMyAdmin跟其他PHP程式一样在网页服务器上执行,但是您可以在任何地方使用这些程式产生的HTML页面,也就是于远端管理MySQL数据库,方便的建立、修改、删除数据库及资料表。也可借由phpMyAdmin建立常用的php语法,方便编写网页时所需要的sql语法正确性。 phpMyAdmin功能特点 PHP是一个基于服务端来创建动态网站的脚本语言,您可以用PHP和HTML生成网站主页.当一个访问者打开主页时,服务端便执行PHP的命令并将执行结果发送至访问者的浏览器中,这类似于ASP和CoildFusion,然而PHP和他们不同之处在于PHP开放源码和跨越平台,PHP可以运行在WINDOWSNT和多种版本的UNIX上.它不需要任何预先处理而快速反馈结果,它也不需要mod_perl的调整来使您的服务器的内存映象减小.PHP消耗的资源较少,当PHP作为ApacheWeb服务器一部分时,运行代码不需要调用外部二进制程序,服务器不需要承担任何额外的负担. 除了能够操作您的页面外,PHP还能发送HIIP的标题.您可以设置cookie,管理数字签名和重定向用户,而且它提供了极好的连通性到其它数据库(还有ODBC),集成各种外部库来做用PDF文档解析XML的任何事情.[1] phpMyAdmin 是一个用PHP编写的软件工具,可以通过web方式控制和操作MySQL数据库.通过phpMyAdmin 可以完全对数据库进行操作,例如建立、复制和删除数据等等.如果使用合适的工具,MySQL数据库的管理就会变得相当简单.应用 MySQL 命令行方式需要对 MySQL 知识非常熟悉,对SQL语言也是同样的道理.不仅如此,如果数据库的访问量很大,列表中数据的读取就会相当困难. 当前出现很多GUI MySQL客户程序,其中最为出色的是基于 Web 的phpMyAdmin 工具.这是一种 MySQL数据库前台的基于PHP的工具. PhpMyAdmin 的缺点是必须安装在 Web 服务器中,所以如果没有合适的访问权限,其它用户有可能损害到 SQL 数据. phpMyAdmin截图 相关阅读 同类推荐:站长常用源码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值