• 四、《任务列表案例》后端程序实现和测试


    本章概要

    • 准备工作
    • 功能实现
    • 前后联调

    4.1 准备工作

    1. 数据库脚本
    CREATE TABLE schedule (
      id INT NOT NULL AUTO_INCREMENT,
      title VARCHAR(255) NOT NULL,
      completed BOOLEAN NOT NULL,
      PRIMARY KEY (id)
    );
    
    INSERT INTO schedule (title, completed)
    VALUES
        ('学习java', true),
        ('学习Python', false),
        ('学习C++', true),
        ('学习JavaScript', false),
        ('学习HTML5', true),
        ('学习CSS3', false),
        ('学习Vue.js', true),
        ('学习React', false),
        ('学习Angular', true),
        ('学习Node.js', false),
        ('学习Express', true),
        ('学习Koa', false),
        ('学习MongoDB', true),
        ('学习MySQL', false),
        ('学习Redis', true),
        ('学习Git', false),
        ('学习Docker', true),
        ('学习Kubernetes', false),
        ('学习AWS', true),
        ('学习Azure', false);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    1. 准备 pojo

    包:com.atguigu.pojo

    /**
     * projectName: com.atguigu.pojo
     *
     * description: 任务实体类
     */
    @Data
    public class Schedule {
    
        private Integer id;
        private String title;
        private Boolean completed;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 准备 R

    包: com.atguigu.utils

    **
     * projectName: com.atguigu.utils
     *
     * description: 返回结果类
     */
    public class R {
    
        private int code = 200; //200成功状态码
    
        private boolean flag = true; //返回状态
    
        private Object data;  //返回具体数据
    
    
        public  static R ok(Object data){
            R r = new R();
            r.data = data;
            return r;
        }
    
        public static R  fail(Object data){
            R r = new R();
            r.code = 500; //错误码
            r.flag = false; //错误状态
            r.data = data;
            return r;
        }
    
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public boolean isFlag() {
            return flag;
        }
    
        public void setFlag(boolean flag) {
            this.flag = flag;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    1. 准备 PageBean

    包: com.atguigu.utils

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class PageBean<T> {
        private int currentPage;   // 当前页码
        private int pageSize;      // 每页显示的数据量
        private long total;    // 总数据条数
        private List<T> data;      // 当前页的数据集合
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.2 功能实现

    1. 分页查询
    • controller
    /*
        @CrossOrigin 注释在带注释的控制器方法上启用跨源请求
     */
    @CrossOrigin
    @RequestMapping("schedule")
    @RestController
    public class ScheduleController
    {
    
        @Autowired
        private ScheduleService scheduleService;
    
        @GetMapping("/{pageSize}/{currentPage}")
        public R showList(@PathVariable(name = "pageSize") int pageSize, @PathVariable(name = "currentPage") int currentPage){
            PageBean<Schedule> pageBean = scheduleService.findByPage(pageSize,currentPage);
            return  R.ok(pageBean);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • service
    @Slf4j
    @Service
    public class ScheduleServiceImpl  implements ScheduleService {
    
        @Autowired
        private ScheduleMapper scheduleMapper;
    
        /**
         * 分页数据查询,返回分页pageBean
         *
         * @param pageSize
         * @param currentPage
         * @return
         */
        @Override
        public PageBean<Schedule> findByPage(int pageSize, int currentPage) {
            //1.设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            //2.数据库查询
            List<Schedule> list = scheduleMapper.queryPage();
            //3.结果获取
            PageInfo<Schedule> pageInfo = new PageInfo<>(list);
            //4.pageBean封装
            PageBean<Schedule> pageBean = new PageBean<>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    
            log.info("分页查询结果:{}",pageBean);
    
            return pageBean;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • mapper

    mapper 接口

    public interface ScheduleMapper {
    
        List<Schedule> queryPage();
    }    
    
    • 1
    • 2
    • 3
    • 4

    mapper.xml 文件

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.atguigu.mapper.ScheduleMapper">
    
        <select id="queryPage" resultType="schedule">
            select * from schedule
        select>
    mapper>    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 计划添加
    • controller
    @PostMapping
    public R saveSchedule(@RequestBody Schedule schedule){
        scheduleService.saveSchedule(schedule);
        return R.ok(null);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • service
    /**
     * 保存学习计划
     *
     * @param schedule
     */
    @Override
    public void saveSchedule(Schedule schedule) {
        scheduleMapper.insert(schedule);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • mapper

    mapper 接口

    void insert(Schedule schedule);
    
    • 1

    mapper.xml 文件

    <insert id="insert">
        insert into schedule (title, completed)
        values
        (#{title}, #{completed});
    insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 计划删除
    • controller
    @DeleteMapping("/{id}")
    public R removeSchedule(@PathVariable Integer id){
        scheduleService.removeById(id);
        return R.ok(null);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • service
    /**
     * 移除学习计划
     *
     * @param id
     */
    @Override
    public void removeById(Integer id) {
        scheduleMapper.delete(id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • mapper

    mapper 接口

    void delete(Integer id);
    
    • 1

    mapper.xml 文件

    <delete id="delete">
        delete from schedule where id = #{id}
    delete>
    
    • 1
    • 2
    • 3
    1. 计划修改
    • controller
    @PutMapping
        public R changeSchedule(@RequestBody Schedule schedule){
        scheduleService.updateSchedule(schedule);
        return R.ok(null);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • service
    /**
     * 更新学习计划
     *
     * @param schedule
     */
    @Override
    public void updateSchedule(Schedule schedule) {
        scheduleMapper.update(schedule);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • mapper

    mapper 接口

    void update(Schedule schedule);
    
    • 1

    mapper.xml 文件

    <update id="update">
        update schedule set title = #{title} , completed = #{completed}
             where id = #{id}
    update>
    
    • 1
    • 2
    • 3
    • 4

    4.3 前后联调

    1. 后台项目根路径设计

    在这里插入图片描述

    1. 启动测试即可
  • 相关阅读:
    WebGL 雾化
    Fabric上搭建Hyperledger caliper进行性能测试
    PDF文件如何设置密码保护?
    Java的数据类型
    详解前端登录流程:实现原理与最佳实践
    MongoDB安装及进程介绍
    flink 总结
    RS232协议、RS485协议
    为什么网站页面没有被百度搜索收录?是网站被攻击了?
    Vue 中可重用组件的 3 个主要问题
  • 原文地址:https://blog.csdn.net/GXL_1012/article/details/136371853