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;