使用SpringBoot和MyBatis實現學生管理

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

學生管理接口

package com.yangzc.studentboot.student.controller;
import com.yangzc.studentboot.common.domain.ActionResult;
import com.yangzc.studentboot.common.utils.DownloadUtil;
import com.yangzc.studentboot.student.dao.StudentDOMapper;
import com.yangzc.studentboot.student.domain.StudentDO;
import com.yangzc.studentboot.student.vo.QueryVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: yangzc
 * @Description:
 * @Date: Created on 7:59 2019/12/24
 * @Modified By:
 */

@Controller
@RequestMapping("/stu/list")
public class StudentController {
    private String prefix = "student";

    @Value("${studentboot.uploadPath}")
    private String savePath;

    @Autowired
    StudentDOMapper studentDOMapper;

    @GetMapping(value="/", produces = MediaType.TEXT_HTML_VALUE)
    @ApiOperation(value = "學生列表頁面", notes = "返回學生列表頁面")
    @RequiresPermissions("stu:list")
    String student() {
        return prefix + "/list";
    }


    @PostMapping(value = "/welcome", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "學生集合", notes = "根據指定的開始行和行數返回學生集合", consumes = MediaType.APPLICATION_JSON_VALUE)
    @RequiresPermissions("stu:list")
    @ResponseBody
    public ActionResult getStudents(@RequestBody QueryVO params) {
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("total",studentDOMapper.count());
        List<StudentDO> students = studentDOMapper.list(params);
        data.put("rows",students);
        ActionResult result = new ActionResult(data);
        return result;
    }

    /**
     * 學生編輯
     * @param sid
     * @return
     */
    @GetMapping(value = "/edit/{sid}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "學生信息加載", notes = "根據學號獲取該學生的信息")
    @ApiImplicitParam(name = "sid", value = "學號", dataType = "int", paramType = "path", example = "1001")
    @ResponseBody
    public ActionResult loadStudent(@PathVariable Integer sid){
        ActionResult result = null;
        StudentDO student = studentDOMapper.selectByPrimaryKey(sid);
        result = ActionResult.ok(student);
        return result;
    }


    /**
     * 學生保存
     * @param stu
     * @return
     */
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "學生信息保存", notes = "將輸入的學生信息保存到數據庫")
    @ResponseBody
    public ActionResult saveStudent(StudentDO stu){
        ActionResult result = null;
        if(stu.getSno()==null||stu.getSno()==0){
            studentDOMapper.insert(stu);
        } else {
            studentDOMapper.updateByPrimaryKey(stu);
        }
        result = ActionResult.ok();
        return result;
    }


    /**
     * 學生刪除
     * @param sid
     * @return
     */
    @ApiOperation(value = "刪除學生信息", notes = "根據學號刪除該學生的信息")
    @ApiImplicitParam(name = "sid", value = "學號", dataType = "int", paramType = "path", example = "1001")
    @DeleteMapping(value = "/delete/{sid}",  produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ActionResult delStudent(@PathVariable Integer sid){
        ActionResult result = null;
        studentDOMapper.deleteByPrimaryKey(sid);
        result = ActionResult.ok();
        return result;
    }


    /**
     * 文件上傳
     * @param file
     * @param req
     * @return
     */
    @ApiOperation(value = "頭像上傳", notes = "文件上傳")
    @ApiImplicitParam(name = "source", value = "圖片", dataType = "__file", required = true, paramType = "form")
    @PostMapping(value = "/upload/file", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ActionResult uploadFile(@RequestParam("source") MultipartFile file, HttpServletRequest req) {
        ActionResult result = null;
        try {
            // 截取不同類型的文件需要自行判斷
            String filename = file.getOriginalFilename();
            if (!file.isEmpty()) {
                String extName = filename.substring(filename.indexOf("."));// 取文件格式後綴名
                String uuid = UUID.randomUUID().toString().replace("-", "");
                // 新名稱
                String newName = uuid + extName;// 在這裏用UUID來生成新的文件夾名字,這樣就不會導致重名
                file.transferTo(new File(savePath+"/"+newName));
                result = ActionResult.ok("/files/"+newName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 導出學生列表
     * @return
     */
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "導出學生列表", notes = "導出學生列表")
    @ResponseBody
    public ActionResult export(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        //1.創建工作簿
        //String path = servletContext.getRealPath("/");
        //path = path+"/make/students.xls";   //得到模板文件所在位置

        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:make/students.xls");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return new ActionResult(-1,"出現異常",null);
        }

        InputStream is = null;     //根據文件,得到指定的文件流
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return new ActionResult(-1,"出現異常",null);
        }

        //根據文件流,加載指定的工作簿
        //它只能操作excel2003版本
        Workbook wb = null;
        try {
            wb = new HSSFWorkbook(is);
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResult(-1,"出現異常",null);
        }

        //2.讀取工作表
        Sheet sheet = wb.getSheetAt(0);   //0代表工作表的下標

        //抽取出一些公用變量
        Row nRow=null;
        Cell nCell = null;

        int rowNo=1;//行號
        int cellNo=0;//列號

        //===========================================數據內容
        nRow = sheet.getRow(rowNo);//讀取第2行

        //
        CellStyle snoCellStyle = nRow.getCell(cellNo++).getCellStyle();//讀取單元格的樣式
        String str = nRow.getCell(cellNo).getStringCellValue();//讀取單元格的內容
        System.out.println(str);

        CellStyle snameCellStyle = nRow.getCell(cellNo++).getCellStyle();//讀取單元格的樣式
        CellStyle isMaleCellStyle = nRow.getCell(cellNo++).getCellStyle();//讀取單元格的樣式
        CellStyle birthCellStyle = nRow.getCell(cellNo++).getCellStyle();//讀取單元格的樣式

        List<StudentDO> list = studentDOMapper.selectAll();
        //遍歷學生列表
        for(StudentDO stu :list){
            //產生一個新行
            nRow = sheet.createRow(rowNo++);
            //nRow.setHeightInPoints(24f);//設置行高

            cellNo=0;

            nCell = nRow.createCell(cellNo++);//創建單元格
            nCell.setCellValue(stu.getSno());//設置單元格內容
            nCell.setCellStyle(snoCellStyle);    //設置單元格樣式

            nCell = nRow.createCell(cellNo++);//創建單元格
            nCell.setCellValue(stu.getSname());//設置單元格內容
            nCell.setCellStyle(snameCellStyle);    //設置單元格樣式

            nCell = nRow.createCell(cellNo++);//創建單元格
            nCell.setCellValue(stu.getGender());//設置單元格內容
            nCell.setCellStyle(isMaleCellStyle);    //設置單元格樣式

            nCell = nRow.createCell(cellNo++);//創建單元格
            nCell.setCellValue(stu.getBirth());//設置單元格內容
            nCell.setCellStyle(birthCellStyle);    //設置單元格樣式

        }

        //輸出
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//內存的緩衝區
        try {
            wb.write(byteArrayOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        DownloadUtil downloadUtil = new DownloadUtil();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //response.setHeader("Transfer-Encoding", "chunked");
        String returnName = "students" + sdf.format(cal.getTime()) + ".xls";
        try {
            downloadUtil.download(byteArrayOutputStream, response, returnName);
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResult(-1,"出現異常",null);
        }
        return null;
    }


    /**
     * 導入學生列表
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value = "import", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "file", value = "Excel文件", dataType = "__file", required = true, paramType = "form")
    @ApiOperation(value = "導入學生列表", notes = "導入學生列表")
    @ResponseBody
    public ActionResult upload(@RequestParam("file") MultipartFile file) throws Exception {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(file.getInputStream());
        List<StudentDO> getData = readOldExcel(hssfWorkbook);
        if (getData == null) {
            return new ActionResult(-1,"解析文件失敗",null);
        }
        file.getInputStream().close();

        for(StudentDO stu:getData) {
            studentDOMapper.insert(stu);
        }
        return ActionResult.ok(getData);
    }

    //處理2007之前的excel
    private List<StudentDO> readOldExcel(HSSFWorkbook hssfWorkbook) {
        List<StudentDO> students = new ArrayList<StudentDO>();
        HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
        HSSFCell cell = null;
        HSSFRow row = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = sheetAt.getFirstRowNum()+1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
            row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            Object[] objects = new Object[row.getLastCellNum()];
            for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                switch (cell.getCellTypeEnum()) {
                    case STRING:
                        objects[j] = cell.getStringCellValue();
                        System.out.println(cell.getStringCellValue());
                        break;
                    case _NONE:
                        objects[j] = "";
                        break;
                    case BOOLEAN:
                        objects[j] = cell.getBooleanCellValue();
                        System.out.println(cell.getBooleanCellValue());
                        break;
                    case NUMERIC:
                        //處理double類型的  1.0===》1
                        DecimalFormat df = new DecimalFormat("0");
                        String s = df.format(cell.getNumericCellValue());
                        objects[j] = s;
                        System.out.println(s);
                        break;
                    default:
                        objects[j] = cell.toString();
                }
            }
            //處理數據
            if (objects != null) {
                StudentDO stu = new StudentDO();
                stu.setSname((String) objects[1]);
                stu.setGender((String)objects[2]);
                stu.setBirth(row.getCell(3).getDateCellValue());
                students.add(stu);
            }
        }
        return students;
    }

}

學生管理頁面(html)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>學生管理系統</title>
    <link rel="stylesheet" type="text/css" href="/bootstrap/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="/bootstrap-datetimepicker/bootstrap-datetimepicker.css"/>
    <link rel="stylesheet" href="/bootstrap-table/bootstrap-table.css">
    <link rel="stylesheet" href="/bootstrap-fileinput/fileinput.css">
    <link rel="stylesheet" href="/bootstrap-fileinput/fileinput-rtl.css">
    <script src="/jquery/jquery.js"></script>
    <script src="/bootstrap/bootstrap.js" type="text/javascript" charset="utf-8"></script>
    <script src="/bootstrap-datetimepicker/bootstrap-datetimepicker.js" type="text/javascript" charset="utf-8"></script>
    <script src="/bootstrap-datetimepicker/bootstrap-datetimepicker.zh-CN.js" type="text/javascript" charset="utf-8"></script>
    <script src="/bootstrap-table/bootstrap-table.js"></script>
    <script src="/bootstrap-table/bootstrap-table-zh-CN.js"></script>
    <script src="/bootstrap-fileinput/fileinput.js"></script>
    <script src="/bootstrap-fileinput/zh.js"></script>
    <script src="/app/FileSaver.js"></script>
    <script src="/app/student.js"></script>
    <script type="text/javascript">
        $(function(){
            $('.form_date').datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0,
            });

            initMainTable();
        })
    </script>
    <script>
        $(document).ready(function() {
            $("#input-b9").fileinput({
                showPreview: false,
                showUpload: true,
                elErrorContainer: '#kartik-file-errors',
                allowedFileExtensions: ["xls","xlsx"],
                uploadUrl: 'import'
            });
        });
    </script>
</head>
<body class="bg-info">
<div>
    <table id="grid" class="table" data-toolbar=".toolbar"></table>
</div>
<div id="toolbar" class="toolbar">
    <button type="button" class="btn btn-primary" onclick="add();">
        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加
    </button>
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
        <span class="glyphicon glyphicon-import" aria-hidden="true"></span> 導入
    </button>
    <button type="button" class="btn btn-primary" onclick="exportData();">
        <span class="glyphicon glyphicon-export" aria-hidden="true"></span> 導出
    </button>
</div>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="file-loading">
                    <input id="input-b9" name="file" multiple type="file">
                </div>
                <div id="kartik-file-errors"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" title="Your custom upload logic">Save</button>
            </div>
        </div>
    </div>
</div>



<!-- 模態框(Modal) -->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3 class="modal-title" id="subject"></h3>
            </div>
            <div class="modal-body">
                <form role="form" id="register">
                    <div class="form-group" id="first">
                        <label for="sid">學號</label>
                        <input type="text" class="form-control" id="sid" name="sno" readonly>
                    </div>
                    <div class="form-group">
                        <label for="sname">姓名</label>
                        <input type="text" class="form-control" id="sname" name="sname" placeholder="請輸入姓名">
                    </div>
                    <label>性別</label>
                    <div class="form-group">
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="male" value="1" checked> 靚仔
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="female" value="0"> 美女
                        </label>
                    </div>
                    <div class="form-group">
                        <label for="birth">生日</label>
                        <div class="input-group date form_date" data-date="" data-date-format="yyyy MM dd" data-link-field="birth" data-link-format="yyyy-mm-dd">
                            <input name="birth" id="birth" class="form-control" type="text" value="" readonly>
                            <span class="input-group-addon">
							<span class="glyphicon glyphicon-remove"></span>
						</span>
                            <span class="input-group-addon">
							<span class="glyphicon glyphicon-calendar"></span>
						</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="photo">上傳頭像</label>
                        <input type="file" id="photo" name="photo">
                        <p class="help-block">請上傳jpg或者png格式的文件!</p>
                    </div>
                    <div class="form-group">
                        <button type="button" class="btn btn-default" onclick="upload();">
                            <span class="glyphicon glyphicon-upload" aria-hidden="true"></span> 上傳
                        </button>
                    </div>
                    <div hidden="hidden">
                        <input type="text" name="photoUrl" id="filePath" value="/img/default.png"/>
                    </div>
                    <div class="form-group">
                        <img width="120" height="120" src="/img/default.png" id="photo2"/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button>
                <button type="button" class="btn btn-primary" onclick="save();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
</html>

學生管理頁面(js)

var $table;
var rows = 5;
function initMainTable() {	//初始化bootstrap-table的內容
    //記錄頁面bootstrap-table全局變量$table,方便應用
    var queryUrl = 'welcome';
    $table = $('#grid').bootstrapTable({
        url: queryUrl,                      //請求後臺的URL(*)
        method: 'POST',                     //請求方式(*)
        toolbar: '.toolbar',              	//工具按鈕用哪個容器
        striped: true,                      //是否顯示行間隔色
        cache: false,                       //是否使用緩存,默認爲true,所以一般情況下需要設置一下這個屬性(*)
        pagination: true,                   //是否顯示分頁(*)
        sortable: true,                     //是否啓用排序
        sortOrder: "asc",                   //排序方式
        sortName: "sno",
        sidePagination: "server",           //分頁方式:client客戶端分頁,server服務端分頁(*)
        pageNumber: 1,                      //初始化加載第一頁,默認第一頁,並記錄
        pageSize: rows,                     //每頁的記錄行數(*)
        pageList: [10, 25, 50, 100],        //可供選擇的每頁的行數(*)
        search: false,                      //是否顯示錶格搜索
        strictSearch: true,
        showColumns: true,                  //是否顯示所有的列(選擇顯示的列)
        showRefresh: true,                  //是否顯示刷新按鈕
        minimumCountColumns: 2,             //最少允許的列數
        clickToSelect: true,                //是否啓用點擊選中行
        //height: 500,                      //行高,如果沒有設置height屬性,表格自動根據記錄條數覺得表格高度
        uniqueId: "sno",                     //每一行的唯一標識,一般爲主鍵列
        showToggle: true,                   //是否顯示詳細視圖和列表視圖的切換按鈕
        cardView: false,                    //是否顯示詳細視圖
        detailView: false,                  //是否顯示父子表
        //得到查詢的參數
        queryParams : function (params) {
            //這裏的鍵的名字和控制器的變量名必須一直,這邊改動,控制器也需要改成一樣的
            var temp = {
                rows: params.limit,                         //頁面大小
                page: (params.offset / params.limit) + 1,   //頁碼
                begin: params.offset,
                sort: params.sort,      //排序列名  
                order: params.order //排位命令(desc,asc)
            };
            return JSON.stringify(temp);
        },
        columns: [{
            checkbox: true,
            visible: true                  //是否顯示覆選框  
        }, {
            field: 'sno',
            title: '學號',
            sortable: true
        }, {
            field: 'sname',
            title: '姓名',
            sortable: true
        }, {
            field: 'gender',
            title: '性別',
            sortable: true,
            formatter: genderFormatter
        }, {
            field: 'birth',
            title: '生日'
        }, {
            field:'sno',
            title: '操作',
            width: 120,
            align: 'center',
            valign: 'middle',
            formatter: actionFormatter
        }, ],
        onLoadSuccess: function () {
        },
        onLoadError: function () {
            alert("數據加載失敗!");
        },
        onDblClickRow: function (row, $element) {
            var id = row.sno;
            edit(id);
        },
        responseHandler: responseHandler,
    });
};


function actionFormatter(value, row, index) {	//操作欄的格式化
    var id = value;
    var result = "";
    result += "<a href='javascript:;' class='btn btn-xs green' οnclick=\"view(" + id + ")\" title='查看'><span class='glyphicon glyphicon-search'></span></a>";
    result += "<a href='javascript:;' class='btn btn-xs blue' οnclick=\"edit(" + id + ")\" title='編輯'><span class='glyphicon glyphicon-pencil'></span></a>";
    result += "<a href='javascript:;' class='btn btn-xs red' οnclick=\"del(" + id + ")\" title='刪除'><span class='glyphicon glyphicon-remove'></span></a>";
    return result;
}

/**
 * 獲取返回的數據的時候做相應處理,讓bootstrap table認識我們的返回格式
 * @param {Object} res
 */
function responseHandler(res) {
    return {
        "rows": res.data.rows, // 具體每一個bean的列表
        "total": res.data.total // 總共有多少條返回數據
    }
}

//性別字段格式化
function genderFormatter(value) {
    var gender;
    if (value == '女' || value == '0') {
        color = 'Red';
        gender = '小姐姐';
    }
    else if (value == '男' || value == '1') {
        color = 'Blue';
        gender = '大帥哥';
    }
    else { color = 'Yellow'; }

    return '<div  style="color: ' + color + '">' + gender + '</div>';
}

function add(){
    $("#subject").text("註冊學生信息");
    $("#first").hide();
    $("#register")[0].reset();
    $("#photo2").attr("src","/img/default.png");
    //$("#edit").css("display","block");
    $('.form_date').datetimepicker("setDate", new Date());
    $('#edit').modal('show');
}

function edit(sid){
    $("#register")[0].reset();
    $("#subject").text("修改學生信息");
    $("#first").show();
    $("#photo2").attr("src","/img/default.png");
    //var sid = $(e).parent().siblings().first().text();
    //alert(sid);
    $.ajax({
        //幾個參數需要注意一下
        type: "GET",//方法類型
        cache: false,
        dataType: "json",//預期服務器返回的數據類型
        url: "edit/"+sid ,//url
        success: function (data) {
            //console.log(data);//打印服務端返回的數據(調試用)
            if (data.status == 200) {
                var stu = data.data;
                $("#sid").val(stu.sno);
                $("#sname").val(stu.sname);
                if(stu.gender == "男" || stu.gender == "1"){
                    $("#male").prop("checked",true);
                }else{
                    $("#female").prop("checked",true);
                }
                //$("#birth").val(stu.birth);
                $('.form_date').datetimepicker("setDate", new Date(stu.birth));
                $("#filePath").val(stu.photoUrl);
                $("#photo2").attr("src",stu.photoUrl);
                $('#edit').modal('show');
            }
        },
        error : function() {
            alert("異常!");
        }
    });
}

function save(){
    $.ajax({
        //幾個參數需要注意一下
        type: "post",//方法類型
        dataType: "json",//預期服務器返回的數據類型
        url: "save" ,//url
        data: $("#register").serialize(),
        success: function (data) {
            //console.log(data);//打印服務端返回的數據(調試用)
            if (data.status == 200) {
                $('#edit').modal('hide');
                //$("#edit").css("display","none");
                //$("#register")[0].reset();
                //$("#photo2").attr("src","images/default.png");
                //alert(data.message);
                $table.bootstrapTable('refresh');
            }
        },
        error : function() {
            alert("異常!");
        }
    });
}

function upload(){
    var file = $("#photo").get(0).files[0];
    var formData = new FormData();
    formData.append("source",file);
    $.ajax({
        url:"upload/file",
        type:"post",
        dataType:"json",
        cache:false,
        data:formData,
        contentType:false,
        processData:false,
        success:function(data){
            if(data.status==200){
                $("#photo2").attr("src",data.data);
                $("#filePath").val(data.data);
            }
            console.log("hello test");
        }
    });
}

function del(sid){
    //var sid = $(e).parent().siblings().first().text();
    var yesOrNo = confirm("確定要刪除該學生麼?");
    if(yesOrNo){
        $.ajax({
            //幾個參數需要注意一下
            type: "DELETE",//方法類型
            dataType: "json",//預期服務器返回的數據類型
            url: "delete/"+sid ,//url
            success: function (data) {
                console.log(data.msg);//打印服務端返回的數據(調試用)
                if (data.status == 200) {
                    $table.bootstrapTable('refresh');
                }
            },
            error : function() {
                alert("異常!");
            }
        });
    }
}

function exportData(){
    //window.open("export" ,"_blank");
    var xhr ;
    if(window.XMLHttpRequest){//code for IE7+,Firefox,Chrome,Opera,Safari
        xhr = new XMLHttpRequest();
    }else{//code for IE6,IE5
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = 'export';
    xhr.open("post", url, true);
    //設置響應類型爲blob類型
    xhr.responseType = "blob";
    xhr.onload = function () {
        if (this.status == "200") {
            var name = xhr.getResponseHeader("Content-disposition");
            var fileName = name.substring(20, name.length);
            //獲取響應文件流  
            var blob = this.response;
            if (blob && blob.size > 0) {
                saveAs(blob, fileName);//處理二進制數據,讓瀏覽器認識它
            }
        }
    }
    xhr.send();
}

Swagger配置

package com.yangzc.studentboot.common.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.stream.Stream;

import static java.util.stream.Collectors.toSet;

/**
 * @Author: yangzc
 * @Description:
 * @Date: Created on 16:47 2019/11/23
 * @Modified By:
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig{
    @Bean
    public Docket customDocket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        return docket.apiInfo(apiInfo()).
                pathMapping("/")
                .select() // 選擇那些路徑和api會生成document
                //.apis(RequestHandlerSelectors.any())// 對所有api進行監控
                .apis(RequestHandlerSelectors.basePackage("com.yangzc.studentboot.student"))
                //不顯示錯誤的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))//錯誤路徑不監控
                .paths(PathSelectors.regex("/.*"))// 對根下所有路徑進行監控
                .build().protocols(Stream.of("http", "https").collect(toSet()));
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("yangzc","https://github.com/yangzc23/","[email protected]");
        return new ApiInfoBuilder().
                title("學生管理API").
                description("學生管理接口說明文檔").
                contact(contact).
                version("1.0.0").
                build();
    }

}


github項目地址

https://github.com/yangzc23/studentboot

參考資料

[01] springmvc接收日期參數轉換
[02] bootstrap-datetimepicker 日期插件設置默認初始值爲上一個月
[03] @DateTimeFormat 和 @JsonFormat 註解
[04] Java 日期 解析減少一天問題。神奇
[05] java 關於時間返回結果與參數的註解@DatetimeFormat和@JsonFormat
[06] 在SpringBoot中 日期型轉json格式 少8個小時 時區問題
[07] SwaggerBootstrapUi手冊
[08] swagger配置和簡單使用

微信掃一掃關注公衆號
image.png
點擊鏈接加入羣聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN
軟件測試學習交流QQ羣號:511619105

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章