SpringCloud整合SpringBoot-综合案例

1. 使用技术

1.1 后端

  • SpringCloud+SpringBoot+maven2+web+MySQL+xml+pagehelper+lombok+jwt+ras+BaseResult
  • Eureka 注册中心 + Eureka 客户端+Zuul网关 + 过滤器

1.2 前端

  • vue渐进式JavaScript框架

2. 需完成功能

2.1 功能1:用户登录

前端代码

api.js

/** ajax axios */
import axios from 'axios'
import {
      Message } from 'element-ui'

/** axios 基本项设置 */
axios.defaults.baseURL = 'http://localhost:10010/api'
axios.defaults.timeout = 3000

// 全局ajax错误处理
// axios.interceptors.response.use(response => {
     
//   return response
// }, error => {
     
//   Message.error(error.message)
//   return Promise.reject(error)
// })

/** 发送请求 */
export function login (user) {
     
  return axios.post('/userservice/user/login', user)
}
export function FindClassesForPageNum (ClassesPi) {
     
  return axios.get('/studentservice/classes/FindClassesForPageNum',{
     
    params:ClassesPi
  } )
}
export function Findteacher1list (t1) {
     
  return axios.get('/studentservice/classes/Findteacher1list/' +t1)
}
export function Findteacher2list (t2) {
     
  return axios.get('/studentservice/classes/Findteacher2list/' +t2)
}
export function Findteacher3list (t3) {
     
  return axios.get('/studentservice/classes/Findteacher3list/' + t3)
}
export function addClasses (classes) {
     
  return axios.post('/studentservice/classes/addClasses', classes)
}
export function DeleteClassesByCid (ids) {
     
  return axios.delete('/studentservice/classes/DeleteClassesByCid/' + ids)
}
export function FindByClassesByCid (cid) {
     
  return axios.get('/studentservice/classes/FindByClassesByCid/' + cid)
}
export function UpdateClasses (classes) {
     
  return axios.put('/studentservice/classes/UpdateClasses', classes)
}
export function FindStudentListForPageNum (studentPi) {
     
  return axios.get('/studentservice/student/FindStudentListForPageNum', {
     
    params:studentPi
  })
}
export function Findclasses () {
     
  return axios.get('/studentservice/student/Findclasses')
}
export function addStudent (student) {
     
  return axios.post('/studentservice/student/addStudent', student)
}
export function deleteStudentBysid (ids) {
     
  return axios.delete('/studentservice/student/deleteStudentBysid/'+ids)
}
export function FindStudent (sid) {
     
  return axios.get('/studentservice/student/FindStudent/'+sid)
}

SpringCloud整合SpringBoot-综合案例_第1张图片
SpringCloud整合SpringBoot-综合案例_第2张图片
SpringCloud整合SpringBoot-综合案例_第3张图片






后端代码

SpringCloud整合SpringBoot-综合案例_第4张图片

  • pom依赖
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.0.4
        
        
        
            mysql
            mysql-connector-java
        
        
            com.czxy.cloud
            Exam_common
            1.0-SNAPSHOT
        
    
  • application.yml文件
server:
  port: 9090
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 947411
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka/
  • UserApplication启动类
package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.czxy.mapper")
public class UserApplicatioon {
     
    public static void main(String[] args) {
     
        SpringApplication.run(UserApplicatioon.class,args);
    }
}

  • domain对象类
package com.czxy.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "tb_user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
     
    @Id
    private Integer id;
    private String user_name;
    private String password;
}
  • mapper 通用mapper类
package com.czxy.mapper;

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

public interface UserMapper extends Mapper<User> {
     
}

  • service
package com.czxy.service;

import com.czxy.domain.User;

public interface UserService {
     
    /**
     * 登录用户
     * @param user
     * @return
     */
    User login(User user);
}
package com.czxy.service.impl;

import com.czxy.domain.User;
import com.czxy.mapper.UserMapper;
import com.czxy.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService {
     
    @Resource
    private UserMapper userMapper;
    /**
     * 登录用户
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
     
        Example example = new Example(User.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("user_name",user.getUser_name());
        c.andEqualTo("password",user.getPassword());
        User bean = userMapper.selectOneByExample(example);
        if (bean==null){
     
            return null;
        }
        return bean;
    }
}

  • controller 控制类
package com.czxy.controller;

import com.czxy.domain.User;
import com.czxy.service.UserService;
import com.czxy.vo.BaseResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
     
    // private static final  String priKeyPath = "D:\\ras\\ras.pri";
    @Resource
    private UserService userService;

    /**
     * 登录用户
     * @param user
     * @return
     */
    @PostMapping("/login")
    public BaseResult login(@RequestBody User user){
     
            //如果登录成功,将数据存入私钥中
            User loginUser = userService.login(user);
            if (loginUser!=null){
     
                // String token = JwtUtils.generateToken(loginUser, 60 * 5, RasUtils.getPrivateKey(priKeyPath));
                return BaseResult.ok("登录成功",loginUser);
            }else{
     
                return BaseResult.error("用户名或密码错误");
            }
        }
}

2.2 功能2:布局

SpringCloud整合SpringBoot-综合案例_第5张图片







2.3 功能3:添加班级

SpringCloud整合SpringBoot-综合案例_第6张图片

前端代码






后端代码

SpringCloud整合SpringBoot-综合案例_第7张图片

  • domain 对象类
package com.czxy.domain;

import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Table(name = "tb_class")
public class Classes implements Serializable {
     
    @Id
    private Integer cid;
    private String cname;
    private Integer teacher1_id;
    private Teacher teacher1;
    private Integer teacher2_id;
    private Teacher teacher2;
    private Integer teacher3_id;
    private Teacher teacher3;


    @Override
    public String toString() {
     
        return "Classes{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", teacher1_id=" + teacher1_id +
                ", teacher1=" + teacher1 +
                ", teacher2_id=" + teacher2_id +
                ", teacher2=" + teacher2 +
                ", teacher3_id=" + teacher3_id +
                ", teacher3=" + teacher3 +
                '}';
    }

    public Classes() {
     
    }

    public Integer getCid() {
     

        return cid;
    }

    public void setCid(Integer cid) {
     
        this.cid = cid;
    }

    public String getCname() {
     
        return cname;
    }

    public void setCname(String cname) {
     
        this.cname = cname;
    }

    public Integer getTeacher1_id() {
     
        return teacher1_id;
    }

    public void setTeacher1_id(Integer teacher1_id) {
     
        this.teacher1_id = teacher1_id;
    }

    public Teacher getTeacher1() {
     
        return teacher1;
    }

    public void setTeacher1(Teacher teacher1) {
     
        this.teacher1 = teacher1;
    }

    public Integer getTeacher2_id() {
     
        return teacher2_id;
    }

    public void setTeacher2_id(Integer teacher2_id) {
     
        this.teacher2_id = teacher2_id;
    }

    public Teacher getTeacher2() {
     
        return teacher2;
    }

    public void setTeacher2(Teacher teacher2) {
     
        this.teacher2 = teacher2;
    }

    public Integer getTeacher3_id() {
     
        return teacher3_id;
    }

    public void setTeacher3_id(Integer teacher3_id) {
     
        this.teacher3_id = teacher3_id;
    }

    public Teacher getTeacher3() {
     
        return teacher3;
    }

    public void setTeacher3(Teacher teacher3) {
     
        this.teacher3 = teacher3;
    }
}

package com.czxy.domain;

import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Table(name = "tb_teacher")

public class Teacher implements Serializable{
     
    @Id
    private Integer tid;
    private String tname;
    private Integer type;

    @Override
    public String toString() {
     
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                ", type=" + type +
                '}';
    }

    public Integer getTid() {
     
        return tid;
    }

    public void setTid(Integer tid) {
     
        this.tid = tid;
    }

    public String getTname() {
     
        return tname;
    }

    public void setTname(String tname) {
     
        this.tname = tname;
    }

    public Integer getType() {
     
        return type;
    }

    public void setType(Integer type) {
     
        this.type = type;
    }

    public Teacher(Integer tid, String tname, Integer type) {
     

        this.tid = tid;
        this.tname = tname;
        this.type = type;
    }

    public Teacher() {
     

    }
}

  • mapper 配置类
package com.czxy.mapper;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface ClassesMapper extends Mapper<Classes> {
     }

package com.czxy.mapper;

import com.czxy.domain.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface TeacherMapper extends Mapper<Teacher> {
     }
  • service和serviceImpl
package com.czxy.service;
import com.czxy.domain.Classes;

public interface ClassesService {
     
  /**
    * 添加班级
    * @param classes
    * @return
    */
   void addClasses(Classes classes);}

package com.czxy.service.impl;
import com.czxy.domain.Classes;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;
import com.czxy.service.ClassesService;
import javax.annotation.Resource;
@Service
@Transactional
public class ClassesServiceImpl implements ClassesService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;

    /**
     * 添加班级
     * @param classes
     * @return
     */
    @PostMapping("/addClasses")
    public BaseResult addClasses(@RequestBody Classes classes){
     
        classesService.addClasses(classes);
        return BaseResult.ok("添加成功");
    }
}
  • controller 控制器
package com.czxy.controller;
import com.czxy.domain.Classes;
import javax.annotation.Resource;
@RestController
@RequestMapping("/classes")
public class ClassesController {
     
    @Resource
    private ClassesService classesService;
    /**
     * 添加班级
     * @param classes
     * @return
     */
    @PostMapping("/addClasses")
    public BaseResult addClasses(@RequestBody Classes classes){
     
        classesService.addClasses(classes);
        return BaseResult.ok("添加成功");
    }

2.4 功能4:班级列表(基本列表 + 分页 + 条件)

SpringCloud整合SpringBoot-综合案例_第8张图片

前端代码





后端代码

  • controller 控制器
package com.czxy.controller;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import com.czxy.service.ClassesService;
import com.czxy.vo.BaseResult;
import com.github.pagehelper.PageInfo;


import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
     
    @Resource
    private ClassesService classesService;

    /**
     * 添加班级
     * @param classes
     * @return
     */
    @PostMapping("/addClasses")
    public BaseResult addClasses(@RequestBody Classes classes){
     
        classesService.addClasses(classes);
        return BaseResult.ok("添加成功");
    }
    
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @return
     */
    @GetMapping("/FindClassesForPageNum")
    public BaseResult FindClassesForPageNum( ClassesPi classesPi){
     
     PageInfo<Classes> pi =  classesService.FindClassesForPageNum(classesPi);
        for (Classes classes : pi.getList()) {
     
            System.out.println(classes.toString());
        }
     return BaseResult.ok("查询成功",pi);
    }
}

  • service 和 serviceImpl
package com.czxy.service;

        import com.czxy.domain.Classes;
        import com.czxy.domain.ClassesPi;
        import com.github.pagehelper.PageInfo;
        import java.util.List;

public interface ClassesService {
     
    /**
     * 添加班级
     * @param classes
     * @return
     */
    void addClasses(Classes classes);
        /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    PageInfo<Classes> FindClassesForPageNum(ClassesPi classesPi);
}

  • mapper 配置类
package com.czxy.mapper;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface ClassesMapper extends Mapper<Classes> {
     
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    @Select(" ")
    @Results(id = "c1",value = {
     
            @Result(property = "cid",column = "cid"),
            @Result(property = "cname",column = "cname"),
            @Result(property = "teacher1_id",column = "teacher1_id"),
            @Result(property = "teacher2_id",column = "teacher2_id"),
            @Result(property = "teacher3_id",column = "teacher3_id"),
            @Result(property = "teacher1",column = "teacher1_id",one = @One(select = "com.czxy.mapper.TeacherMapper.selectByID")),
            @Result(property = "teacher2",column = "teacher2_id",one = @One(select = "com.czxy.mapper.TeacherMapper.selectByID")),
            @Result(property = "teacher3",column = "teacher3_id",one = @One(select = "com.czxy.mapper.TeacherMapper.selectByID"))
    })
    List<Classes> FindClassesForPageNum(@Param("classesPi") ClassesPi classesPi);

}

2.5 功能5:批量删除班级

SpringCloud整合SpringBoot-综合案例_第9张图片

前段代码





后端代码

  • controller
package com.czxy.controller;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import com.czxy.service.ClassesService;
import com.czxy.vo.BaseResult;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
     
    @Resource
    private ClassesService classesService;

    /**
     * 查询班级列表(分页+条件)
     * @param
     * @return
     */
    @GetMapping("/FindClassesForPageNum")
    public BaseResult FindClassesForPageNum( ClassesPi classesPi){
     
     PageInfo<Classes> pi =  classesService.FindClassesForPageNum(classesPi);
        for (Classes classes : pi.getList()) {
     
            System.out.println(classes.toString());
        }
     return BaseResult.ok("查询成功",pi);
    }
    /**
     * 添加班级
     * @param classes
     * @return
     */
    @PostMapping("/addClasses")
    public BaseResult addClasses(@RequestBody Classes classes){
     
        classesService.addClasses(classes);
        return BaseResult.ok("添加成功");
    }

    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    @DeleteMapping("/DeleteClassesByCid/{ids}")
    public BaseResult DeleteClassesByCid(@PathVariable("ids")String ids){
     
        classesService.DeleteClassesByCid(ids);
        return BaseResult.ok("删除成功");
    }


}

  • service
package com.czxy.service;

        import com.czxy.domain.Classes;
        import com.czxy.domain.ClassesPi;
        import com.github.pagehelper.PageInfo;

        import java.util.List;

public interface ClassesService {
     
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    PageInfo<Classes> FindClassesForPageNum(ClassesPi classesPi);
    /**
     * 添加班级
     * @param classes
     * @return
     */
    void addClasses(Classes classes);
    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    void DeleteClassesByCid(String ids);
 
}

  • serviceImpl
package com.czxy.service.impl;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;
import com.czxy.service.ClassesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class ClassesServiceImpl implements ClassesService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    @Override
    public PageInfo<Classes> FindClassesForPageNum(ClassesPi classesPi) {
     
        PageHelper.startPage(classesPi.getPageNum(),classesPi.getPageSize());
        if (classesPi.getCname()!=null && !"" .equals(classesPi.getCname())){
     
            classesPi.setCname("%"+classesPi.getCname()+"%");
        }
        List<Classes> clist =  classesMapper.FindClassesForPageNum(classesPi);

        PageInfo<Classes> classesPageInfo = new PageInfo<>(clist);
        return classesPageInfo;
    }
    /**
     * 添加班级
     * @param classes
     * @return
     */
    @Override
    public void addClasses(Classes classes) {
     
        classesMapper.insertSelective(classes);
    }
    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    @Override
    public void DeleteClassesByCid(String ids) {
     
        String[] split = ids.split(",");
        for (String s : split) {
     
            classesMapper.deleteByPrimaryKey(Integer.parseInt(s));
        }
    }
}

2.6 功能6:修改班级

SpringCloud整合SpringBoot-综合案例_第10张图片

前段代码





后端代码

  • controller
package com.czxy.controller;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import com.czxy.domain.Teacher;
import com.czxy.service.ClassesService;
import com.czxy.vo.BaseResult;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
     
    @Resource
    private ClassesService classesService;

    /**
     * 查询班级列表(分页+条件)
     * @param
     * @return
     */
    @GetMapping("/FindClassesForPageNum")
    public BaseResult FindClassesForPageNum( ClassesPi classesPi){
     
     PageInfo<Classes> pi =  classesService.FindClassesForPageNum(classesPi);
        for (Classes classes : pi.getList()) {
     
            System.out.println(classes.toString());
        }
     return BaseResult.ok("查询成功",pi);
    }

    /**
     * 查询授课老师信息
     * @param tid
     * @return
     */
    @GetMapping("/Findteacher1list/{t1}")
    public BaseResult Findteacher1list(@PathVariable("t1")Integer tid){
     
       List<Teacher> tlist =  classesService.Findteacher1list(tid);
        System.out.println(tlist);
       return BaseResult.ok("查询成功",tlist);
    }
    /**
     * 查询助理老师信息
     * @param tid
     * @return
     */
    @GetMapping("/Findteacher2list/{t2}")
    public BaseResult Findteacher2list(@PathVariable("t2")Integer tid){
     
        List<Teacher> tlist =  classesService.Findteacher2list(tid);
        System.out.println(tlist);
        return BaseResult.ok("查询成功",tlist);
    }
    /**
     * 查询辅导员老师老师信息
     * @param tid
     * @return
     */
    @GetMapping("/Findteacher3list/{t3}")
    public BaseResult Findteacher3list(@PathVariable("t3")Integer tid){
     
        List<Teacher> tlist =  classesService.Findteacher3list(tid);
        System.out.println(tlist);
        return BaseResult.ok("查询成功",tlist);
    }

    /**
     * 添加班级
     * @param classes
     * @return
     */
    @PostMapping("/addClasses")
    public BaseResult addClasses(@RequestBody Classes classes){
     
        classesService.addClasses(classes);
        return BaseResult.ok("添加成功");
    }

    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    @DeleteMapping("/DeleteClassesByCid/{ids}")
    public BaseResult DeleteClassesByCid(@PathVariable("ids")String ids){
     
        classesService.DeleteClassesByCid(ids);
        return BaseResult.ok("删除成功");
    }

    /**
     * 修改第一步:回显数据
     * @param cid
     * @return
     */
    @GetMapping("/FindByClassesByCid/{cid}")
    public BaseResult FindByClassesByCid(@PathVariable("cid") Integer cid){
     
        Classes classes = classesService.FindByClassesByCid(cid);
        return BaseResult.ok("查询成功",classes);
    }


}

  • service
package com.czxy.service;

        import com.czxy.domain.Classes;
        import com.czxy.domain.ClassesPi;
        import com.czxy.domain.Teacher;
        import com.github.pagehelper.PageInfo;

        import java.util.List;

public interface ClassesService {
     
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    PageInfo<Classes> FindClassesForPageNum(ClassesPi classesPi);
    /**
     * 查询授课老师信息
     * @param tid
     * @return
     */
    List<Teacher> Findteacher1list(Integer tid);
    /**
     * 查询助理老师信息
     * @param tid
     * @return
     */
    List<Teacher> Findteacher2list(Integer tid);
    /**
     * 查询辅导员老师老师信息
     * @param tid
     * @return
     */
    List<Teacher> Findteacher3list(Integer tid);
    /**
     * 添加班级
     * @param classes
     * @return
     */
    void addClasses(Classes classes);
    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    void DeleteClassesByCid(String ids);
    /**
     * 修改第一步:回显数据
     * @param cid
     * @return
     */
    Classes FindByClassesByCid(Integer cid);
}
  • serviceIpml
package com.czxy.service.impl;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import com.czxy.domain.Teacher;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;
import com.czxy.service.ClassesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class ClassesServiceImpl implements ClassesService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    @Override
    public PageInfo<Classes> FindClassesForPageNum(ClassesPi classesPi) {
     
        PageHelper.startPage(classesPi.getPageNum(),classesPi.getPageSize());
        if (classesPi.getCname()!=null && !"" .equals(classesPi.getCname())){
     
            classesPi.setCname("%"+classesPi.getCname()+"%");
        }
        List<Classes> clist =  classesMapper.FindClassesForPageNum(classesPi);

        PageInfo<Classes> classesPageInfo = new PageInfo<>(clist);
        return classesPageInfo;
    }
    /**
     * 查询授课老师信息
     * @param tid
     * @return
     */
    @Override
    public List<Teacher> Findteacher1list(Integer tid) {
     
        List<Teacher> tlist =  teacherMapper.Findteacher1list(tid);
        return tlist;
    }
    /**
     * 查询助理老师信息
     * @param tid
     * @return
     */
    @Override
    public List<Teacher> Findteacher2list(Integer tid) {
     
        List<Teacher> tlist =  teacherMapper.Findteacher1list(tid);
        return tlist;
    }
    /**
     * 查询辅导员老师老师信息
     * @param tid
     * @return
     */
    @Override
    public List<Teacher> Findteacher3list(Integer tid) {
     
        List<Teacher> tlist =  teacherMapper.Findteacher1list(tid);
        return tlist;
    }
    /**
     * 添加班级
     * @param classes
     * @return
     */
    @Override
    public void addClasses(Classes classes) {
     
        classesMapper.insertSelective(classes);
    }
    /**
     * 批量删除数据
     * @param ids
     * @return
     */
    @Override
    public void DeleteClassesByCid(String ids) {
     
        String[] split = ids.split(",");
        for (String s : split) {
     
            classesMapper.deleteByPrimaryKey(Integer.parseInt(s));
        }
    }
    /**
     * 修改第一步:回显数据
     * @param cid
     * @return
     */
    @Override
    public Classes FindByClassesByCid(Integer cid) {
     
        Classes classes =  classesMapper.FindByClassesByCid(cid);
        return classes;
    }
}

  • mapper
package com.czxy.mapper;

import com.czxy.domain.Classes;
import com.czxy.domain.ClassesPi;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface ClassesMapper extends Mapper<Classes> {
     
    /**
     * 查询班级列表(分页+条件)
     * @param
     * @param classesPi
     * @return
     */
    @Select(" ")
    @Results(id = "c1",value = {
     
            @Result(property = "cid",column = "cid"),
            @Result(property = "cname",column = "cname"),
            @Result(property = "teacher1_id",column = "teacher1_id"),
            @Result(property = "teacher2_id",column = "teacher2_id"),
            @Result(property = "teacher3_id",column = "teacher3_id"),
            @Result(property = "teacher1",column = "teacher1_id",one = @One(select = "com.czxy.mapper.TeacherMapper.selectByID")),
            @Result(property = "teacher2",column = "teacher2_id",one = @One(select = "com.czxy.mapper.TeacherMapper.selectByID")),
            @Result(property = "teacher3",column = "teacher3_id",one = @One(select = "com.czxy.mapper.TeacherMapper.selectByID"))
    })
    List<Classes> FindClassesForPageNum(@Param("classesPi") ClassesPi classesPi);

    @Select("select * from tb_class where cid=#{cid}")
    @ResultMap( "c1")
    Classes FindByClassesByCid(@Param("cid") Integer cid);
}

2.7 功能7:添加学生

SpringCloud整合SpringBoot-综合案例_第11张图片

前端代码






后端代码

  • domain
package com.czxy.domain;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Table(name = "tb_student")
public class Student implements Serializable {
     
    @Id
    private Integer sid;
    private String sname;
    private Integer gender;
    private Integer age;
    private Double salary;
    @Column(name = "`desc`")
    private String desc;
    private Integer class_id;
    private Classes classes;

    @Override
    public String toString() {
     
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                ", salary=" + salary +
                ", desc='" + desc + '\'' +
                ", class_id=" + class_id +
                ", classes=" + classes +
                '}';
    }

    public Integer getSid() {
     
        return sid;
    }

    public void setSid(Integer sid) {
     
        this.sid = sid;
    }

    public String getSname() {
     
        return sname;
    }

    public void setSname(String sname) {
     
        this.sname = sname;
    }

    public Integer getGender() {
     
        return gender;
    }

    public void setGender(Integer gender) {
     
        this.gender = gender;
    }

    public Integer getAge() {
     
        return age;
    }

    public void setAge(Integer age) {
     
        this.age = age;
    }

    public Double getSalary() {
     
        return salary;
    }

    public void setSalary(Double salary) {
     
        this.salary = salary;
    }

    public String getDesc() {
     
        return desc;
    }

    public void setDesc(String desc) {
     
        this.desc = desc;
    }

    public Integer getClass_id() {
     
        return class_id;
    }

    public void setClass_id(Integer class_id) {
     
        this.class_id = class_id;
    }

    public Classes getClasses() {
     
        return classes;
    }

    public void setClasses(Classes classes) {
     
        this.classes = classes;
    }

    public Student(Integer sid, String sname, Integer gender, Integer age, Double salary, String desc, Integer class_id, Classes classes) {
     

        this.sid = sid;
        this.sname = sname;
        this.gender = gender;
        this.age = age;
        this.salary = salary;
        this.desc = desc;
        this.class_id = class_id;
        this.classes = classes;
    }

    public Student() {
     

    }
}

  • mapper
package com.czxy.mapper;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface StudentMapper extends Mapper<Student> {
     
 
}

  • service
package com.czxy.service;
import com.czxy.domain.Student;
import java.util.List;

public interface StudentService {
     
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    void addStudent(Student student);
   
}

  • serviceImpl
package com.czxy.service.impl;


import com.czxy.domain.Student;

import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
@Transactional
public class StudentServiceImpl implements StudentService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @Override
    public void addStudent(Student student) {
     
        studentMapper.insertSelective(student);
    }
   

  • controller
package com.czxy.controller;


import com.czxy.domain.Student;

import com.czxy.service.StudentService;
import com.czxy.vo.BaseResult;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/student")
public class StudentController {
     
    @Resource
    private StudentService studentService;


    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    private BaseResult addStudent(@RequestBody Student student){
     
        studentService.addStudent(student);
        return BaseResult.ok("添加成功");
    }


}

2.8 功能8:学生列表(基本列表+分页+条件)

SpringCloud整合SpringBoot-综合案例_第12张图片

前端代码






后端代码

package com.czxy.controller;

        import com.czxy.domain.Classes;
        import com.czxy.domain.Student;
        import com.czxy.domain.StudentPi;
        import com.czxy.service.StudentService;
        import com.czxy.vo.BaseResult;
        import com.github.pagehelper.PageInfo;
        import org.springframework.web.bind.annotation.*;

        import javax.annotation.Resource;
        import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
     
    @Resource
    private StudentService studentService;

    /**
     *  学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @GetMapping("/FindStudentListForPageNum")
    public BaseResult FindStudentListForPageNum( StudentPi studentPi){
     
        PageInfo<Student> pageInfo =  studentService.FindStudentListForPageNum(studentPi);
        return BaseResult.ok("查询成功",pageInfo);
    }

    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    private BaseResult addStudent(@RequestBody Student student){
     
        studentService.addStudent(student);
        return BaseResult.ok("添加成功");
    }
}

  • service
package com.czxy.service;

import com.czxy.domain.Classes;
import com.czxy.domain.Student;
import com.czxy.domain.StudentPi;
import com.github.pagehelper.PageInfo;

import java.util.List;

public interface StudentService {
     
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    PageInfo<Student> FindStudentListForPageNum(StudentPi studentPi);
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    void addStudent(Student student);

}

  • serviceImpl
package com.czxy.service.impl;

import com.czxy.domain.Classes;
import com.czxy.domain.Student;
import com.czxy.domain.StudentPi;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;
import com.czxy.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @Override
    public PageInfo<Student> FindStudentListForPageNum(StudentPi studentPi) {
     
        PageHelper.startPage(studentPi.getPageNum(),studentPi.getPageSize());
    //    设置姓名
        if (studentPi.getSname() != null && !"" .equals(studentPi.getSname())){
     
            studentPi.setSname("%"+studentPi.getSname()+"%");
        }
        List<Student> slist =  studentMapper.FindStudentListForPageNum(studentPi);
        PageInfo<Student> pageInfo = new PageInfo<>(slist);
        return pageInfo;
    }

    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @Override
    public void addStudent(Student student) {
     
        studentMapper.insertSelective(student);
    }
 

  • mapper
package com.czxy.mapper;

        import com.czxy.domain.Student;
        import com.czxy.domain.StudentPi;
        import org.apache.ibatis.annotations.*;
        import tk.mybatis.mapper.common.Mapper;

        import java.util.List;

public interface StudentMapper extends Mapper<Student> {
     
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @Select("")
    @Results(id = "s1",value = {
     
            @Result(property = "class_id",column = "class_id"),
            @Result(property = "classes",column = "class_id",one = @One(select = "com.czxy.mapper.ClassesMapper.FindByClassesByCid"))
    })
    List<Student> FindStudentListForPageNum(@Param("studentPi") StudentPi studentPi);


}

2.9 功能9:批量删除

SpringCloud整合SpringBoot-综合案例_第13张图片

前段代码






后端代码

  • controller
package com.czxy.controller;

        import com.czxy.domain.Classes;
        import com.czxy.domain.Student;
        import com.czxy.domain.StudentPi;
        import com.czxy.service.StudentService;
        import com.czxy.vo.BaseResult;
        import com.github.pagehelper.PageInfo;
        import org.springframework.web.bind.annotation.*;

        import javax.annotation.Resource;
        import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
     
    @Resource
    private StudentService studentService;

    /**
     *  学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @GetMapping("/FindStudentListForPageNum")
    public BaseResult FindStudentListForPageNum( StudentPi studentPi){
     
        PageInfo<Student> pageInfo =  studentService.FindStudentListForPageNum(studentPi);
        return BaseResult.ok("查询成功",pageInfo);
    }
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    private BaseResult addStudent(@RequestBody Student student){
     
        studentService.addStudent(student);
        return BaseResult.ok("添加成功");
    }

    /**
     *  批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteStudentBysid/{ids}")
    public BaseResult deleteStudentBysid(@PathVariable("ids") String ids){
     
        studentService.deleteStudentBysid(ids);
        return BaseResult.ok("删除成功");
    }

 

}

  • service
package com.czxy.service;

import com.czxy.domain.Classes;
import com.czxy.domain.Student;
import com.czxy.domain.StudentPi;
import com.github.pagehelper.PageInfo;

import java.util.List;

public interface StudentService {
     
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    PageInfo<Student> FindStudentListForPageNum(StudentPi studentPi);

    /**
     *  添加一个学生
     * @param student
     * @return
     */
    void addStudent(Student student);
    /**
     *  批量删除
     * @param ids
     * @return
     */
    void deleteStudentBysid(String ids);

}

  • serviceImpl
package com.czxy.service.impl;

import com.czxy.domain.Classes;
import com.czxy.domain.Student;
import com.czxy.domain.StudentPi;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;
import com.czxy.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @Override
    public PageInfo<Student> FindStudentListForPageNum(StudentPi studentPi) {
     
        PageHelper.startPage(studentPi.getPageNum(),studentPi.getPageSize());
    //    设置姓名
        if (studentPi.getSname() != null && !"" .equals(studentPi.getSname())){
     
            studentPi.setSname("%"+studentPi.getSname()+"%");
        }
        List<Student> slist =  studentMapper.FindStudentListForPageNum(studentPi);
        PageInfo<Student> pageInfo = new PageInfo<>(slist);
        return pageInfo;
    }
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @Override
    public void addStudent(Student student) {
     
        studentMapper.insertSelective(student);
    }
    /**
     *  批量删除
     * @param ids
     * @return
     */
    @Override
    public void deleteStudentBysid(String ids) {
     
        String[] split = ids.split(",");
        for (String s : split) {
     
            studentMapper.deleteByPrimaryKey(Integer.parseInt(s));
        }
    }
 

2.10 功能10:修改学生

SpringCloud整合SpringBoot-综合案例_第14张图片

前端代码






后端代码

  • controller
package com.czxy.controller;

        import com.czxy.domain.Classes;
        import com.czxy.domain.Student;
        import com.czxy.domain.StudentPi;
        import com.czxy.service.StudentService;
        import com.czxy.vo.BaseResult;
        import com.github.pagehelper.PageInfo;
        import org.springframework.web.bind.annotation.*;

        import javax.annotation.Resource;
        import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
     
    @Resource
    private StudentService studentService;

    /**
     *  学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @GetMapping("/FindStudentListForPageNum")
    public BaseResult FindStudentListForPageNum( StudentPi studentPi){
     
        PageInfo<Student> pageInfo =  studentService.FindStudentListForPageNum(studentPi);
        return BaseResult.ok("查询成功",pageInfo);
    }

    /**
     *  查询班级信息
     * @return
     */
    @GetMapping("/Findclasses")
    public BaseResult Findclasses(){
     
        List<Classes> clist = studentService.Findclasses();
        return BaseResult.ok("查询成功",clist);
    }

    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    private BaseResult addStudent(@RequestBody Student student){
     
        studentService.addStudent(student);
        return BaseResult.ok("添加成功");
    }

    /**
     *  批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteStudentBysid/{ids}")
    public BaseResult deleteStudentBysid(@PathVariable("ids") String ids){
     
        studentService.deleteStudentBysid(ids);
        return BaseResult.ok("删除成功");
    }

    /**
     *  修改第一步: 回显对象
     * @param sid
     * @return
     */
    @GetMapping("/FindStudent/{sid}")
    public BaseResult FindStudent(@PathVariable("sid") Integer sid){
     
        Student student =  studentService.FindStudent(sid);
        return BaseResult.ok("查询成功",student);
    }


}

  • service
package com.czxy.service;

import com.czxy.domain.Classes;
import com.czxy.domain.Student;
import com.czxy.domain.StudentPi;
import com.github.pagehelper.PageInfo;

import java.util.List;

public interface StudentService {
     
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    PageInfo<Student> FindStudentListForPageNum(StudentPi studentPi);
    /**
     * 查询班级信息
     * @return
     */
    List<Classes> Findclasses();
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    void addStudent(Student student);
    /**
     *  批量删除
     * @param ids
     * @return
     */
    void deleteStudentBysid(String ids);
    /**
     *  修改第一步: 回显对象
     * @param sid
     * @return
     */
    Student FindStudent(Integer sid);
}

  • serviceImpl
package com.czxy.service.impl;

import com.czxy.domain.Classes;
import com.czxy.domain.Student;
import com.czxy.domain.StudentPi;
import com.czxy.mapper.ClassesMapper;
import com.czxy.mapper.StudentMapper;
import com.czxy.mapper.TeacherMapper;
import com.czxy.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
     
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @Override
    public PageInfo<Student> FindStudentListForPageNum(StudentPi studentPi) {
     
        PageHelper.startPage(studentPi.getPageNum(),studentPi.getPageSize());
    //    设置姓名
        if (studentPi.getSname() != null && !"" .equals(studentPi.getSname())){
     
            studentPi.setSname("%"+studentPi.getSname()+"%");
        }
        List<Student> slist =  studentMapper.FindStudentListForPageNum(studentPi);
        PageInfo<Student> pageInfo = new PageInfo<>(slist);
        return pageInfo;
    }
    /**
     * 查询班级信息
     * @return
     */
    @Override
    public List<Classes> Findclasses() {
     
        List<Classes> classes = classesMapper.selectAll();
        return classes;
    }
    /**
     *  添加一个学生
     * @param student
     * @return
     */
    @Override
    public void addStudent(Student student) {
     
        studentMapper.insertSelective(student);
    }
    /**
     *  批量删除
     * @param ids
     * @return
     */
    @Override
    public void deleteStudentBysid(String ids) {
     
        String[] split = ids.split(",");
        for (String s : split) {
     
            studentMapper.deleteByPrimaryKey(Integer.parseInt(s));
        }
    }
    /**
     *  修改第一步: 回显对象
     * @param sid
     * @return
     */
    @Override
    public Student FindStudent(Integer sid) {
     
       Student student =  studentMapper.FindStudent(sid);
       return student;
    }
}

  • mapper
package com.czxy.mapper;

        import com.czxy.domain.Student;
        import com.czxy.domain.StudentPi;
        import org.apache.ibatis.annotations.*;
        import tk.mybatis.mapper.common.Mapper;

        import java.util.List;

public interface StudentMapper extends Mapper<Student> {
     
    /**
     * 学生列表(分页+多条件)
     * @param studentPi
     * @return
     */
    @Select("")
    @Results(id = "s1",value = {
     
            @Result(property = "class_id",column = "class_id"),
            @Result(property = "classes",column = "class_id",one = @One(select = "com.czxy.mapper.ClassesMapper.FindByClassesByCid"))
    })
    List<Student> FindStudentListForPageNum(@Param("studentPi") StudentPi studentPi);

    @Select("select * from tb_student where sid=#{sid}")
    @ResultMap("s1")
    Student FindStudent(@Param("sid") Integer sid);
}

你可能感兴趣的:(SpringCloud)